0

In my application I've to stream videos from server. For that I've used below Code

-(void)playingSong:(NSURL*) url{

   AVAsset *asset = [AVAsset assetWithURL:url];

   duration = asset.duration;

   playerItem = [AVPlayerItem playerItemWithAsset:asset];

   player = [AVPlayer playerWithPlayerItem:playerItem];

   [player play];
}

All are Global Variables

It's playing all videos when network is good, but unable to play videos with big size, when network is slow. Means It's not playing for big size videos and it's playing small videos; I'm using http Server not https;

for ex : 3min video it's playing but for 1hr video it's not. Why so?

SUDHAKAR RAYAPUDI
  • 549
  • 1
  • 9
  • 18

2 Answers2

2

Seems like you have to download the whole video before you can begin playback. It can also be because of your server not AVPlayer.

when you serve videos on a site using plain HTTP – known as progressive download – the position of the header becomes very important. Either the header is placed in the beginning of the file or it’s places in the end of the file. In case of the latter, you’ll have to download the whole thing before you can begin playback – because without the header, the player can’t start decoding.

Have look at this guide if your problem is because of videos source.

Have a look at this thread and change you implementation accordingly.

Community
  • 1
  • 1
Imran
  • 2,985
  • 19
  • 33
1

Download video in local and then play in avplayer.

DispatchQueue.global(qos: .background).async {

        do {

            let data = try Data(contentsOf: url)
            DispatchQueue.main.async {

//                    store "data" in document folder

                let fileUrl = URL(fileURLWithPath: <#localVideoURL#>)

                let asset = AVAsset(url: fileUrl)
                let item = AVPlayerItem(asset: asset)
                let player = AVPlayer(playerItem: item)

                let layer = AVPlayerLayer(player: player)
                layer.bounds = self.view.bounds
                self.view.layer.addSublayer(layer)

                player.play()
            }

        } catch {
            print(error.localizedDescription)
        }
    }
Mahendra
  • 8,448
  • 3
  • 33
  • 56