MPMoviePlayerController在四秒后停止

Kur*_*urt 8 objective-c mpmovieplayercontroller ios

我正在尝试建立一个非常简单的视频播放器.(iOS 5.1,Xcode 4.3.1)

-(void)playMedia {
NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]];

[moviePlayer prepareToPlay];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.fullscreen = YES;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self.view addSubview: moviePlayer.view];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(playMediaFinished:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:moviePlayer];
[moviePlayer play];
}
Run Code Online (Sandbox Code Playgroud)

它在调用时工作正常,但它只播放四秒钟,然后出现黑屏.如果我在播放期间点按屏幕,它将播放整个序列.如果我停止点击屏幕四秒钟,则会出现黑屏.

我错过了什么?

库尔特


编辑版很好.

在接口文件中:

@property (nonatomic,strong) MPMoviePlayerController *myMovieController;
Run Code Online (Sandbox Code Playgroud)

在.m文件中:

-(void)playMedia {
NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]];

[moviePlayer prepareToPlay];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.fullscreen = YES;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

self.myMovieController = moviePlayer;
[self.view addSubview: self.myMovieController.view];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(playMediaFinished:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:moviePlayer];
[self.myMovieController play];
}
Run Code Online (Sandbox Code Playgroud)

dav*_*cis 19

如果你正在使用ARC,我相信你需要保留外部的moviePlayer.我自己就把它分配给了一个新的房产.HTH