开发者

problem using MPMovieController in iPhone SDK 4.0

I was using MPMoviePlayer to play a short video in my app with no problems in SDK 3.1.3. I made the changes to the code in SDK 4 but the video is not playing. I just get a black screen and audio. The Apple Dev Center doesnt have any sample code for this class for the latest SDK. Following is the code I'm using:

- (void)viewDidLoad {

    [super viewDidLoad];

        //videoPlayer is a MPMoviePlayerController object defined in the header file of the view controller

    if (videoPlayer == nil){
        NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"];
        if (videoPath == NULL){
            ret开发者_如何学Gourn; 
        }
        NSURL * videoURL = [NSURL fileURLWithPath:videoPath];

        videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
        videoPlayer.controlStyle = MPMovieControlStyleNone;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];

        [videoPlayer play];
        [videoPlayer setFullscreen:YES];
        [self.view addSubview:videoPlayer.view];


    }

}

The above results in just the audio being played with a black screen. The notification is called correctly at the end of the playback.

When the above did not work, then I even tried using the new MPMoviePlayerViewController class as follows:

- (void)viewDidLoad {

    [super viewDidLoad];
    NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"];

    if (videoPath == NULL){
        return; 
    }
    NSURL * videoURL = [NSURL fileURLWithPath:videoPath];

        //movieController is an MPMoviePlayerViewController object defined in the header file of view controller

    movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];

    [movieController.moviePlayer setFullscreen:YES];
    [movieController.moviePlayer play];
    [self presentMoviePlayerViewControllerAnimated:movieController];
}

Same problem persists - I can hear the audio and the notification at the end of the playback is called as expected. However I just see a black screen instead of the video.

There is nothing wrong in the encoding of the video because the same video plays fine in iTunes as well as on my iPod Touch in the regular videos playlist.

Could anyone help me out with this problem?

Thanks in advance


problem solved - for the benefit of those who are stuck on a similar issue, the solution is to explicitly create the frame for the view of the MPMoviePlayerController as follows:

I changed the lines:

[videoPlayer play];

[videoPlayer setFullscreen:YES];

[self.view addSubview:videoPlayer.view];

to the following:

[videoPlayer prepareToPlay];

[videoPlayer play];

[self.view addSubview:videoPlayer.view];

videoPlayer.view.frame = CGRectMake(0.0, 0.0, 480.0, 320.0); //this is explicitly added and solves the problem
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜