2015-01-20 86 views
1

在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. 
    } 
} 

回答

1

因爲你使用MPMoviePlayerController代替MPMoviePlayerViewController您的代碼不起作用。

要調用:

self.dismissMoviePlayerViewControllerAnimated() 

但沒有MPMoviePlayerViewController解僱。這就是爲什麼沒有發生。

如果您更願意使用MPMoviePlayerController(與您發佈的代碼相同),在手動添加view之後,您還必須手動刪除它。

+1

感謝忍耐。我將所有'MPMoviePlayerController'改爲'MPMoviePlayerViewController's。這還需要我調用'moviePlayer.movi​​ePlayer.controlStyle = MPMovieControlStyle.None'(注意雙倍的'moviePlayer',第一個是var名稱,第二個是'MPMoviePlayerViewController'上的方法 – 2015-01-20 22:00:18