我正在制作一个正在尝试播放视频的应用程序。视频正常启动,但视频屏幕在4秒后变为黑色。我不知道问题出在哪里。
另外,当我设置player.movieplayer.shouldautoplay = NO时,此行没有任何效果,视频会自动开始。
以下是代码:
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSURL *urlObj = [NSURL fileURLWithPath:urlString];
UIGraphicsBeginImageContext(CGSizeMake(1,1));
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlObj];
UIGraphicsEndImageContext();
[player.view setBounds:self.view.bounds];
// when playing from server source type shoud be MPMovieSourceTypeStreaming
[player.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming];
[player.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
player.moviePlayer.shouldAutoplay = NO;
[self.view addSubview:player.view];
[player.moviePlayer play];
我是不是漏掉了什么?
我试图获取视频的总时长(使用mpmovieplayercontroller的duration属性),但显示为0.0。如何获取视频时长??
发布于 2013-04-12 17:50:16
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSURL *urlObj = [NSURL fileURLWithPath:urlString];
UIGraphicsBeginImageContext(CGSizeMake(1,1));
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlObj];
UIGraphicsEndImageContext();
[player.view setBounds:self.view.bounds];
// when playing from server source type shoud be MPMovieSourceTypeStreaming
[player.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming]; // I was missing this line therefore video was not playing
[player.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[self.view addSubview:player.view];
[player.moviePlayer play];
发布于 2013-04-14 21:10:07
这里有几个问题:
MPMoviePlayerController
,而不是MPMoviePlayerViewController
。如果您希望拥有一个可以使用presentMoviePlayerViewControllerAnimated:
.呈现的自包含视图控制器,请使用MPMoviePlayerViewController
有关这方面的完整示例,请参阅Till的优秀answer to a similar question.
UIGraphicsBeginImageContext
和UIGraphicsEndImageContext
调用的目的是什么,但我看不出这里需要它们。至于shouldAutoplay = NO
,视频仍然在开始,因为您随后立即调用了play
。
播放器的duration
属性仅在接收到MPMovieDurationAvailableNotification
后才包含有用的值。您需要执行类似以下内容的操作才能访问实际持续时间:
__weak MediaPlayerController *weakSelf = self;
[[NSNotificationCenter defaultCenter] addObserverForName:MPMovieDurationAvailableNotification object:self.player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
NSLog(@"Movie duration: %lf", weakSelf.player.duration);
}];
完成后,使用removeObserver:name:object:
删除观察者。
https://stackoverflow.com/questions/15968020
复制相似问题