开发者

How to change UIModalTransitionStyle for MPMoviePlayerViewController

When I use the MPMoviePlayerViewController, I don't seem to be able to change the modalTransitionStyle to anything other than the default slide up animation.

Has anyone else managed to get this to work?

MPMoviePlayerViewController* theMoviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:videoURL]];开发者_运维知识库
theMoviePlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; // doesn't work
[self presentMoviePlayerViewControllerAnimated:theMoviePlayer];

Thanks


The way that I found to launch a ´MPMoviePlayerViewController´ instance with a CrossDisolve modal animation is to launch the movie player inside the navigation controller, like this:

NSURL * videoUrl = [[NSURL alloc] initFileURLWithPath:videoPath];
MPMoviePlayerViewController * moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];

UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:moviePlayerController];
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[navController setNavigationBarHidden:YES];

[self presentViewController:navController animated:YES completion:nil];

and the listen to MPMoviePlayerPlaybackDidFinishNotification notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

and dismiss it when the video has finished:

-(void)movieDidFinish:(NSNotification *)notification {

    [self dismissViewControllerAnimated:YES completion:nil];
}


Looking at

http://www.drobnik.com/touch/2010/07/the-3-2-hurdle-of-mpmovieplayercontroller/

It seems the code there also sets the modalTransitionStyle of the view controller presenting the MPMoviePlayerViewController instance to the same value. Does that work?


Ecarrion's answer was very helpful -- here's a Swift version, hopefully save someone some time.

import MediaPlayer

class ViewController: UIViewController {

    ...

    func playAction() {

    // setup the media player view
    var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
    var moviePlayerView = MPMoviePlayerViewController(contentURL: url)
    moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyle.None
    moviePlayerView.moviePlayer.repeatMode = MPMovieRepeatMode.None

    // register the completion
    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: "videoHasFinishedPlaying:",
        name: MPMoviePlayerPlaybackDidFinishNotification,
        object: nil)

    // instantiate nav controller and add the moviePlayerView as its root
    var navController = UINavigationController(rootViewController:  moviePlayerView)

    // set transition (this is what overrides the animated "slide up" look
    navController?.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    navController?.setNavigationBarHidden(true, animated: false)

    // present the nav controller
    self.presentViewController(navController!, animated: true, completion: nil)
}


func videoHasFinishedPlaying(notification: NSNotification){
    println("Video finished playing")

    self.dismissViewControllerAnimated(false, completion: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self)

    let reason =
    notification.userInfo![MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
        as NSNumber?

    if let theReason = reason{

        let reasonValue = MPMovieFinishReason(rawValue: theReason.integerValue)

        switch reasonValue!{
        case .PlaybackEnded:
            // The movie ended normally
            println("Playback Ended")
        case .PlaybackError:
            // An error happened and the movie ended
            println("Error happened")
        case .UserExited:
            // The user exited the player
            println("User exited")
        default:
            println("Another event happened")
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜