草庐IT

objective-c - MPMoviePlayerController 在四秒后停止

coder 2023-09-20 原文

我正在尝试设置一个非常简单的视频播放器。 (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];
}

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

我错过了什么?

库尔特


编辑后的版本运行良好。

在接口(interface)文件中:

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

最佳答案

如果您使用的是 ARC,我相信您需要保留外部的 moviePlayer。我只是自己将它分配给了一个新属性。

关于objective-c - MPMoviePlayerController 在四秒后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780018/

有关objective-c - MPMoviePlayerController 在四秒后停止的更多相关文章

随机推荐