I've been trying to use AVAudioPlayer
to play remote MP3 files in my app and I've read some of the other answers on Stack Overflow but I still cannot get it to work. The common suggestion I'm reading is to use AVPlayer
instead of AVAudioPlayer
. I don't know why this is. Also, in one of the questions the accepted answer mentioned that one needs to create an instance of AVPlayer
in order to use it in the app. What should I do?
What you've read is correct. Creating an instance of AVPlayer will allow you to successfully run your code.
You should initialize your AVPlayer outside of where you want to call it.
var myPlayer = AVPlayer()
Now, in a separate place in your code, try something like this:
func playAudio() {
//initialize whatever you have to (you seem to have that correct)
//now call myPlayer.play(), and that should work correctly
}
Let me know if this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With