iphone - 错误消息: MPMoviePlayerController instance is already playing

标签 iphone cocoa-touch

在我的应用程序中,我使用标准 MPMoviePlayerController 类播放应用程序中的视频。

第一次这样做效果很好,但是在观看 1 个视频后,如果您尝试观看其他内容,应用程序会在 MPMoviePlayerController 的播放方法上崩溃并出现错误:

*** 由于未捕获的异常“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 instance is already playing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/661369/

相关文章:

iphone - NSPredicate 基于此对象是否与另一个对象相关?

iphone - 让 iPhone 应用程序与 OSX 应用程序通信

objective-c - 为什么 NSRegularExpression 不像 iOS 那样包含在 Mac 应用程序的 foundtion.framework 中?

ios - 每次启动时制作 ImageView

xcode - Interface Builder/跨平台中的自定义 UIView

ios - UICollectionView 单元格之间的距离不变

iphone - iOS 5 是否以不同方式处理低内存情况?

javascript - 在 iOS 上获取 YouTube 音频 URL

ios - 在获取实时广告的同时停止 mopub 的测试广告

ios - objective-c : how to execute a function when app is in the background/killed automatically based on time Intervals