Reputation: 4483
I'm trying to play a video from a remote URL using AVPlayer. It is a very simple setup: Load the view controller, and in the viewDidLoad, setup the AVPlayer with the url and play it. This is my code, in 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. It just isn't playing. You can test that urlString too, it is a valid url. The size of the file is 3.8 MB. I got the url from some test URLs here: 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. 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. 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.
what could I be doing wrong here?? any help is appreciated. thanks.
Upvotes: 3
Views: 8649
Reputation: 6112
Change this:
NSURL *url = [NSURL fileURLWithPath:urlString];
to this:
NSURL *url = [NSURL URLWithString:urlString];
fileURLWithPath
is used to load an url from the local file system. For a URL from the web you should use URLWithString
Upvotes: 11