iOS提供了以下几种音频播放方法:
一、System Sound Services
需要用到AudioToolbox框架,将短声音注册到system sound服务上。这是一种最简单最底层的音频播放方式,只能播放一些满足以下条件的较短的提示音或警告音:
1、音频长度<30s;
2、必须是PCM或IMA4格式;
3、必须是打包成Core Audio Format(.caf), Wavaform Audio(.wav), Audio Interchange File(.aif)格式的文件
这种播放方式还有诸多限制:不能控制播放进度、调用方法后立即播放、不能循环播放等。
具体代码可以参考苹果官方示例:SystemSound
#import "SysSoundViewController.h"
@implementation SysSoundViewController
@synthesize soundFileURLRef;
@synthesize soundFileObject;
- (void) viewDidLoad {
[super viewDidLoad];
// Provide a nice background for the app user interface.
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
// Create the URL for the source audio file. The URLForResource:withExtension: method is
// new in iOS 4.0.
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: @"tap"
withExtension: @"aif"];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
}
// Respond to a tap on the System Sound button.
- (IBAction) playSystemSound: (id) sender {
AudioServicesPlaySystemSound (soundFileObject);
}
// Respond to a tap on the Alert Sound button.
- (IBAction) playAlertSound: (id) sender {
AudioServicesPlayAlertSound (soundFileObject);
}
// Respond to a tap on the Vibrate button. In the Simulator and on devices with no
// vibration element, this method does nothing.
- (IBAction) vibrate: (id) sender {
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
- (void) dealloc {
AudioServicesDisposeSystemSoundID (soundFileObject);
CFRelease (soundFileURLRef);
[super dealloc];
}
@end
二、AVAudioPlayer类
使用这个类需要先引入AVFoundation.framework,它是一种比SystemSound更高级的音频播放方法,对于超过30s或压缩过的音频文件,都可以使用AVAudioPlayer播放。它支持以下音频格式:
■ AAC
■ AMR(AdaptiveMulti-Rate, aformatforspeech)
■ ALAC(AppleLossless)
■ iLBC(internetLowBitrateCodec, anotherformatforspeech)
■ IMA4(IMA/ADPCM)
■ linearPCM(uncompressed)
■ µ-lawanda-law
■ MP3(MPEG-1audiolayer3
使用AVAudioPlayer类播放声音,只需为其指定一个音频文件并设定一个实现了AVAudioPlayerDelegate协议的代理对象即可:
@interface MainViewController () <AVAudioPlayerDelegate>
{
AVAudioPlayer *_audioPlayer;
}
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
_audioPlayer.delegate = self;
}
-(void)playSound
{
// 播放声音
NSString *wavPath = [[NSBundle mainBundle]pathForResource:@"composer_open" ofType:@"wav"];
if (wavPath) {
NSURL *soundURL = [NSURL fileURLWithPath:wavPath];
_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:nil];
// 播放器属性
// 1. 音量 0.0~1.0之间
_audioPlayer.volume = 0.6;
// 2. 循环播放次数
_audioPlayer.numberOfLoops = 2;
// 3. 播放的起始位置
_audioPlayer.currentTime = 5.6;
// 4. 声道数
NSUInteger channels = _audioPlayer.numberOfChannels;
// 5. 声音持续时间
NSTimeInterval duration = _audioPlayer.duration;
// 6. 仪表计数
_audioPlayer.meteringEnabled = YES;
[_audioPlayer updateMeters];
[_audioPlayer play];
}
}
@end
而且,通过调用play, pause, stop方法可以随时控制AVAudioPlayer对象的播放、暂停和停止。
三、Audio Queue Services
使用这种方法可以实现对声音的完全控制,如可以在音频数据从文件读到内存缓冲区后对声音进行一定的处理后再播放,进而实现声音的变速播放功能。
Audio Queue Services方法较为复杂,具体请参考Audio Queue Services Programming Guide 和Speak Here程序示例。
四、OpenAL
与OpenGL类似,OpenAL是一套跨平台的开源音频处理接口,主要适合开发游戏的音效,用法与其它平台相同。