简体   繁体   English

使用AVPlayer从远程URL播放视频

[英]playing video from remote URL with AVPlayer

I'm trying to play a video from a remote URL using AVPlayer. 我正在尝试使用AVPlayer从远程URL播放视频。 It is a very simple setup: Load the view controller, and in the viewDidLoad, setup the AVPlayer with the url and play it. 这是一个非常简单的设置:加载视图控制器,并在viewDidLoad中,使用url设置AVPlayer并播放它。 This is my code, in viewDidLoad: 这是我的代码,在viewDidLoad中:

NSString *urlString = @"http://download.wavetlan.com/SVV/Media/HTTP/MOV/ConvertedFiles/MediaConvert/MediaConvert_test4_1m10s_MPEG4SP_VBR_383kbps_320x240_30fps_AACLC_VBR_60kbps_Stereo_44100Hz.mov";
NSURL *url = [NSURL fileURLWithPath:urlString];
self.item = [AVPlayerItem playerItemWithURL:url];
self.player = [[AVPlayer alloc] initWithPlayerItem:self.item];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.item];
playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
playerLayer.backgroundColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:playerLayer];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.player play];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

But when the view loads, nothing happens. 但是当视图加载时,没有任何反应。 The screen is just green (because I set the playerLayer to be green), so I know it's there. 屏幕只是绿色(因为我将playerLayer设置为绿色),所以我知道它就在那里。 It just isn't playing. 它只是不玩。 You can test that urlString too, it is a valid url. 您也可以测试该urlString,它是一个有效的URL。 The size of the file is 3.8 MB. 文件大小为3.8 MB。 I got the url from some test URLs here: http://download.wavetlan.com/SVV/Media/HTTP/http-mov.htm 我从这里得到了一些测试网址的网址: http//download.wavetlan.com/SVV/Media/HTTP/http-mov.htm

So, I have initialized the player, the playerLayer, the item, and the URL is correct. 所以,我已经初始化了播放器,播放器层,项目,并且URL是正确的。 But nothing appear on screen. 但屏幕上没有任何内容。 One thing I noticed in the debugger is that the NSURL and the actual string of the url are different at the end. 我在调试器中注意到的一件事是NSURL和url的实际字符串在结尾处是不同的。 So maybe that could be of some help, but I'm not sure how to change that. 也许这可能会有所帮助,但我不知道如何改变它。 Here is a photo: 这是一张照片:

在此输入图像描述

Also, I'm testing this on an iOS 8.4 device, so I know it is not the app transport security that is blocking the HTTP address. 另外,我在iOS 8.4设备上测试它,所以我知道阻止HTTP地址的app传输安全性不是。

what could I be doing wrong here?? 我在这里可能做错了什么? any help is appreciated. 任何帮助表示赞赏。 thanks. 谢谢。

Change this: 改变这个:
NSURL *url = [NSURL fileURLWithPath:urlString];
to this: 对此:
NSURL *url = [NSURL URLWithString:urlString];

fileURLWithPath is genrally used to load url from the local file system while you are using url from the web you should use URLWithString fileURLWithPath通常用于从本地文件系统加载url,当你在网上使用URL时你应该使用URLWithString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM