Monday, November 2, 2015

How to transition between sound effects seamlessly in Swift 2

As part of Udacity's iOS Developer NanoDegree program, we're creating an app called Pitch Perfect. This simple app allows a user to record audio, then play back the audio with different audio effects.

Two of the audio effects that are part of the app are playing audio fast, and playing audio slow. In order to do that, you'll need to add an audio file to your project to play.  You can do that by right clicking your project in the Navigator section of Xcode, then choose "Add files to <project name>...":


Ensure when you do so, that Add to Target is checked for your project:


Your file will appear in the Navigator area within your project, and you'll now be able to access it.  The file we're using in this example is called "movie_quote.mp3".

Next, you'll need to import the AVFoundation framework into your ViewController, so you can work with Audio or Video files:



Next we need to instantiate the AVAudioPlayer class.  You should do so as a global variable if you plan to access it in multiple functions (as we will do in this example):


Now, in the viewDidLoad() function, we can create the AVAudioPlayer object using the audio file we've added to our project.

The AVAudioPlayer method we use to create the object take two arguments:
1. contentOfURL, of type NSURL
2. fileTypeHint, of type optional String

For the contentofURL object, the AVAudioPlayer method is looking for a path to get to the audio file. To do so, we can use NSBundle.mainBundle().pathForResource to create a string path for the file:


Because the contentOfURL is looking for an NSURL type, you can use NSURL.fileURLWithPath to convert the string:


Lastly, we'll create the AVAudioPlayer object:


Now we have an AVAudioPlayer object is available.  To see what you can do with these objects, you can find the class reference here.  In our example, we'll explore:

- the play and stop methods
- the enableRate, rate, and currentTime attributes

In our ViewController, we've created a playback function, as well as button actions.  You can see in the screenshot below, we have two buttons, one for fast playback and one for slow.  


In the playback function, playAudio we start by stopping any current playback using the stop() method.  Next, we set the enableRate attribute to "true" so we can change the rate.  Then, we set the rate using rate attribute, passing in the rate argument from the function.  Finally, we play the audio.

You'll then notice in the @IBAction functions, we're calling the playAudio function, and passing in a value to either speed up, or slow down the playback speed.

The code above allows for a seamless transition between sounds effects.  You can tap the slow button and the audio will play slowly.  At any time, you can tap the fast button, and the audio will immediately speed up.

If we wanted to change that, and have the audio restart each time a button is clicked, you can do so by setting the currentTime attribute to 0.0 to ensure the audio starts from the beginning:


That's it - you can now choose to seamlessly transition between audio effects, or restart audio each time a new button is clicked.  For more information on these topics, go here:










No comments:

Post a Comment