如何同时播放多个音频文件 [英] How to Play multiple audio files simultaneously

查看:937
本文介绍了如何同时播放多个音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how to Play numbers Of audio files at same time With AVAudioPlayer? Is it Possible to Play numbers Of audio files can play at same time using AVAudioPlayer? or any other way to play numbers of audio files at same time ? Thank You!

推荐答案

以下格式的文件可以在iPhone上同时播放.

Files with below formats can be played simultaneously on iPhone.

AAC,MP3和ALAC(Apple无损)音频:与CPU资源有关. 线性PCM和IMA/ADPCM(IMA4音频):无需担心CPU资源.

AAC, MP3, and ALAC (Apple Lossless) audio: have CPU resource concern. Linear PCM and IMA/ADPCM (IMA4 audio): without CPU resource concerns.

您只需要为要播放的每个音乐文件创建一个新的播放器实例.

You just need to create a new player instance for every music file, that you want to play.

示例代码段:

-(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

/usr/bin/afconvert -f caff -d ima4 sound.mp3 sound.caf

详细的教程:

https://brainwashinc.wordpress .com/2009/08/14/iphone-playing-2-sounds-at-once/

这篇关于如何同时播放多个音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆