iOS-MPMoviePlayerController 无法从远程 URL 播放视频

标签 ios objective-c iphone video mpmovieplayercontroller

我正在尝试从远程 URL 播放视频。但它不起作用。我的代码是:

- (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);
    }
}

我收到这条消息

Did finish with error: Error Domain=MediaPlayerErrorDomain Code=-11850 "Operation Stopped" UserInfo=0x15e77b20 {NSLocalizedDescription=Operation Stopped}

当我在浏览器中复制/粘贴我的视频 URL 时,它会成功下载(即使在浏览器中没有缓冲)并且下载完成后它在笔记本电脑上播放正常。我很沮丧,请帮助,任何建议都很好,提前致谢

最佳答案

如果视频未在设备上播放,则说明您的 HTTP 服务器不支持字节范围请求。

HTTP servers hosting media files for iOS must support byte-range requests, which iOS uses to perform random access in media playback. (Byte-range support is also known as content-range or partial-range support.) Most, but not all, HTTP 1.1 servers already support byte-range requests.

Source From Safari Web Service Content

Source From Byte-Range Request

Solution

关于iOS-MPMoviePlayerController 无法从远程 URL 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35622246/

相关文章:

ios - 使用 UILabel 和 UITextView 自动布局

iphone - Objective-C 中的私有(private)变量是否强大?

ios - 如何通过使用iPhone上的phonegap发送短信和短信?

objective-c - 显示 Storyboard 中 View 中的隐藏对象

iphone - 在 iPhone sdk 中搜索数组中的值

java - 从 Java 编程到 iPhone 应用程序的转变有多大?

iphone - 如何自定义 UIAlert View..?

ios - 应用程序终止时在 DidUpdateLocations 中进行网络调用

ios - 如何使用 CallKit 进行非 voip 调用?

ios - 在 TableView 单元格上添加 Segue 的正确方法