代码之家  ›  专栏  ›  技术社区  ›  kdbdallas

错误消息:MPMoviePlayerController实例已在播放

  •  2
  • kdbdallas  · 技术社区  · 17 年前

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

    ***由于未捕获异常“MPMoviePlayerController播放异常”而终止应用程序,原因:“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];
    }
    

    更奇怪的是,如果您查看代码,在电影结束后,我会检查用户是否启用了自动重复。

    如果他们有,我只是重新开始电影,这是有效的。

    但是,如果他们没有启用自动重复并离开该类,然后尝试观看另一部电影(或同一部),则会导致崩溃。

    有人知道为什么会这样吗?

    我做错什么了吗?

    谢谢

    4 回复  |  直到 17 年前
        1
  •  2
  •   Toonsy Net Toonsy Net    17 年前

    这是我的解决办法。我必须使用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];
    }
    
        2
  •  1
  •   sth    16 年前

    - (void) moviePlayBackDidFinish:(NSNotification*)notification
    {
        // you should add this:
        if (mMoviePlayer != nil) {
            // free the old movie player
            NSLog(@"releasing!");
            [mMoviePlayer release];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
        }
    }
    
        3
  •  0
  •   lostInTransit    17 年前

    创建MPMoviePlayerController的实例变量,并在整个代码中使用它。所以不是

    theMovie = [notification object];
    [theMovie release];
    theMovie = nil;
    

    使用实例变量,并在PlayMovieAttribute和playbackDidFinish方法中使用该变量。我的问题是电影播放器对象没有被释放,因此我无法看到下一个视频,只能听到音频播放。

    希望这会有帮助。

        4
  •  0
  •   kdbdallas    14 年前

    这似乎已在较新的iOS版本中修复。