我正在尝试设置一个非常简单的视频播放器。 (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];
}
调用时效果很好,但只播放4秒钟,然后出现黑屏。如果我在播放期间点按屏幕,它将播放整个序列。如果我停止点击屏幕四秒钟,则会出现黑屏。
我缺少什么?
库尔特
编辑版本播放正常。
在界面文件中:
@property (nonatomic,strong) MPMoviePlayerController *myMovieController;
在.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];
}
答案 0 :(得分:19)
如果你正在使用ARC我相信你需要保留外部的moviePlayer。我自己就把它分配给了一个新的房产。 HTH
答案 1 :(得分:4)
解决方案是播放器必须是视图控制器的实例变量或属性。 即我们必须使用MPMoviePlayerController的实例
@property(非原子,强)MPMoviePlayerController * myMovieController;