在我的应用程序中,我正在使用标准MPMoviePlayerController类播放应用程序中的视频。
第一次解决此问题的效果很好,但是,如果您观看了1个视频,然后尝试观看其他内容,则该应用在MPMoviePlayerController的play方法上崩溃并出现错误:
***由于未捕获的异常'MPMoviePlayerControllerPlaybackException'而终止应用程序,原因:'MPMoviePlayerController实例已在播放'
我不知道为什么会这样。
我在另一个应用程序中有非常相似的代码,但没有收到此错误。
我正在为设备-2.0进行编译,并在带有固件2.2.1的iPhone上运行它。
这是我的代码:
@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];
}
甚至更奇怪的是,如果您查看代码,电影结束后,我会检查用户是否启用了自动重复。
如果有的话,我只需要重新开始看电影即可。
但是,如果他们没有启用自动重复并离开此类,然后尝试观看另一部电影(或同一部电影),则会导致崩溃。
有谁知道为什么会这样?
难道我做错了什么?
谢谢!
最佳答案
这是我的解决方案。我必须使用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];
}
关于iphone - 错误消息:MPMoviePlayerController实例已在播放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/661369/