gpt4 book ai didi

ios - 电影播放完毕后关闭 AVPlayer

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:46 25 4
gpt4 key购买 nike

我正在制作一个简单的 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 中可能有一些内容,但我不确定如何实现它。谢谢。

最佳答案

这是在 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

关于ios - 电影播放完毕后关闭 AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40099223/

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