IOS Programming -Building a simple AudioPlayer in iOS

Building a simple AudioPlayer in iOS

This tutorial describes how to build a simple AudioPlayer Element for a native iPhone App. It will play an mp3 file with a simple AudioPlayer interface, containing a play and pause button, a slider with scrubbing functionality and a display for duration and elapsed time. The code is written with Xcode 5. Xcode versions below this will not be able to build and run the project. I will not go too much into detail about simple steps like how to create a new Xcode project or adding sources to a project. This knowledge is assumed for this tutorial. The full iOS app project source code can be downloaded from GitHub. At the end, the AudioPlayer will look like this:
audioPlayer









Let’s start coding

First of all a “Single View Application” in Xcode has to be created as a new project. Then the supporting files should be added like an .mp3 audio file and images containing a play and a pause button. After setting up the project, the first step is to create the player items in the .storyboard file. Adding a button with a playbutton image, a slider and two empty textLabels is all you need. So your storyboard should look like this:
xcode storyboard

AudioPlayer Class

Next step is to create an AudioPlayer Class. It can be grouped into a folder named “AudioPlayer” for example. The YMCAudioPlayer.h file should look like this:
The “audioPlayer” property is a type of AVAudioPlayer, the built in class from the AVFoundationframework. To control the AudioPlayer, several public classes are needed. The Init, play and pause methods need no explanation I think. setCurrentAudioTime is to set the position at what time the audio file will be played. getAudioDuration returns the length of the audio track as a float value that can be converted to a timeFormat with the method of the same name. getCurrentAudioTime returns the current position where the audio file is playing right now.
The YMCAudioPlayer.m file should look like this:
The init method is to set up the internal “audioPlayer” property with the AVAudioPlayer class. The filename is required as an argument:
After that [self.audioPlayer play]; can be called directly to play the audio. I just added seperate methods arround the play and the pause commands to keep the commands consistent and keep the ability to add some code later. For example, if pause is pressed, the current player position can be stored with NSUserDefaults.
The timeFormat method returns a nice time formatted string from the given float value by rounding, dividing and string formatting like 01:38 for example.

ViewController Class

Next step is to edit the automatically created ViewController Class. The ViewController.h file should look like this:
The previously edited YMCAudioPlayer class can now be included here. And once again the “audioPlayer” property is cast into this class. The IBOutlet properties have now to be connected to the storyboard elements we created before. Simply drag the properties to its respective element in the storyboard with the assistant editor view:
connect properties
The ViewController.m file should look like this:
Here in our view, the methods of the YMCAudioPlayer class can be called, processed and assigned to view elements. In the automatically called viewDidLoad method, the internal “audioPlayer” property gets initialised as a YMCAudioPlayer.
After that, the setupAudioPlayer method can be called. This method sets the textLabels for example the “timeElapsed” property:
Or the “duration” property using the timeFormat method for the return value of our before createdgetAudioDuration method:
The following method playAudioPressed is simply connected as an IBAction with the playButton:
UIPlaybutton
This method checks the “isPaused” property to handle the display of the button images depending on the current state (if playing or paused). A timer function is also called here to update the textLabels with the updateTime method every second with the new formatted time values of the played audio. The value of the slider position will also be updated here.
The “scrubbing” property is to check if the user is currently dragging the slider. In this case the slider position should not be updated every second. Otherwise the slider will be flickering due to it updating the current position and the current dragged position at the same time.
The method setCurrentTime is to set exactly this audio position as a further IBAction element. Also the userIsScrubbing method is connected to the UISlider element, especially for the Touch drag Inside event, to check if the user is dragging the slider. If setCurrentTime is called, the scrubbing property is set to false again:
scrubbing
After all methods are connected to the elements in the storyboard we’re ready to make a build and run the app with our AudioPlayer element.
The full iOS app project source code can be downloaded on GitHub.
I’m looking forward to your comments or requests for new articles.

Nhận xét

Bài đăng phổ biến