Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between AVPlayer and AVAudioPlayer in terms of functionality?

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?

like image 378
user817238593 Avatar asked Nov 10 '22 09:11

user817238593


1 Answers

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.

like image 62
sterling archer Avatar answered Nov 15 '22 11:11

sterling archer