gpt4 book ai didi

ios - 如何使用 AVPlayer addperiodictimeobserverforinterval 函数更新 uilabel?

转载 作者:行者123 更新时间:2023-11-28 16:04:37 32 4
gpt4 key购买 nike

我正在尝试用 AVPlayer 剩余时间更新 UILabel。我能够在视频开始后立即将 UILabel 设置为视频的当前剩余时间,但该数字不会随着视频的进行而改变。我搜索了 Apple 的文档并找到了 this block观察视频的时间,但此资源和 Stackoverflow 上的其他问题不一定指出如何使用此功能更新 UILabel。

这是我的编译代码,但我不确定如何继续:

import UIKit
import AVKit
import AVFoundation

extension UIView {
func fadeTransition(duration:CFTimeInterval) {
let animation:CATransition = CATransition()
animation.timingFunction = CAMediaTimingFunction(name:
kCAMediaTimingFunctionEaseInEaseOut)
animation.type = kCATransitionFade
animation.duration = duration
self.layer.add(animation, forKey: kCATransitionFade)
}
}

class MeetTheAuthorViewController: UIViewController {




@IBOutlet weak var videoTimeRemainingLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
//playBackgroundMusic("bensound-jazzyfrenchy-castlesbackground.mp3")
// Do any additional setup after loading the view.


}

func btn_clicked(_ sender: UIBarButtonItem) {
// Do something
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "videoSegue"
{
//set up the player
let videoURL = Bundle.main.url(forResource: "The Atlanta Goat_ Part One-HD", withExtension: "mp4")
let videoViewController = segue.destination as! AVPlayerViewController
videoViewController.player = AVPlayer(url: videoURL!)
videoViewController.player?.play()

//this code is an attempt to have a uilabel update with the time remaining in video
let videoDuration = CMTimeGetSeconds((videoViewController.player?.currentItem?.asset.duration)!)
let videoCurrentTime = videoViewController.player?.currentItem?.currentTime()
let timeRemainingInVideo = videoDuration - CMTimeGetSeconds(videoCurrentTime!)

videoTimeRemainingLabel.fadeTransition(duration: 0.4)
videoTimeRemainingLabel.text = String(describing: timeRemainingInVideo)


/*after the code above which updates the label initially, this code below is an attempt to update the UILabel over time as the video plays
*/
func addPeriodicTimeObserver() {
// Invoke callback every half second
let interval = CMTime(seconds: 0.5,
preferredTimescale: CMTimeScale(NSEC_PER_SEC))
// Queue on which to invoke the callback
let mainQueue = DispatchQueue.main
// Add time observer
var timeObserverToken =
videoViewController.player?.addPeriodicTimeObserver(forInterval: interval, queue: mainQueue) {
[weak self] time in
// update player transport UI
}
}
//end of attempt to update time remaining label

NotificationCenter.default.addObserver(self, selector: #selector(MeetTheAuthorViewController.playerDidFinishPlaying),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: videoViewController.player?.currentItem)



}
}

func playerDidFinishPlaying() {
print("Video Finished")
self.dismiss(animated: true, completion: nil)
}



}

最佳答案

在注释 //update player transport UI 的地方,您需要重新计算 timeRemainingInVideo 并更新标签。

关于ios - 如何使用 AVPlayer addperiodictimeobserverforinterval 函数更新 uilabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40478362/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com