Skip to content

Instantly share code, notes, and snippets.

@B-Lach
Created November 22, 2016 09:29
Show Gist options
  • Save B-Lach/d78306e95634fb8fa48eb8e15a70405d to your computer and use it in GitHub Desktop.
Save B-Lach/d78306e95634fb8fa48eb8e15a70405d to your computer and use it in GitHub Desktop.
import AVFoundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
extension AVPlayerItemStatus {
var description: String {
switch self {
case .failed:
return "Failed"
case .readyToPlay:
return "ReadyToPlay"
case .unknown:
return "Unknown"
}
}
}
extension AVPlayerTimeControlStatus {
var description: String {
switch self {
case .paused:
return "Paused"
case .playing:
return "Playing"
case .waitingToPlayAtSpecifiedRate:
return "WaitingToPlayAtSpecifiedRate"
}
}
}
class AVPlayerTest {
// Results
// LIVESTREAM
// Using a mp3 live stream will apparently fail every time
// Using the same stream embedded in a m3u playlist will also fail
// Using an acc stream will fail as well
// REMOTE FILES
// Using a mp3 file is working
// Using a m4a file is working
// Playlists not tested
// Conclusion
// Is there any format that is working with live streams using AVPlayer?
let player = AVPlayer()
// MP3 live stream
let streamurl = URL(string: "http://mp3.ffh.de/radioffh/hqlivestream.mp3")!
// MP3 live stream embedded in playlist
// let streamurl = URL(string: "http://streams.ffh.de/radioffh/mp3/hqlivestream.m3u")!
// AAC live Stream
// let streamurl = URL(string: "http://mp3.ffh.de/radioffh/hqlivestream.aac")!
// Remote M4A File
// let streamurl = URL(string: "http://tracking.feedpress.it/link/13453/4805625/fs187-knueppelgraetsche-richtig-geil.m4a")!
// Remote MP3 File
// let streamurl = URL(string: "http://files.wrint.de/WR307_Was_ist_Politik.mp3")!
func startTest() {
let item = AVPlayerItem(url: streamurl)
item.canUseNetworkResourcesForLiveStreamingWhilePaused = true
player.replaceCurrentItem(with: item)
player.play()
Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(timerTickedToPause), userInfo: nil, repeats: false)
printStatus()
}
@objc func timerTickedToPause(timer: Timer) {
player.pause()
// pause now for some time. 90s is not enough.
Timer.scheduledTimer(timeInterval: 120, target: self, selector: #selector(timerTickedToPlay), userInfo: nil, repeats: false)
}
@objc func timerTickedToPlay(timer: Timer) {
// try to resume playback
printStatus()
player.play()
printStatus()
// check in some seconds if it recovered
Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(timerTickedCheck), userInfo: nil, repeats: true)
}
@objc func timerTickedCheck(timer: Timer) {
printStatus()
}
func printStatus() {
guard let item = player.currentItem else { return }
print("###########")
print("rate: \(player.rate)")
print("currentItem errror: \(item.error)")
print("currentItem errrorLog: \(item.errorLog())")
print("timeControlStatus: \(player.timeControlStatus.description)")
print("reasonForWaitingToPlay; \(player.reasonForWaitingToPlay?.description)")
print("playbackLikelyToKeepUp: \(item.isPlaybackLikelyToKeepUp)")
print("playbackBufferEmpty: \(item.isPlaybackBufferEmpty)")
print("playbackBufferFull: \(item.isPlaybackBufferFull)")
print("status ", item.status.description )
print("###########")
}
}
let test = AVPlayerTest()
test.startTest()
@meggamind
Copy link

did you find a solution to issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment