MPMoviePlayerController sound only after app has become active
I am using MPMoviePlayerController
in my application and when I press the home button and then open the application again, the player continues to play, but only audio. Instead of video, the user sees a black screen.
This bug is present only on the device (iOS 5.0, possibly earlier versions). This is normal on a simulator.
Any ideas?
Code that instantiates MPMoviePlayerController
and adds it for viewing:
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 - link to mp4 of this form: http: //host.domain/filename.mp4
probably MPMoviePlayerController
doesn't work correctly with the mp4 source type on http?
Decision
All is well in this case:
- (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];
}
}
+3
source to share
1 answer
try this code:
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];
}
0
source to share