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

I am trying to use AVAudioPlayer

to play MP3 files in my application and I have read some of the other answers on Stack Overflow, but I still cannot get it to work. The general suggestion I am reading is to use AVPlayer

instead AVAudioPlayer

. I don't know why this is so. Also, one of the questions in the accepted answer mentioned that you need to create an instance AVPlayer

in order to use it in your application. What should I do?

+3


source to share


1 answer


What you have read is correct. Instantiating the AVPlayer will allow you to run your code successfully.

You must 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 it helps.

+1


source







All Articles