Murat Kaya
Murat Kaya

Reputation: 1321

Avplayer wont playing video from url

AVPlayer not playing video when I'm trying to play it from url. But when I try download and play video its playing.What I'm doing wrong ?

self.avAsset = AVAsset(URL: NSURL(string: contentURLString)!)
 let item = AVPlayerItem(asset: avAsset)
            avPlayer = AVPlayer(playerItem: item)
            playerLayer = AVPlayerLayer(player: avPlayer)
            playerLayer.frame = self.view.frame
            playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
            cell.videoView.layer.addSublayer(playerLayer)
            self.avPlayer.seekToTime(kCMTimeZero)
            avPlayer.play()

Upvotes: 3

Views: 7271

Answers (4)

x23b5
x23b5

Reputation: 709

I had the same symptoms, I tried playing a sample video from URL.

However I got an empty Player view, but the video was not playing. There was no error message when testing on a device, but when testing on simulator I got the following message:

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

So the problem was basically the HTTP-Protocol.

Solution A: Use a video source with https.

Solution B: Change the App Transport Security policies in your info-file (as described in this topic)

Upvotes: 0

Neven Grubisic
Neven Grubisic

Reputation: 51

AVplayer won't play remote url video unless server support Http Range: parameter. Otherwise show blank black screen

Upvotes: 4

mitch10e
mitch10e

Reputation: 96

Swift 3.0 Translation of Objective-C Answer

    let videoURL: URL = URL(string: contentURLString)!

    let playerViewController = AVPlayerViewController()
    self.playerController = playerViewController
    self.playerController.player = AVPlayer(url: videoURL)
    self.present(self.playerController, animated: true) { 
        self.playerController.player?.play()
    }

Upvotes: 0

Vinay Kini
Vinay Kini

Reputation: 61

For using AVPlayer with remote file, create a sample project and add the following lines in the viewDidLoad() of the ViewController.

NSURL *videoURL = [NSURL URLWithString:contentURLString];

//Use AVPlayerViewController to use default Apple Controls
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
self.playerController = playerViewController;
self.playerController.player = [AVPlayer playerWithURL:videoURL];
[self presentViewController:self.playerController animated:YES completion:^{
     //Start Playback
     [self.playerController.player play];
}];

Upvotes: -1

Related Questions