With those two lines of code inserted, all you need to do is play your audio file. In our example we're going to play a bomb explosion sound, so I created a property for it like this: var bombSoundEffect: AVAudioPlayer? Like I said, you need to store your audio player as a property somewhere so it is retained while the sound is playing. The only real catch is that you must store your player as a property or other variable that won't get destroyed straight away – if you don't, the sound will stop immediately.ĪVAudioPlayer is part of the AVFoundation framework, so you'll need to import that: import AVFoundation The most common way to play a sound on iOS is using AVAudioPlayer, and it's popular for a reason: it's easy to use, you can stop it whenever you want, and you can adjust its volume as often as you need.
0 Comments
Leave a Reply. |