3

我有VideoController哪个播放视频。现在 VideoController 被推入navigationController

 VideoController *ObjVideoController = [[VideoController alloc] init];
 ObjVideoController.strVideoURL = [AnimationArr objectAtIndex:SequenceCount];
 [self.navigationController pushViewController:ObjVideoController animated:NO];
 [ObjVideoController play:[AnimationArr objectAtIndex:SequenceCount]];

Play method在 VideoController 中是这样的:

 - (void)play:(NSString*)videoFile {
    playbaktime = 0.0;

    NSBundle *main = [NSBundle mainBundle];
    NSURL *url = [NSURL fileURLWithPath:[[main resourcePath] stringByAppendingPathComponent:videoFile]];

    if (!self.ctl)
    {
        self.ctl = nil;
        self.ctl = [[MPMoviePlayerController alloc]init];
        [self.view addSubview:self.ctl.view];
    }
    [self.ctl prepareToPlay];
    self.ctl.contentURL = url;
    self.ctl.controlStyle = 0;
    //self.ctl.useApplicationAudioSession = NO;
    [self.ctl.view setUserInteractionEnabled:NO];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        [self.ctl.view setFrame:CGRectMake(0,0,480,320)];
    else
        [self.ctl.view setFrame:CGRectMake(0,0,1024,768)];

    self.ctl.backgroundView.backgroundColor = [UIColor whiteColor];
    [self.ctl play];
}

现在observer已添加为UIApplicationWillResignActive, UIApplicationDidBecomeActive。其选择器如下:

 -(void)Pause_Video:(NSNotification*)notification {
   Pausevideo = 1;
   playbaktime = self.ctl.currentPlaybackTime;
   [self.ctl pause];
 }

-(void)Play_Video:(NSNotification*)notification {

  if(self.ctl.loadState == MPMovieLoadStateUnknown)
  {
    [self play:self.strVideoURL];
    Pausevideo = 0;
  }
  else{
    if (self.ctl.playbackState == MPMoviePlaybackStatePaused) {
        [self.ctl play];
        Pausevideo = 0;
    } 
    else
    {
        [self.ctl setInitialPlaybackTime:playbaktime];
        [self.ctl play];
        Pausevideo = 0;
    }
   }  
  }

希望您理解问题和帮助将不胜感激。

4

1 回答 1

2

好像prepareToPlay每次播放前都在调用,这不是必须的,加载文件时调用一次即可。暂停后无需调用它。

此外,您会遇到延迟,因为通知不会立即触发。具体来说,UIApplicationDidBecomeActive只有在应用程序进入前台后才会触发。相反,您想要的是观察UIApplicationWillEnterForegroundNotification,它将在应用程序进入前台时触发,并允许您显着减少延迟。

于 2012-11-04T21:18:24.977 回答