cohenadair
cohenadair

Reputation: 2072

iOS AVPlayer not playing all remote files

I'm using SwiftyDropbox's getTemporaryLink() to play a video in an AVPlayer. I have six test files, and they all work as expected, except 1.

The one that doesn't work is 41 MB in size (which I would not consider a large video file), the rest are < 22 MB.

I've read the AVFoundation and SwiftDropbox documentation many times and haven't been able to find anything on a maximum file size, though I wouldn't expect a maximum file size for streaming content. I would expect it to continuously play smaller chunks downloaded to memory.

My questions are:

  1. Is there a file size limit on playing a remote URL in an AVPlayer?
  2. If not, is there a certain way I need to use AVPlayer in order to stream these larger files?

I'm using the following code to start the AVPlayer:

self.previewPlayer.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: url)))
self.previewPlayer.play()

Thank-you!

Upvotes: 0

Views: 1443

Answers (1)

Adeel Miraj
Adeel Miraj

Reputation: 2496

You should observe the value of status property to know why the playerItem might have failed to play. Here's a small code snippet to start with:

  1. Add observer

    let url = URL.init(string: "your url string")
    let item = AVPlayerItem.init(url: url!)
    item.addObserver(self,
                     forKeyPath: "status",
                     options: .new,
                     context: nil)
    
  2. Check for the the error

    override func observeValue(forKeyPath keyPath: String?,
                               of object: Any?,
                               change: [NSKeyValueChangeKey : Any]?,
                               context: UnsafeMutableRawPointer?) {
        if let item = object as? AVPlayerItem, keyPath == "status" {
            if item.status == .failed {
                print(item.error?.localizedDescription ?? "Unknown error")
            }
        }
    }
    

Upvotes: 0

Related Questions