2015-06-19 17 views
9

Ho un video che si apre in un MPMoviePlayerController nella mia app. Funziona tutto alla grande tranne il pulsante Done che dovrebbe chiudere il lettore. La prima volta che si riproduce il video il pulsante Done funziona alla grande. Ma se lo metti in pausa mentre lo stai guardando, quindi premi Done la seconda volta che provi a riprodurre il video, il pulsante Done non funziona. Ho fatto una registrazione dello schermo qui per renderlo un po 'più semplice da capire: http://1drv.ms/1JcdodcSwift - Can not Dismiss MPMoviePlayerViewController

Qualcuno può aiutare?

Questo è il mio codice che sto usando:

import UIKit 
import MediaPlayer 

class MainContent: UIViewController { 

//Movie Player variables 
    var movieViewController : MPMoviePlayerViewController? 
    var movieplayer : MPMoviePlayerController! 


override func viewDidLoad() { 

     super.viewDidLoad() 

     //Video Player setup 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil) 

     var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")! 
     movieViewController = MPMoviePlayerViewController(contentURL: url) 

} 



func doneButtonClick(sender:NSNotification?){ 
    let value = UIInterfaceOrientation.Portrait.rawValue 
    UIDevice.currentDevice().setValue(value, forKey: "orientation") 
    } 

     //when watch button is pressed, present the movie player 
    @IBAction func watchPressed(sender: AnyObject) 
    {self.presentMoviePlayerViewControllerAnimated(movieViewController)} 

} 

risposta

3

Per risolvere questo problema, ho aggiunto il myMoviePlayerViewController.moviePlayer.stop() alla funzione 'doneButtonClick'. Ho poi aggiunto il myMoviePlayerViewController.moviePlayer.play() di nuovo al

@IBAction func watchPressed(sender: AnyObject) 
    {self.presentMoviePlayerViewControllerAnimated(movieViewController)} 

} 

Quindi, tutto sommato una semplice correzione! Il codice è qui sotto:

import UIKit 
import MediaPlayer 

class MainContent: UIViewController { 

//Movie Player variables 
    var movieViewController : MPMoviePlayerViewController? 
    var movieplayer : MPMoviePlayerController! 


override func viewDidLoad() { 

     super.viewDidLoad() 

     //Video Player setup 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil) 

     var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")! 
     movieViewController = MPMoviePlayerViewController(contentURL: url) 

} 

func doneButtonClick(sender:NSNotification?){ 
    let value = UIInterfaceOrientation.Portrait.rawValue 
    UIDevice.currentDevice().setValue(value, forKey: "orientation") 
    movieViewController?.moviePlayer.stop() 
    } 

    @IBAction func watchPressed(sender: AnyObject){ 
     self.presentMoviePlayerViewControllerAnimated(movieViewController) 
     movieViewController?.moviePlayer.play() 
    } 
} 
Problemi correlati