AVPlayer does not start playback BufferEmpty, but does not play

I use AVPlayer to play audio live over the Internet. I like to restore playback if it has been paused for more than 1 minute.

I call player.rate = 1.0 to resume. However, if a stream has been suspended for> 1 minute, it will no longer play. I need to recreate AVPlayerItem in this case so that it works again.

So, how can I catch this case, so I know that playback has not recovered?

  • I tried KVO on player.rate . It stays at 1.0 . The player is not playing!
  • I tried KVO on currentItem.playbackBufferEmpty . In this case, it is not called.
  • currentItem.status does not switch to .Failed . It does not change at all.

In this case, AVPlayer just does nothing. Any ideas?

I am creating a site code to demonstrate the problem:

 import UIKit import AVFoundation // keep it running forever so it plays audio import XCPlayground XCPSetExecutionShouldContinueIndefinitely(true) class AVPlayerTest { let player = AVPlayer() let streamurl = NSURL(string: "http://detektor.fm/stream/mp3/musik/")! func startTest() { let item = AVPlayerItem(URL: streamurl) player.replaceCurrentItemWithPlayerItem(item) player.play() // give it some start time to build a buffer NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(timerTickedToPause), userInfo: nil, repeats: false) } @objc func timerTickedToPause(timer: NSTimer) { player.pause() // pause now for some time. 90s is not enough. NSTimer.scheduledTimerWithTimeInterval(120, target: self, selector: #selector(timerTickedToPlay), userInfo: nil, repeats: false) } @objc func timerTickedToPlay(timer: NSTimer) { // try to resume playback print(player.rate) player.play() print(player.rate) // check in some seconds if it recovered NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(timerTickedCheck), userInfo: nil, repeats: false) } @objc func timerTickedCheck(timer: NSTimer) { // it reports rate = 1.0 but is not playing here though! // there is no way to know for me it did not recover here!? print(player.rate) // recover by creating a new item works let item = AVPlayerItem(URL: streamurl) player.replaceCurrentItemWithPlayerItem(item) player.play() } } let test = AVPlayerTest() test.startTest() 
+7
ios avplayer
source share

No one has answered this question yet.

See similar questions:

4
AVPlayer cannot resume after a pause + some waiting
one
AVPlayer pauses live broadcast on iOS 9.3

or similar:

102
Check AVPlayer Playback Status
6
AVPlayer is never ready to play
3
Play audio file and pause at different times on iOS
2
distinguish from [UICollectionViewCell] to an unrelated type of 'profileviewCell', as always
one
Playback in AVPlayer causes UI to hang
one
seekToTime launches two AVPlayers simultaneously
one
Does the timer run faster on the iOS simulator?
one
Swift 2 iOS 9 animation disappears after changing button text
0
AVplayer does not play live audio
0
How to increase the speed of a timer object using Swift

All Articles