在Swift中似乎沒有dismissMoviePlayerViewControllerAnimated
上的任何SO帖子,所以我想我會解決它。dismissMoviePlayerViewControllerAnimated在Swift中不工作
我有一個表格單元格,當您長按它時,它會顯示一個視頻。當視頻結束時,我的目標是讓用戶回到桌面視圖。最後一塊是沒有工作的那一點。
在這裏的任何幫助將不勝感激。我已經通過Apple文檔和一些關於Objective-C的文章進行了閱讀。似乎答案是運行dismissMoviePlayerViewControllerAnimated
,UIViewController上的方法,但它不工作。
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer:MPMoviePlayerController!
@IBOutlet weak var longPressView: UIView!
let longPressRec = UILongPressGestureRecognizer()
func longPressedView() {
playVideo()
}
func videoHasFinishedPlaying(notification: NSNotification){
println("Video finished playing")
self.dismissMoviePlayerViewControllerAnimated()
// not returning me to the ViewController
}
func playVideo() {
// get path and url of movie
let path = NSBundle.mainBundle().pathForResource("IMG_8602", ofType:"MOV")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
// construct the views
moviePlayer.view.frame = self.view.bounds
self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true
// remove controls at top and bottom of video
moviePlayer.controlStyle = MPMovieControlStyle.None
// add event observer for videoHasFinsihedPlaying
NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoHasFinishedPlaying:",
name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
longPressRec.addTarget(self, action: "longPressedView")
longPressView.addGestureRecognizer(longPressRec)
longPressView.userInteractionEnabled = true
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
感謝忍耐。我將所有'MPMoviePlayerController'改爲'MPMoviePlayerViewController's。這還需要我調用'moviePlayer.moviePlayer.controlStyle = MPMovieControlStyle.None'(注意雙倍的'moviePlayer',第一個是var名稱,第二個是'MPMoviePlayerViewController'上的方法 – 2015-01-20 22:00:18