以下格式的文件可以在iPhone上同时播放.
AAC,MP3和ALAC(Apple Lossless)音频:有CPU资源问题.
线性PCM和IMA / ADPCM(IMA4音频):没有CPU资源问题.
您只需要为要播放的每个音乐文件创建一个新的播放器实例.
示例代码段:
-(void)playSounds{
[self playSound1];
[self playSound2];
}
-(void)playSound1{
NSString *path = [[NSBundle mainBundle] pathForResource:@"file1"
ofType:@"m4a"];
AVAudioPlayer* player= [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:path]
error:NULL];
player.delegate = self;
[player play];
}
-(void)playSound2{
SString *path = [[NSBundle mainBundle] pathForResource:@"file2"
ofType:@"m4a"];
AVAudioPlayer* player= [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:path]
error:NULL];
player.delegate = self;
[player play];
}
转换为支持的格式(即mp3到caf):
/usr/bin/afconvert -f caff -d ima4 sound.mp3 sound.caf
详细教程: