Is it possible to cache HLS segments with AVPlayer?

Let’s start with really good news – iOS 10 and up – gives this out of the box. No more need for hacks soon. More details can be found in the following WWDC16 session about whats new in HTTP Live Streaming: https://developer.apple.com/videos/play/wwdc2016/504/ Now back to the current state of things – iOS 9 and lower: … Read more

Hide controls in AVPlayerViewController — only at start

UPDATE: I ended up making my own controls for better customization. It’s more difficult but worth the time. Please read Apple’s sample code for reference. It’s about implementing PiP but also about making custom controls: https://developer.apple.com/library/prerelease/ios/samplecode/AVFoundationPiPPlayer/Introduction/Intro.html UPDATE: When tapped, AVPlayerViewController only fires touchesBegan event, and not touchesEnded event. But it’s enough to show the controls. … Read more

How to track when song finished playing using AVPlayer in Swift

Something like this works: func play(url: NSURL) { let item = AVPlayerItem(URL: url) NSNotificationCenter.defaultCenter().addObserver(self, selector: “playerDidFinishPlaying:”, name: AVPlayerItemDidPlayToEndTimeNotification, object: item) let player = AVPlayer(playerItem: item) player.play() } func playerDidFinishPlaying(note: NSNotification) { // Your code here } Don’t forget to remove the observer when you’re done (or in deinit)!

Swift: AVPlayer playing video is showing this error: [AVOutputContext] WARNING: AVF context unavailable for sharedAudioPresentationContext

I had similar issue with mp4 file stored on device which got fixed by prepending “file://” to the file path guard let strPath = Bundle.main.path(forResource: “demo”, ofType: “mp4”), let url = URL(string: “file://\(strPath)”) else { print(“Umm, looks like an invalid URL!”) return } Inspired by this post

AVPlayer streaming progress

I am just working on this, and so far have the following: – (NSTimeInterval) availableDuration; { NSArray *loadedTimeRanges = [[self.player currentItem] loadedTimeRanges]; CMTimeRange timeRange = [[loadedTimeRanges objectAtIndex:0] CMTimeRangeValue]; Float64 startSeconds = CMTimeGetSeconds(timeRange.start); Float64 durationSeconds = CMTimeGetSeconds(timeRange.duration); NSTimeInterval result = startSeconds + durationSeconds; return result; }

AVPlayer, notification for play/pause state?

Why do you say that “rate” is not KVO complaint? It works for me. Here is what I did: – (void)viewDidLoad { … [self.player addObserver:self forKeyPath:@”rate” options:0 context:nil]; } And then: – (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@”rate”]) { if ([self.player rate]) { [self changeToPause]; // This changes the button … Read more

AVPlayer seekToTime does not play at correct position

int32_t timeScale = self.player.currentItem.asset.duration.timescale; CMTime time = CMTimeMakeWithSeconds(77.000000, timeScale); [self.player seekToTime:time toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero]; I had a problem with ‘seekToTime’. I solved my problem with this code. ‘timescale’ is trick for this problem. Swift version: let playerTimescale = self.player.currentItem?.asset.duration.timescale ?? 1 let time = CMTime(seconds: 77.000000, preferredTimescale: playerTimescale) self.player.seek(to: time, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) { (finished) … Read more