3

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()
Murat Kaya
  • 1,281
  • 3
  • 28
  • 52
  • 1
    `AVPlayer not playing video` Just not playing? Or is there an error message? What are the symptoms? – Eric Aya Jun 02 '16 at 14:01
  • just not playing video. – Murat Kaya Jun 02 '16 at 14:07
  • is there a black blank screen?? – Harris Jun 02 '16 at 14:16
  • 2
    While playing remote files, you are supposed to make use of KVO to get notified for changes in player status. You should then be able to tell whether the playback has failed or not. – Xcoder Jun 02 '16 at 14:31
  • See this question for more on what Xcoder is talking about http://stackoverflow.com/questions/5401437/knowing-when-avplayer-object-is-ready-to-play – naomimichiko Jun 02 '16 at 17:13
  • actually I used kvo to get notified. Its solved my half of my problem.But when internet is really slow I'm trying to play video in every condition. So what suggest to me ? – Murat Kaya Jun 04 '16 at 21:58

4 Answers4

3

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

0

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()
    }
mitch10e
  • 96
  • 8
0

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)

Wasif Ali
  • 886
  • 1
  • 13
  • 29
x23b5
  • 649
  • 5
  • 14
-1

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];
}];
Vinay Kini
  • 41
  • 1
  • The question is tagged "swift", *not* "objective-c". Please replace your Objective-C example with a Swift example. – Eric Aya Jan 25 '17 at 09:43