我有可以播放视频的VideoController。现在在navigationController中推送了VideoController。
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];
}现在已经为UIApplicationWillResignActive、UIApplicationDidBecomeActive添加了observer。它的选择器如下所示:
-(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;
}
}
}希望您理解的问题和帮助将被感谢。
发布于 2012-11-05 05:18:25
你似乎每次播放前都会调用prepareToPlay,这不是必需的,只需在加载文件时调用一次即可。没有必要在暂停后调用它。
此外,您还会遇到延迟,因为通知不会立即触发。具体地说,UIApplicationDidBecomeActive只有在应用程序进入前台后才会触发。相反,您需要的是观察UIApplicationWillEnterForegroundNotification,它将在应用程序进入前台时触发,并允许您显著减少延迟。
https://stackoverflow.com/questions/13065680
复制相似问题