MPMoviePlayerController - 当我更改contentURL时视频停止

时间:2012-03-05 11:30:59

标签: iphone xcode cocoa-touch mpmovieplayercontroller mpmovieplayer

正在播放电影时,如果我更改了我的contentURL,则电影停止播放并删除视图。

我已经尝试过解决方案here,但它仍无效。

这是我播放电影的代码:

- (void) playMovie:(NSURL *)moviePath{
theMovie = [[MPMoviePlayerController alloc] initWithContentURL: moviePath];

UIView * movieView = [theMovie view];

[movieView setFrame: CGRectMake(0, 0, 480, 320)];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[movieView setTransform: landscapeTransform];

self.theMovie.scalingMode = MPMovieScalingModeAspectFit;
self.theMovie.fullscreen = TRUE;
self.theMovie.controlStyle = MPMovieControlStyleNone;
[self performSelector:@selector(showCtrlsOnTouch) withObject:NULL afterDelay:0.1];
[self.theMovie prepareToPlay];

int a = currentMovie;
int b = [self.tableDataSource count] - 1;

if(a < b)
{
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    nextButton.frame = CGRectMake(360.0, 138.0, 100.0, 44.0);
    [nextButton setTitle:@"Next" forState:UIControlStateNormal];
    [self.theMovie.view addSubview:nextButton];
    [nextButton addTarget:self action:@selector(playNextMovie:) forControlEvents:UIControlEventTouchUpInside];
}
if(currentMovie > 0)
{
    UIButton *previousButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    previousButton.frame = CGRectMake(40.0, 138.0, 100.0, 44.0);
    [previousButton setTitle:@"Prev" forState:UIControlStateNormal];
    [self.theMovie.view addSubview:previousButton];
    [previousButton addTarget:self action:@selector(playPreviousMovie:) forControlEvents:UIControlEventTouchUpInside];
}

[[[UIApplication sharedApplication] keyWindow] addSubview: movieView];

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(movieFinishedCallback:)
                                             name: MPMoviePlayerPlaybackDidFinishNotification
                                           object: self.theMovie];}

这是我切换电影的代码:

- (void) playNextMovie:(id) sender{
currentMovie++;

NSDictionary *dictionary = [self.tableDataSource objectAtIndex:currentMovie];

NSString *videoString = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"videoFile"]];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:videoString ofType:@"m4v"];
NSLog(@"Video Path: %@", videoPath);
NSURL *moviePath = [NSURL fileURLWithPath: videoPath];

self.theMovie.contentURL = moviePath;
[self.theMovie prepareToPlay];}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

一旦提供新contentURL,玩家停止游戏是完全正常的。

MPMoviePlayerController.view通常不会自动删除。我假设你忘了你的一个通知处理程序为你做了类似的事情。