如何同时播放多个音频文件

Vij*_*hod 3 multimedia ios

如何使用AVAudioPlayer同时播放音频文件的数量?是否可以播放使用AVAudioPlayer同时播放音频文件的数量?或任何其他方式同时播放多个音频文件?谢谢!

bll*_*akk 8

以下格式的文件可以在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];
}
Run Code Online (Sandbox Code Playgroud)

转换为支持的格式(即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/