AVPlayer 播放视频的速度比正常速率快 6 倍或更多

AVPlayer playing video 6x or more faster than normal rate

提问人:iphoneBees 提问时间:11/16/2023 最后编辑:iphoneBees 更新时间:11/17/2023 访问量:35

问:

我有一个用于健身行业的电视应用程序,其中 6 台智能电视连接到 Apple TV,该应用程序从本地服务器播放视频。Unitl tvOS 17.0 一切都很顺利,但不久之后我们就面临着一个奇怪的问题,因为有时相同的视频仅在 6 台随机电视中播放的速度比正常速度快 1 倍或更多。 我可以看到 12 分钟的视频在 1.3 分钟内完成播放。

视频编解码器:MPEG-4 AAC、H.264。这些格式会影响 Apple TV 的速度吗?

电视最初播放介绍视频,然后热身,然后锻炼,最后冷却。我在遇到问题的地方添加了练习背景计时器视频的代码。在背景视频的顶部,还有其他 4 个视频循环播放。请找到附图。enter image description here我正在使用 AVPlayer、Swift 5、Xcode 5 和 tvOS 17.0、Apple TV 4K。

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {
    
    @IBOutlet weak var workoutCircuitContainerView: UIView!
    
    var workoutCircuitPlayerItem: AVPlayerItem?
    var workoutCircuitPlayerLayer: AVPlayerLayer?
    var workoutCircuitPlayer: AVPlayer?
    var isWorkoutCircuitPlayerObserverAdded: Bool = false
    private var workoutCircuitObserverContext = 0
   
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        initializeWorkoutCircuitPlayer()
    }
    
    func initializeWorkoutCircuitPlayer() {
        workoutCircuitPlayer?.pause()
        workoutCircuitPlayerLayer?.removeFromSuperlayer()
        self.removeWorkoutCircuitPlayerObservers()
        startWorkoutCircuitPlayer()
    }
    
    func startWorkoutCircuitPlayer() {
        guard let streamURL = URL(string: "http://192.168.1.116:3001/videos/CARDIO_Work_Circuit1.mp4") else {
            return
        }
        self.workoutCircuitPlayerItem = AVPlayerItem(url: streamURL as URL)
        self.workoutCircuitPlayer = AVPlayer(playerItem: self.workoutCircuitPlayerItem)
        self.workoutCircuitPlayerLayer = AVPlayerLayer(player: self.workoutCircuitPlayer!)
        self.workoutCircuitPlayerLayer!.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resizeAspectFill.rawValue)
        self.workoutCircuitPlayerLayer!.frame = self.workoutCircuitContainerView.bounds
        self.workoutCircuitContainerView.layer.addSublayer(self.workoutCircuitPlayerLayer!)
        self.workoutCircuitPlayer!.addObserver(self, forKeyPath: "timeControlStatus", options: [.old, .new], context: nil)
        self.playWorkoutCircuitPlayer()
        self.addWorkoutCircuitPlayerObservers()
    }
    
    func playWorkoutCircuitPlayer() {
       workoutCircuitPlayer?.play()
    }
    
    func addWorkoutCircuitPlayerObservers() {
        if !isWorkoutCircuitPlayerObserverAdded {
            workoutCircuitPlayer?.addObserver(self,
                                              forKeyPath: #keyPath(AVPlayerItem.status),
                                              options: [.old, .new],
                                              context: &workoutCircuitObserverContext)
            NotificationCenter.default.addObserver(self, selector: #selector(self.workoutCircuitPlayerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: workoutCircuitPlayer?.currentItem)
            NotificationCenter.default.addObserver(self, selector: #selector(self.workoutCircuitPlayerStalled(note:)), name: .AVPlayerItemPlaybackStalled, object: self.workoutCircuitPlayer?.currentItem)
            isWorkoutCircuitPlayerObserverAdded = true
        }
    }
    
    @objc func workoutCircuitPlayerDidFinishPlaying(notification: NSNotification) {
        // play cool down videos code
    }
    
    @objc func workoutCircuitPlayerStalled(note: NSNotification) {
        if let playerItem = note.object as? AVPlayerItem {
            playerItem.seek(to: (workoutCircuitPlayer?.currentTime())!, completionHandler: nil)
            self.workoutCircuitPlayer!.play()
        }
    }
    
    func removeWorkoutCircuitPlayerObservers() {
        if isWorkoutCircuitPlayerObserverAdded {
            workoutCircuitPlayer?.removeObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), context: &workoutCircuitObserverContext)
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: workoutCircuitPlayer?.currentItem)
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemPlaybackStalled, object: workoutCircuitPlayer?.currentItem)
            isWorkoutCircuitPlayerObserverAdded   = false
        }
    }
}

是否有人面临同样的问题或对我的情况有很好的了解,请帮忙。

在我开始面临快进问题后,尝试调用最后一行

workoutCircuitPlayer?.playImmediately(atRate: 1.0)

但我仍然面临这个问题。

Swift AVPlayer tvOS 播放速率

评论

0赞 soundflix 11/17/2023
编辑您的问题并提供足够的代码来重现您的问题。
0赞 iphoneBees 11/20/2023
@soundflix 编辑了代码并添加了有关该问题的更多信息

答: 暂无答案