【问题标题】:Close AVPlayer when movie is complete电影完成后关闭 AVPlayer
【发布时间】:2017-02-27 04:40:45
【问题描述】:

我正在制作一个简单的 iPad 应用程序,以便在按下按钮时播放电影。电影播放,当电影完成后,我想关闭 AVPlayerView 以便它返回主屏幕。 目前,当视频完成时,它会停留在最后一帧。 我现在的 ViewController.Swift。

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {
//MARK : Properties 

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
//MARK: Actions

@IBAction func playButton(_ sender: AnyObject) {

    let movieURL = Bundle.main.url(forResource: "ElephantSeals", withExtension: "mov")!
    let player = AVPlayer(url: movieURL as URL)

    let playerViewController = AVPlayerViewController()

    playerViewController.player = player

    self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
        }
//    player.actionAtItemEnd = playerViewController.dismiss(animated: true)
}
}

如您所见,我认为 actionAtItemEnd 中可能有一些东西,但我不确定如何实现它。 谢谢。

【问题讨论】:

    标签: ios swift avfoundation avkit


    【解决方案1】:

    这是 swift 5.3 和 iOS 14.2 中的工作代码,试试这个并告诉我...:)

    import UIKit
    import AVKit
    import AVFoundation
    
    class ViewController: UIViewController {
        
        let playerViewController = AVPlayerViewController()
        
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
        
        @IBAction func playButton(_ sender: AnyObject) {
            
            let movieURL = Bundle.main.url(forResource: "ElephantSeals", withExtension: "mp4")!
            let player = AVPlayer(url: movieURL as URL)
            
            playerViewController.player = player
            NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerViewController.player?.currentItem)
            
            self.present(playerViewController, animated: true) {
                self.playerViewController.player!.play()
            }
        }
        
        
        @objc func playerDidFinishPlaying(note: NSNotification) {
            self.playerViewController.dismiss(animated: true)
        }
    }
    

    您可以从这里下载相同的示例项目 https://github.com/deepakiosdev/AVPlayerViewControllerDemo

    【讨论】:

    • 非常感谢,很清楚明白了。这对我有用!
    • 当播放器播放完毕时,当前时间不设置为零,如果用户再次尝试播放视频,它将从最后开始。在 player.currentItem 上的 playerDidFinishPlaying 调用 seek(to: CMTime(seconds: 0.0, preferredTimescale: 1) 中。此外,.AVPlayerItemDidPlayToEndTime 通知带有警告“此通知可能发布在与观察者所在的线程不同的线程上已注册。”因此应在主线程上关闭 AVPlayerViewController。
    【解决方案2】:

    斯威夫特 4

    let playerController = AVPlayerViewController()
    
    private func playVideo() {
        guard let path = Bundle.main.path(forResource: "p810", ofType:"mp4") else {
            debugPrint("video.m4v not found")
            return
        }
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        playerController.player = player
        NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerController.player?.currentItem)
    
        present(playerController, animated: true) {
            player.play()
        }
    }
    
    @objc func playerDidFinishPlaying(note: NSNotification) {
        playerController.dismiss(animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案3】:

      这是客观的c代码

      [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(nextVideo:)  name:AVPlayerItemDidPlayToEndTimeNotification object:playerViewController.player.currentItem];
      

      【讨论】:

        【解决方案4】:

        使用 NSNotificationCenter 你可以做到这一点。

         NotificationCenter.default.addObserver(self, selector: #selector(ViewController.playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object:  videoPlayer!.currentItem)
        
        @objc func playerDidFinishPlaying(note: NSNotification) {
            // here you can do your dismiss controller logic
            AVPlayerViewController.dismiss(animated: true)
            print("Video Finished")
        }
        

        【讨论】:

        • Xcode 在这段代码的第一行告诉我两件事:“NSNotificationCenter”已重命名为“NotificationCenter”
        【解决方案5】:

        调用 AVPlayerViewControllerDelegate。

        在 viewDidLoad 中初始化委托并实现该方法

        func playerViewControllerDidStopPictureInPicture(AVPlayerViewController) {
              AVPlayerViewController.dismiss(animated: true)
        }
        

        https://developer.apple.com/reference/avkit/avplayerviewcontrollerdelegate

        【讨论】:

        • 视频正常播放完毕后,不会调用该方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多