Close AVPlayer when movie is complete

2020-07-06 08:09发布

I am making a simple iPad app to play a movie when a button is pressed. The movie plays and when the movie is finished I want to close AVPlayerView so it goes back to the main screen. Currently when the video finishes it stays on the last frame. My ViewController.Swift at the moment.

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

As you can see, I think there might be something in actionAtItemEnd, but I'm not sure how to implement it. Thank you.

5条回答
老娘就宠你
2楼-- · 2020-07-06 08:47

here is objective c code

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(nextVideo:)  name:AVPlayerItemDidPlayToEndTimeNotification object:playerViewController.player.currentItem];
查看更多
做个烂人
3楼-- · 2020-07-06 08:51

This is working code in swift 3.0, try this and let me know...:)

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)
           }
}
查看更多
冷血范
4楼-- · 2020-07-06 08:55

Invoke AVPlayerViewControllerDelegate.

In viewDidLoad initialize the delegate and implement this method

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

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

查看更多
Ridiculous、
5楼-- · 2020-07-06 08:58

Using NSNotificationCenter you can do this .

 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")
}
查看更多
对你真心纯属浪费
6楼-- · 2020-07-06 09:02

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)
}
查看更多
登录 后发表回答