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

标签 ios multimedia

如何用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/

相关文章:

ios - 将图像动态添加到原型(prototype)自定义单元格

actionscript-3 - 如何从 Actionscript 3.0 使多个视频剪辑同时播放

frameworks - 如何在 Gstreamer (windows) 中重用 directshow 组件

JavaScript 游戏 : What should i know?

windows - 您可以在 Windows wave 音频输入中重复使用缓冲区吗?

ios - 我们可以从 iOS 设备向 APNs 发送推送通知吗?

php - 使用xcode上传图像(MySQL + PHP)

ios - 如何仅使用一个单元格在一个 View 上设置 UITableView 单元格宽度不同于在 objc 中选择段按钮

iphone - 在 UITableView 中对大量行/节进行动画处理性能不佳