如何为MPMoviePlayerViewController更改UIModalTransitionStyle

时间:2010-09-10 15:23:38

标签: iphone ios4 mpmovieplayercontroller

当我使用MPMoviePlayerViewController时,我似乎无法将modalTransitionStyle更改为默认的上滑动画以外的任何内容。

有没有其他人设法让这个工作?

MPMoviePlayerViewController* theMoviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:videoURL]];
theMoviePlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; // doesn't work
[self presentMoviePlayerViewControllerAnimated:theMoviePlayer];

由于

3 个答案:

答案 0 :(得分:6)

我发现使用CrossDisolve模态动画启动'MPMoviePlayerViewController'实例的方法是在导航控制器内启动电影播放器​​,如下所示:

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];

并收听MPMoviePlayerPlaybackDidFinishNotification通知:

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

并在视频播放完毕后将其解雇:

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

    [self dismissViewControllerAnimated:YES completion:nil];
}

答案 1 :(得分:2)

看着

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

似乎的代码将视图控制器的modalTransitionStyle设置为将MPMoviePlayerViewController实例呈现为相同的值。这有用吗?

答案 2 :(得分:0)

Ecarrion的回答非常有帮助 - 这是一个Swift版本,希望能节省一些时间。

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")
        }
    }
}