1. Access to sound services
Add the framework AudioToolBox and the sound file to be played. In addition, you need to import the interface file of the framework in the class that implements the sound service:
#import <AudioToolbox/>
To play system sound, two functions are AudioServicesCreateSystemSoundID and AudioServicesPlaySystemSound. You also need to declare a variable of type SystemSoundID, which represents the sound file to be used.
-(IBAction) playSysSound:(id)sender {
SystemSoundID sourceID;
//Calling the method of the NSBundle class mainBundle returns an NSBundle object, which corresponds to the directory to which the current program executable binary file belongs.
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"wav"];
//A CFURLRef object pointing to the file location and a pointer to the SystemSoundID variable to be set
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
}
2. Reminder sound and vibration
1. Reminder sound
Differences from system sound:
If the phone is silent, the alert tone will automatically trigger a vibration;
The function required to play reminder sound is AudioServicesPlayAlertSound instead of AudioServicesPlaySystemSound.
2. Vibration
You only need to call the AudioServicesPlaySystemSound() method and pass in the kSystemSoundID_Vibrate constant.
If the device does not support vibration (such as iPad 2), then it doesn't matter, it just won't.
3. AV Foundation framework
For compressed Audio files, or audio files that exceed 30 seconds, you can use the AVAudioPlayer class.
1. AVAudioPlayer also needs to know the path of the audio file;
2. The AVAudioPlayerDelegate corresponding to this class has two delegate methods:
1) audioDidFinishPlaying: successfully: Triggered after audio playback is completed;
2) audioPlayerEndInterruption: Triggered when the program is interrupted by the application outside the application and returns to the application.
4. MediaPlayer framework
You can use MPMoviePlayerController to play movie files (it seems that you can only play H.264 and MPEG-4 Part2 video formats), and you can also play video files on the Internet.
5. Call and customize sound effect instances
There are roughly three types of requirements:
1. Vibration
2. System sound effects (no audio files required)
3. Custom sound effects (audio files are required)
My tool class encapsulation:
//
//
// WQSound
//
// Created by Nian Qian on 12-7-20.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/>
#import <AudioToolbox/>
@interface WQPlaySound : NSObject
{
SystemSoundID soundID;
}
/**
* @brief Initializes the vibration effect for playback
*
* @return self
*/
-(id)initForPlayingVibrate;
/**
* @brief Initialize the sound effects of the playback system (no audio files required)
*
* @param resourceName System sound effect name
* @param type System sound effect type
*
* @return self
*/
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
/**
* @brief Initializes specific audio files for playback (audio files need to be provided)
*
* @param filename Audio filename (added in the project)
*
* @return self
*/
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
/**
* @brief Play sound effects
*/
-(void)play;
@end
//
//
// WQSound
//
// Created by Nian Qian on 12-7-20.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import ""
@implementation WQPlaySound
-(id)initForPlayingVibrate
{
self = [super init];
if (self) {
soundID = kSystemSoundID_Vibrate;
}
return self;
}
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type
{
self = [super init];
if (self) {
NSString *path = [[NSBundle bundleWithIdentifier:@""] pathForResource:resourceName ofType:type];
if (path) {
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);
if (error == kAudioServicesNoError) {
soundID = theSoundID;
}else {
NSLog(@"Failed to create sound ");
}
}
}
return self;
}
-(id)initForPlayingSoundEffectWith:(NSString *)filename
{
self = [super init];
if (self) {
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
if (fileURL != nil)
{
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
if (error == kAudioServicesNoError){
soundID = theSoundID;
}else {
NSLog(@"Failed to create sound ");
}
}
}
return self;
}
-(void)play
{
AudioServicesPlaySystemSound(soundID);
}
-(void)dealloc
{
AudioServicesDisposeSystemSoundID(soundID);
}
@end
Steps to call the method:
1. Join the project
2. Call the WQPlaySound tool class
2.1 Vibration
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate];
[sound play];
2.2 System sound effects, take Tock as an example
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];
[sound play];
2.3 Custom sound effects, add audio files to the project
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@""];
[sound play];