我正在尝试从远程网址播放视频。但它没有用。我的代码是:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"start playing");
NSURL *nsURL= [[NSURL alloc]initWithString:@"http://my_server_path:8080/content-service/v1/video/12/RGl1Nm1ib1VLMitFUmM5bEZzYVpxVGllM2RrWUw3Y09yMzdVZ0pzYlEvYjNaYUI5bEQvZERhTUhNTjBOaW5lY1hqYlZSRUM5anAvL3FsUTA2NzEwN2NMM2dnUnkxR0s4QUVyMnV2MlBLMjA9?thumb=false"];
videoPlayer = [[MPMoviePlayerController alloc]
initWithContentURL:nsURL];
[videoPlayer.view setFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
videoPlayer.controlStyle = MPMovieControlStyleDefault;
videoPlayer.movieSourceType = MPMovieSourceTypeStreaming;//MPMovieSourceTypeFile/MPMovieSourceTypeUnknown ;
[self.view addSubview:videoPlayer.view];
[videoPlayer prepareToPlay];
[videoPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
videoPlayer.shouldAutoplay = YES;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSError *error = [[notification userInfo] objectForKey:@"error"];
if (error) {
NSLog(@"Did finish with error: %@", error);
}
}
我收到此消息
完成错误:错误Domain = MediaPlayerErrorDomain Code = -11850“Operation Stopped”UserInfo = 0x15e77b20 {NSLocalizedDescription =操作已停止}
当我在浏览器中复制/粘贴我的视频URL时,它会成功下载(甚至不是浏览器中的缓冲区),完成下载后,它在笔记本电脑中播放正常。我很沮丧,请帮助,任何建议都会很棒,提前致谢
答案 0 :(得分:2)
如果视频未在设备上播放,则您的HTTP服务器不支持字节范围请求。
托管iOS媒体文件的HTTP服务器必须支持字节范围 请求,iOS用于在媒体播放中执行随机访问。 (字节范围支持也称为内容范围或部分范围 支持。)大多数(但不是全部)HTTP 1.1服务器已经支持 字节范围请求。
Source From Safari Web Service Content
答案 1 :(得分:0)
使用此代码:
其中fileURL是服务器上视频的网址
MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
if (tmpMoviePlayViewController) {
tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:tmpMoviePlayViewController animated:YES completion:nil];
[tmpMoviePlayViewController.moviePlayer prepareToPlay];
// tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[tmpMoviePlayViewController.moviePlayer play];
}
答案 2 :(得分:0)
使用fileUrl
替换此代码中的网址 MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] init];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[mpvc.moviePlayer setContentURL:fileURL];
[self presentMoviePlayerViewControllerAnimated:mpvc];