Iphone 错误消息:MPMoviePlayerController实例已在播放
在我的应用程序中,我使用标准的MPMoviePlayerController类播放应用程序中的视频 第一次这样做效果很好,但是在观看1个视频后,如果您尝试观看其他内容,应用程序会在MPMoviePlayerController的播放方法上崩溃,并出现以下错误: ***由于未捕获异常“MPMoviePlayerController播放异常”而终止应用程序,原因:“MPMoviePlayerController实例已在播放” 我不明白为什么会这样 我在另一个应用程序中有非常相似的代码,我没有得到这个错误 我正在为设备-2.0进行编译,并在固件为2.2.1的iPhone上运行它 这是我的代码:Iphone 错误消息:MPMoviePlayerController实例已在播放,iphone,cocoa-touch,Iphone,Cocoa Touch,在我的应用程序中,我使用标准的MPMoviePlayerController类播放应用程序中的视频 第一次这样做效果很好,但是在观看1个视频后,如果您尝试观看其他内容,应用程序会在MPMoviePlayerController的播放方法上崩溃,并出现以下错误: ***由于未捕获异常“MPMoviePlayerController播放异常”而终止应用程序,原因:“MPMoviePlayerController实例已在播放” 我不明白为什么会这样 我在另一个应用程序中有非常相似的代码,我没有得到这个
@synthesize movieURL;
- (void) setMovieAndPlay
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
moviePath = [[[paths lastObject] stringByAppendingPathComponent:movieURL] retain];
[self playVideoWithURL:[NSURL fileURLWithPath:moviePath]];
}
-(void)playMovieAtURL:(NSURL*)theURL
{
MPMoviePlayerController *mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
mMoviePlayer.scalingMode = MPMovieScalingModeAspectFill;
if ([defaults boolForKey:@"disableControls"])
{
mMoviePlayer.movieControlMode = MPMovieControlModeHidden;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mMoviePlayer];
[mMoviePlayer play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie release];
theMovie = nil;
NSDictionary *notiUserInfo = [notification userInfo];
if (notiUserInfo != nil)
{
NSError *errorInfo = [notiUserInfo objectForKey:@"error"];
if ([[errorInfo domain] isEqualToString:@"MediaPlayerErrorDomain"])
{
UIAlertView *notice = [[UIAlertView alloc] initWithTitle:@"Error" message:[errorInfo localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[notice show];
[notice release];
return;
}
}
if ([defaults boolForKey:@"autoRepeat"])
{
[self playMovieAtURL:[NSURL fileURLWithPath:moviePath]];
}
else
{
KFAppDelegate *appDelegate = (KFAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate endMovie];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
KFAppDelegate *appDelegate = (KFAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate endMovie];
}
更奇怪的是,如果您查看代码,在电影结束后,我会检查用户是否启用了自动重复
如果他们有,我只是重新开始电影,这是有效的
但是,如果他们没有启用自动重复并离开该类,然后尝试观看另一部电影(或同一部),则会导致崩溃
有人知道为什么会这样吗
我做错什么了吗
谢谢 只有当您的MPMoviePlayerController对象尚未释放并且您正在尝试播放另一部电影时,才会出现此错误 创建MPMoviePlayerController的实例变量,并在整个代码中使用它。所以不是
theMovie = [notification object];
[theMovie release];
theMovie = nil;
使用实例变量,并在PlayMovieAttribute和playbackDidFinish方法中使用该变量。我的问题是电影播放器对象没有被释放,因此我无法看到下一个视频,只能听到音频播放
希望这会有所帮助。这是我的解决方案。我必须使用NSTimer让下一次播放等待1秒,否则我会得到每个人都在谈论的唯一音频问题。这是针对2.2.1的
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(playTrack) userInfo:nil repeats:NO];
}
- (void) playTrack {
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer release];
NSURL *nsUrl = [NSURL URLWithString:track.url];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:nsUrl];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer play];
}
我认为
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
// you should add this:
if (mMoviePlayer != nil) {
// free the old movie player
NSLog(@"releasing!");
[mMoviePlayer release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
这似乎已在较新的iOS版本中得到修复。如您所见,我正在这样做:MPMoviePlayerController*theMovie=[notification object];[电影发行];theMovie=零;这并不是说我只能听到音频,而是我的应用程序完全崩溃了。谢谢你。还有其他想法吗?我还尝试了一个在playbackDidFinish中发布的实例var,得到了相同的结果。在这种情况下,你能在调用Playmovieturl方法的地方发布代码吗?如果你在[mMoviePlayer play]方法调用之后编写一个发布语句会发生什么?