电影完成后关闭AVPlayer

时间:2016-10-18 03:54:41

标签: ios swift avfoundation avkit

按下按钮时,我正在创建一个简单的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中可能存在某些内容,但我不确定如何实现它。 谢谢。

5 个答案:

答案 0 :(得分:15)

这是swift 3.0中正常工作的代码,试试这个让我知道...... :)

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: "mov")!
        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()
        }
    }


    func playerDidFinishPlaying(note: NSNotification) {
            self.playerViewController.dismiss(animated: true)
           }
}

答案 1 :(得分:2)

使用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")
}

答案 2 :(得分:2)

这里是客观的c代码

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

答案 3 :(得分:2)

Swift 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)
}

答案 4 :(得分:0)

调用AVPlayerViewControllerDelegate。

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

sqlalchemy.exc.InvalidRequestError: Can't call Query.update() or Query.delete() when join(), outerjoin(), select_from(), or from_self() has been called

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