Handling Audio in iOS
Its not a rare occurrence when you will have to deal with Audio data in your iOS apps. Lets take a look at how we can use the AVFoundation framework to play, restart, stop, pause audio file in our iOS apps. We will even have a look at how we can manipulate with the audio timings, ie go back/front to a specific time and play the audio from there.
- Drag and drop 4 buttons on the storyboard scene from the object library and style and position the segment control the way you want, the 4 buttons are start, restart, play(pause), stop. Also drag and drop a slider onto the screen. The first button is use to load the music and start playing it. Its used for nothing else. The other 3 buttons will be the most used ones. The restart button will take the music to start and play it from first, the play(pause) is a single toggle button that is use for playing and pausing, the stop button will take the music to start and stop playing it.
- Now lets add the outlet and the actions, add an outlet for the play(pause) button because we will be changing the button title as we play and pause the music file. Add 3 actions for the restart, stop and the start button. Add an outlet and an action(changeAudioTime) for the slider.
Get the path of the music file by using the following code.1let path = NSBundle.mainBundle().pathForResource("musicname", ofType: "mp3")
Once you have the path of the resource create an instance of NSURL from the path1let audioUrl = NSURL(fileURLWithPath: path)
Next we need to import the AVFoundation framework in the view controller file, this allows us to use the speakers on the iphone to play the music file that we have. There are 2 steps to be done here 1st is to place the code
import AVFoundationin top of view controller file and the other step is to click on the project settings in the file explorer, the go to build phases, and make sure you are selected on target, the click the + button in link binary with libraries, type AVFoundation in the dialog and click add.
- Next step is to create a audio player and give it the contents of the music file that we have with us.
- To start playing the music we can call the play method on the audio player, to pause playing the music we can call the stop method on the audio player, to change the timings of the music we can use the currentTime property of the audio player.
- In the code below we start playing the music once the view is loaded into the memory, we can’t present the alert controller here because it can be presented only when the view controller hierarchy is set up, that is why we present the alert controller in the viewDidAppear method which is called after the view hierarchy is setup, ie after the views appear on the screen.
- Always remember that you must stop playing the audio before manipulating with the currentTime of the audio player.