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.
Edit for in addition: Also Luaan is right. "Live streaming needs server support - at least the ability to do partial downloads. Does some other player allow live streaming with that URL?" You should also try with an undefined duration sound url.
Have you tried to approach like it is an undefined duration audio stream ?
CachingPlayerItem going to prepare an AVURLAsset from the url.
After AVPlayerItem is initialized, make an AVPlayer object from it so that it can kick start the process of loading audio stream from the given url:
Once resource is installed:
Then, start receiving data bytes into the delegate function:
Now you have started receiving live audio stream.
Now create an OutputStream object, open it then append the bytes we are receiving in the above delegate function and thats it, we saved the desired part of the live audio stream.
Taken from: Medium Article, Mohan Pandey