Playing a Sound, Waiting it to Finish Playing and Continuing (iPhone)
Introduction
As a beginner with iPhone development in Objective-C, playing a sound is an essential feature that can be achieved using the SystemSound API. In this article, we will explore how to play a sound, wait for it to finish playing, and continue with the rest of the code.
Understanding System Sound API
The SystemSound API provides a way to play sounds on the device. The AudioServicesCreateSystemSoundID function is used to create a system sound ID, which is then used to play the sound using the AudioServicesPlaySystemSound function.
In our case, we need to play a sound file named “file.wav”. We will use the SystemSound API to achieve this.
Breaking Down the Code
The provided answer breaks down the solution into two main functions: playASound: and startSound.
1. playASound:
This function takes a filename as input, creates a system sound ID using AudioServicesCreateSystemSoundID, and plays the sound using AudioServicesPlaySystemSound. The function returns the created system sound ID.
-(SystemSoundID) playASound: (NSString *) file {
// Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
file];
SystemSoundID soundID;
// Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
// Play the file
AudioServicesPlaySystemSound(soundID);
return soundID;
}
2. startSound:
This function calls playASound: and prints “Hello” immediately after playing the sound. It also adds a completion handler to register a callback function when the sound finishes playing.
-(void)startSound
{
printf("Hello");
SystemSoundID id = [self playASound:@"file.wav"];
AudioServicesAddSystemSoundCompletion (
id,
NULL,
NULL,
endSound,
NULL
);
}
3. endSound:
This function is the callback registered when the sound finishes playing. It prints “World” and returns.
void endSound (
SystemSoundID ssID,
void *clientData
)
{
printf("world\n");
}
Putting it all Together
To wait for the sound to finish playing, we need to add a completion handler to AudioServicesAddSystemSoundCompletion. This completion handler is called when the sound finishes playing.
-(void)startSound
{
printf("Hello");
SystemSoundID id = [self playASound:@"file.wav"];
AudioServicesAddSystemSoundCompletion (
id,
NULL,
NULL,
endSound,
NULL
);
}
In this code, endSound is the callback function that is called when the sound finishes playing. It prints “World” and returns.
Conclusion
Playing a sound on an iPhone using Objective-C can be achieved by creating a system sound ID using AudioServicesCreateSystemSoundID and playing the sound using AudioServicesPlaySystemSound. To wait for the sound to finish playing, we need to add a completion handler to AudioServicesAddSystemSoundCompletion.
By breaking down the solution into smaller functions and registering a callback function, we can ensure that our code is efficient and effective.
Tips and Variations
- When working with system sounds, make sure to handle any potential errors that may occur during playback.
- To play multiple sounds simultaneously, you can use
AudioServicesAddSystemSoundCompletionmultiple times with different sound IDs. - You can also use other audio APIs such as AVAudioPlayer or AVAudioSession to play sounds on your iPhone app.
Example Use Cases
- Playing a sound effect in a game to signal a successful action.
- Playing a sound alert when the user receives an incoming message.
- Playing a music track during a transition between screens.
Last modified on 2024-10-30