我正在尝试使用MPMoviePlayerController显示视频,但我无法让它工作。 我的视频在我的服务器上。所以我试着用这种方式显示它:
-(void) playMovieAtURL: (NSURL*) theURL {
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.controlStyle = MPMovieControlModeDefault;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.fullscreen = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
moviePlayer是我的viewController的一个属性。 给出的URL是正确的(在safari中测试)。 我的视频是.mov视频。
调用我的函数时,我只是一个黑屏。 控件没有出现,我认为视频没有加载,因为在应用程序的状态栏中,不显示网络活动指示。
欢迎任何帮助。
修改
我刚刚注意到,尝试通过设备的Safari应用程序访问视频会导致:
编辑2
我的视频是480×850,而MPMoviePlayerController仅支持高达640 x 480的视频。 所以它不能用这个框架来玩。 我是否必须调整视频大小,还是有其他框架可以用来显示它? 我只需要一个非常简单的球员......
答案 0 :(得分:1)
这样做:
-(void)playMovieAtURL:: (NSURL*) url
{
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
[self.view bringSubviewToFront:moviePlayer.view];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
答案 1 :(得分:1)
//你可以这样做
-(void)playMovie:(id)sender
{
NSURL *url = [NSURL URLWithString:
@"http://www.xxxx.com/movie.mov"];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
//目标行动通知方法
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}