In a new project drag a mp3 audio file into the Supporting Files folder
Select Copy item if needed
The audio file should now appear in the Supporting Files Folder
Add 2 buttons to the storyboard, the first at the top left and the other at the top right.
Rename the buttons to Play Audio and Stop Audio
Select the Play Audio button and add a top constraint and a left constraint then press the Add 2 Constraints button.
Select the Stop Audio button and add a top constraint and a right constraint then press the Add 2 Constraints button.
Open the Assistant Editor by clicking on the icon with the overlapping circles.
Select the Play Audio button and then hold down the control button and drag into the Assistant Editor.
A popup will appear, set the field values as follows
- Connection: Action
- Name: playAudioBtnPressed
- Type: UIButton
- Event: Touch Up Inside
- Arguments: Sender
Select the Stop Audio button and then hold down the control button and drag into the Assistant Editor.
A popup will appear, set the field values as follows
- Connection: Action
- Name: stopAudioBtnPressed
- Type: UIButton
- Event: Touch Up Inside
- Arguments: Sender
Under the line import UIKit add
import AVFoundation
Above the the method viewDidLoad add the following code
var sampleAudio = AVAudioPlayer()
We need to declare this variable outside a function to make it a global variable
In the method viewDidLoad add the following code under super.viewDidLoad()
if var filePath = NSBundle.mainBundle().pathForResource("sampleAudio", ofType: "mp3"){}
This code creates a filePath to the audiofile. The if statement is at the start of the declaration to test that filePath exists.
You will need to replace sampleAudio with the name of the audio file you added the project.
Inside the if statment add the following code.
var fileUrl = NSURL.fileURLWithPath(filePath)
This code creates a variable of type NSURL from the variable filePath.
Next add
sampleAudio = AVAudioPlayer(contentsOfURL: fileUrl, error: nil)
This code loads the audio file with the fileUrl variable
finally add
sampleAudio.enableRate = true
This allows us to play with the speed of the audio file.
Add an else to the IF statment and print a message to the console.
println("Audio file is empty")
In the playAudioBtnPressed IBAction add the following lines.
sampleAudio.stop()
It is good practice to alway stop the audio before playing it again.
sampleAudio.currentTime = 0.0
This set the audio back to the start
sampleAudio.play()
Now we can start playing the audio
In the stopAudioPressed IBAction add
sampleAudio.stop()
This only needs to stop the audio
Run the app in the iOS simulator and you should be able to play and stop the audio.
Back on the storyboard add another to buttons the bottom left and bottom right.
Name the buttons Play Fast and Play Slowly
Select the Play Fast button and add a bottom constraint and a left then press the Add 2 Constraints button.
Select the Play Slowly button and add a bottom constraint and a right then press the Add 2 Constraints button.
Select the Play Fast button and then hold down the control button and drag into the Assistant Editor.
A popup will appear, set the field values us follows
- Connection: Action
- Name: playFastBtnPressed
- Type: UIButton
- Event: Touch Up Inside
- Arguments: Sender
Select the Play Fast button and then hold down the control button and drag into the Assistant Editor.
A popup will appear, set the field values us follows
- Connection: Action
- Name: playSlowBtnPressed
- Type: UIButton
- Event: Touch Up Inside
- Arguments: Sender
Back in the assistant editor add the following function.
func playAudio(){}
Cut the code from the playAudioPressed IBAction
Add paste it into the playAudio() fucntion
Add the following code to the playAudioPressed IBAction
sampleAudio.rate = 1.0
This will set the audio to play at its normal speed
playAudio()
This will call the playAudio function which will play the audio.
Add the following code to the playFastBtnPressed IBAction
sampleAudio.rate = 2.0
This will set the audio to play fast.
The value 2.0 is the highest value rate can accept
playAudio()
This will call the playAudio function which will play the audio.
Add the following code to the playSlowBtnPressed IBAction
sampleAudio.rate = 0.5
This will set the audio to play slow.
The value 0.5 is the lowest value rate can accept
playAudio()
This will call the playAudio function which will play the audio.
Run the app in the iOS simulator, You will now be able to;
- Press the Play Audio button and have the audio play at the normal rate
- Press the Play Fast and have the audio play fast
- Press the Play Slowly button and have the audio play slow
- Press the Stop Audio button to stop the audio.
Rotate the simulator to the right and the layout should still be correct.
No comments:
Post a Comment