如何用AVAudioPlayer同时播放多个音频文件? 是否可以使用 AVAudioPlayer 同时播放多个音频文件? 或任何其他方式同时播放多个音频文件? 谢谢!
最佳答案
以下格式的文件可以在 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
详细教程:
https://brainwashinc.wordpress.com/2009/08/14/iphone-playing-2-sounds-at-once/
关于ios - 如何同时播放多个音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28299679/