从应用程序背景到前景的视频播放在iOS 6.0中从暂停到播放需要2秒钟

时间:2012-10-25 09:36:24

标签: iphone objective-c ios xcode

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

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

Play method是这样的:

 - (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];
}

现在为observerUIApplicationWillResignActive添加了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;
    }
   }  
  }

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

1 个答案:

答案 0 :(得分:2)

在播放之前,您似乎每次都在调用prepareToPlay,这不是必需的,只需在加载文件时调用一次。暂停后无需调用它。

此外,由于通知不会立即触发,因此会产生延迟。具体来说,UIApplicationDidBecomeActive仅在应用进入前台后才会激活。你想要的是观察UIApplicationWillEnterForegroundNotification,它会在应用程序进入前景时触发,并允许你显着减少延迟。