#summary Plays movies but handle with care
#labels Bug, Movies
Playing videos in an iPhone application requires the usage of the MPMoviePlayerController. This class does require some special care when using it multiple times in a row. By default playing a second movie during a single application run will result in black screen with only sound being played. Other cases will result in the second movie being played with a flickering screen.
The second time a video plays you either see a black or flickering screen.
stop before release to prevent the black screeninitialPlaybackTime to -1 before release to prevent flickeringThe sample code below applies all the above in a small example.
- (void) playMovie {
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]
initWithURL:[NSURL URLWithString:@"somefilepath"]];
if (player) {
self.moviePlayer = player;
[player release];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// configure player
// [..]
[self.moviePlayer play];
}
}
- (void)moviePlayerDidFinish:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// to prevent a black screen on second play
[self.moviePlayer stop];
// to prevent flickering on second play
self.initialPlaybackTime = -1;
self.moviePlayer = nil;
}