First time I am working with AVPlayer and I want to play a mp3 file using HTTP request with live streaming. I use AVPlayer
that is working fine to play mp3 files, but the problem is AVPlayer
needs to download the mp3 first then play it. I don't know why AVPlayer
downloads the file and then play it instead of live playing. My Xcode 8.2.1 and I am using Swift 3.
Here is my code snippet.
var audioPlayer = AVPlayer()
var avplayerItem : AVPlayerItem?
override func viewDidLoad() {
super.viewDidLoad()
let fileurl:URL = URL(string : "http://www.noiseaddicts.com/samples_1w72b820/2514.mp3")!
avplayerItem = AVPlayerItem(url : fileurl)
audioPlayer = AVPlayer(playerItem : avplayerItem)
audioPlayer.rate = 1.0
audioPlayer.play()
}
In case of AVPlayer
requires to download the whole file before playing it, what should I do for audio live streaming?
Thanks.
You are basically using mp3 as the container to transmit your audio throughout internet so the player must have to download it all, if you want to make it efficiently or avoid latency of the download you must use HLS, DASH, CMAF or WebRTC to transmit it, so the player will download it by chunks, and plays it while downloading, something similar to Spotify or Apple music does.