MPMoviePlayerController
停止播放,
这是我正在使用的代码:
NSString *urlAddress = @"http://67.159.28.74:8730";
NSURL *url = [NSURL URLWithString:urlAddress];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
player.movieSourceType = MPMovieSourceTypeStreaming;
player.view.hidden = NO;
[self.view addSubview:player.view];
[player prepareToPlay];
if(player){
[player play];
}
进入控制台的错误是:
2012-09-23 18:07:56.618 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-09-23 18:07:56.619 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay
2012-09-23 18:07:56.638 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-09-23 18:07:56.638 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay
2012-09-23 18:07:56.643 Reader[696:c07] [MPAVController] Autoplay: Enabling autoplay
2012-09-23 18:07:56.645 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-09-23 18:07:56.646 Reader[696:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-09-23 18:07:56.648 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-09-23 18:07:56.648 Reader[696:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-09-23 18:07:56.650 Reader[696:c07] [MPAVController] Autoplay: Enabling autoplay
2012-09-23 18:07:56.652 Reader[696:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2012-09-23 18:07:58.746 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-09-23 18:07:58.746 Reader[696:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-09-23 18:07:58.747 Reader[696:c07] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1
2012-09-23 18:07:58.748 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 1
2012-09-23 18:07:58.748 Reader[696:c07] [MPAVController] Autoplay: Enabling autoplay`
答案 0 :(得分:9)
解决了它:
我将MPMoviePlayerController转换为属性:
//添加标题
@property (strong,nonatomic) MPMoviePlayerController *myPlayer;
//添加.m文件
- (IBAction)playStream:(id)sender {
NSString *urlAddress = @"http://67.159.28.74:8730";
NSURL *url = [NSURL URLWithString:urlAddress];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
[player prepareToPlay];
//Remember you have to add MediaPlayer.framework to your project
/*[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];*/
player.movieSourceType = MPMovieSourceTypeStreaming;
player.view.hidden = YES;
self.myPlayer = player;
[self.view addSubview:self.myPlayer.view];
if(player){
[self.myPlayer play];
}
}
答案 1 :(得分:8)
你必须像这样设置一个类变量:
@property (nonatomic, retain) MPMoviePlayerController *moviePlayer;
我的代码如下所示:
NSURL *theUrl = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theUrl];
[moviePlayer prepareToPlay];
[moviePlayer.view setFrame: self.view.bounds];
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self.view addSubview: moviePlayer.view];
[moviePlayer play];
这对我来说非常合适!
答案 2 :(得分:2)
在xCode 4.6.1上运行正常。
但是xCode 5出现了这个问题!
但是这个解决方案非常有用:
“解决方案是播放器必须是视图控制器的实例变量或属性。即我们必须使用MPMoviePlayerController的实例
@property(非原子,强)MPMoviePlayerController * myMovieController;“
非常感谢作者!
这是我的代码:
UIGraphicsBeginImageContext(CGSizeMake(1,1));
NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
self.moviePlayerView = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.moviePlayerView prepareToPlay];
[self.moviePlayerView.view setFrame:CGRectMake(0, 0, 320, self.view.frame.size.height/2)];
self.moviePlayerView.view.backgroundColor = [UIColor clearColor];
[self.moviePlayerView setContentURL:videoURL];
self.moviePlayerView.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayerView.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview:self.moviePlayerView.view];
self.moviePlayerView.shouldAutoplay = NO;
[self.moviePlayerView play];
UIGraphicsEndImageContext();
答案 3 :(得分:1)
您可以在实例化播放器后尝试执行此操作:
[player prepareToPlay]
此处有更多信息:iOS 6 streaming player com.apple.coremedia.networkbuffering bug
答案 4 :(得分:1)
试试这个在ios6中适合我的工作
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"ddd" ofType:@"mp4"];
NSLog(@"%@",resourcePath);
NSURL *url = [NSURL fileURLWithPath:resourcePath];
NSLog(@"%@",url);
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayer.view.frame = self.view.frame;
moviePlayer.moviePlayer.shouldAutoplay=YES;
moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer play];
答案 5 :(得分:0)
这是一段你不想要的有趣代码。
根据Apple的文件
“当您将电影播放器的视图添加到应用的视图层次结构时,请务必正确调整框架大小,如下所示:http://tinyurl.com/9wgv9wc
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
答案 6 :(得分:0)
解决方案是播放器必须是视图控制器的实例变量或属性。 即我们必须使用MPMoviePlayerController的实例
@property(非原子,强)MPMoviePlayerController * myMovieController;