我的电影文件没有问题。完成按钮不会关闭视频内容。不明白为什么?此外,快进和快退按钮只会导致黑屏。我不认为我正在使用通知功能吗?
import Foundation
import UIKit
import MediaPlayer
class VideoViewController: UIViewController {
var moviePlayer:MPMoviePlayerController!
@IBAction func videoLaunch(sender: AnyObject) {
playVideo()
}
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("MyVideo", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
moviePlayer?.controlStyle = MPMovieControlStyle.Fullscreen
player.prepareToPlay()
self.view.addSubview(player.view)
}
}
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "moviePlayBackDidFinish:",
name: MPMoviePlayerPlaybackDidFinishNotification,
object: moviePlayer)
func moviePlayBackDidFinish(notification: NSNotification){
self.view.removeFromSuperview()
}
}
}
答案 0 :(得分:1)
您正在将玩家视图添加为子视图。按下完成按钮后应删除它(removeFromSuperview)。使用通知收听播放结束:
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "moviePlayBackDidFinish:",
name: MPMoviePlayerPlaybackDidFinishNotification,
object: moviePlayer)
和moviePlayBackDidFinish:
func moviePlayBackDidFinish(notification: NSNotification){
// remove from superview
}
答案 1 :(得分:1)
您应该从超级视图中删除moviePlayer,如下所示:
func moviePlayBackDidFinish(notification: NSNotification){
let moviePlayer:MPMoviePlayerController = notif.object as! MPMoviePlayerController
moviePlayer.view.removeFromSuperview()
}
因为在您的情况下,您删除了self.view