仅在应用程序变为活动状态后,MPMoviePlayerController音频

时间:2012-03-11 08:43:30

标签: objective-c ios video mpmovieplayercontroller

我在我的应用程序中使用MPMoviePlayerController,当我点击主页按钮然后再次打开应用程序时,播放器继续播放,但只播放音频。用户看不到视频,而是看到黑屏。

此错误仅在设备上(iOS 5.0,也许是早期版本)。在模拟器上没关系。

有什么想法吗?

创建MPMoviePlayerController实例并将其添加到视图的代码:

if (!self.moviePlayerController) {
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    if (player) 
    {
        /* Save the movie object. */
        [self setMoviePlayerController:player];

        /* Register the current object as an observer for the movie
         notifications. */
        [self installMovieNotificationObservers];

        player.shouldAutoplay = YES;
        player.controlStyle = MPMovieControlStyleNone;
        player.scalingMode = MPMovieScalingModeAspectFit;
        player.useApplicationAudioSession = NO;
        [player setContentURL:movieURL];
        [player prepareToPlay];
        if ([player respondsToSelector:@selector(setAllowsAirPlay:)]) {
            [player setAllowsAirPlay:YES];
        }
        [player setMovieSourceType:sourceType];
        [[player view] setFrame:[self.view bounds]];

        [self.view addSubview: [player view]];
        [self.view sendSubviewToBack:[player view]];
        [player release];
    }
}

movieURL - 链接到此表单的mp4:http://host.domain/filename.mp4 也许MPMoviePlayerController无法正确使用mp4源类型而不是http?

解决方案

在这种情况下,一切都很好:

- (void)applicationWillResignActiveNotification:(NSNotification*)notification {    
    if (self.moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) {
        self.playerState = PlayerStatePlaying;
    } else if (self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused) {
        self.playerState = PlayerStatePaused;
    self.playbackTimeBeforeBackground = self.moviePlayerController.currentPlaybackTime;
    [self.moviePlayerController pause];
}

- (void)applicationDidBecomeActiveNotification:(NSNotification*)notification {
self.moviePlayerController.currentPlaybackTime = -1;    
    if (self.playerState & PlayerStatePlaying) {
        [self performSelector:@selector(startPlayerAfterDelayPause:) withObject:nil afterDelay:0.2];
    } else if (self.playerState & PlayerStatePaused) {
        [self performSelector:@selector(startPlayerAfterDelayPause:) withObject:[NSNumber numberWithBool:YES] afterDelay:0.2];
    }
}

- (void)startPlayerAfterDelayPause:(NSNumber *)isPause{
    self.moviePlayerController.currentPlaybackTime = self.playbackTimeBeforeBackground;
    if ([isPause boolValue] == YES) {
        [self.moviePlayerController pause];
    } else {
        [self.moviePlayerController play];
    }
}

1 个答案:

答案 0 :(得分:0)

你试试这段代码:

NSString *path=[bundle pathForResource:@"ImageHomeAnimation" ofType:@"mp4"];

MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];

 self.player1= mp;//[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
    [mp release];
    self.player1.view.frame = CGRectMake(0, -20, 320,480);
    self.player1.view.backgroundColor=[UIColor clearColor];
   [self.view addSubview:self.player1.view];
    self.player1.fullscreen=YES;
   [self.player1 setRepeatMode:MPMovieRepeatModeNone];
   self.player1.controlStyle=MPMovieControlStyleNone;
   [[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(movieFinishedCallback)  name:MPMoviePlayerPlaybackDidFinishNotification
 object:self.player1];
   [self.player1 prepareToPlay];
   [self.player1 play];

-(void)movieFinishedCallback
{

[self.player1.view removeFromSuperview];

}