Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching streaming URLs (HLS) #25

Open
xvicient21 opened this issue Jul 18, 2019 · 2 comments
Open

Caching streaming URLs (HLS) #25

xvicient21 opened this issue Jul 18, 2019 · 2 comments

Comments

@xvicient21
Copy link

xvicient21 commented Jul 18, 2019

I have a problem trying with streaming videos like this: https://stream.mux.com/E41fV5QQeq2zzL54A1rfXoVzw00Rmlc3B.m3u8
I can see the video is downloaded incorrectly because it's calling so fast the delegate
func playerItem(_ playerItem: CachingPlayerItem, didFinishDownloadingData data: Data)
and avplayer is playing nothing.

final class DemoView: UIViewController {
    
    var player: AVPlayer!
    var playerLayer: AVPlayerLayer!
    var playerItem: CachingPlayerItem!
    @IBOutlet weak var playerView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        downloadURL()
        addPlayerAndPlay()
    }
    
    func addPlayerAndPlay() {
        player = AVPlayer(playerItem: playerItem)
        player.automaticallyWaitsToMinimizeStalling = false
        playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = .resizeAspectFill
        playerLayer.frame = playerView.bounds
        playerView.layer.addSublayer(playerLayer)
        player.play()
    }
    
    func downloadURL() {
        let url = URL(string: "https://stream.mux.com/fmOV64ZNi800fgfpLa019zhnLK4whkZvz02.m3u8")!
        playerItem = CachingPlayerItem(url: url)
        playerItem.delegate = self
        playerItem.download()
    }
}

extension DemoView: CachingPlayerItemDelegate {
    
    func playerItem(_ playerItem: CachingPlayerItem, didFinishDownloadingData data: Data) {
        print("File is downloaded and ready for storing")
    }
    
    func playerItem(_ playerItem: CachingPlayerItem, didDownloadBytesSoFar bytesDownloaded: Int, outOf bytesExpected: Int) {
        print("\(bytesDownloaded)/\(bytesExpected)")
    }
    
    func playerItemPlaybackStalled(_ playerItem: CachingPlayerItem) {
        print("Not enough data for playback. Probably because of the poor network. Wait a bit and try to play later.")
    }
    
    func playerItem(_ playerItem: CachingPlayerItem, downloadingFailedWith error: Error) {
        print(error)
    }
    
}

Any idea why it's not working with HLS videos?

@JoeyBodnar
Copy link

Anyone ever figure this out?

@kstefanou52
Copy link

I know I'm late to the party, but I just figured out what was wrong. I created a pull request that fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants