import AVFoundation
import UIKit
import AVKit
class Class1: AVPlayerViewController, AVPlayerViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func playVideo() {
if let path = NSBundle.mainBundle().pathForResource("output_aV7EL0", ofType: "mp4") {
let url = NSURL(fileURLWithPath: path)
player = AVPlayer(URL: url)
player?.play()
}
else {
print("Oops, something wrong when playing video.mp4")
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
func AVPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
if player.playing {
player.stop()
}
}
}
我已经获得要播放的 mp4 文件,但视频播放完毕后我需要切换到不同的 View ,我该怎么做?任何提示将不胜感激。
func playVideo() {
if let path = NSBundle.mainBundle().pathForResource("output_aV7EL0", ofType: "mp4") {
let url = NSURL(fileURLWithPath: path)
player = AVPlayer(URL: url)
player?.play()
self.dismissViewControllerAnimated(true, completion: nil)
self.presentViewController(viewcontrollerToPresent: GameViewController1, animated: true, completion: nil)
}
最佳答案
“切换”到不同的 View 是什么意思?能具体点吗?
如果您想关闭页面,请执行以下操作:
如果它位于导航 Controller 中:
self.navigationController?.popViewControllerAnimated(true)
如果出现:
self.dismissViewControllerAnimated(true, completion: nil)
如果你想展示另一个 View Controller :
如果它位于导航 Controller 中:
self.navigationController?.pushViewController(yourViewController, animated: true)
如果您想以模态方式呈现它:
self.presentViewController(vc, animated: true, completion: nil)
关于xcode - 播放 mp4 文件,然后切换到 View Controller ,文件播放正常,但无法找出切换到另一个 View 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924591/