在iphone中同时播放2个mp3声音?

时间:2011-02-17 07:16:30

标签: iphone

我们可以在iphone上同时播放2个mp3声音吗?iphone是否支持此功能?

2 个答案:

答案 0 :(得分:5)

使用音频队列服务(AudioToolbox / AudioQueue.h)中的接口。为要播放的每个声音创建一个音频队列对象。然后使用AudioQueueEnqueueBufferWithParameters函数指定每个音频队列中第一个音频缓冲区的同时开始时间。

以下限制适用于iPhone OS中的同步声音,具体取决于音频数据格式:

AAC,MP3和ALAC(Apple Lossless)音频:您可以同时播放多种AAC,MP3和ALAC格式的声音;播放这些格式的多种声音需要CPU资源进行解码。

线性PCM和IMA / ADPCM(IMA4音频):您可以同时播放多个线性PCM或IMA4格式声音而无需CPU资源。

就一次播放多个声音而言,这很容易,只需为你想玩的每一个创建一个新的播放器实例(并记得在你完成时释放它们)

 NSString *path = [[NSBundle mainBundle] pathForResource:@"YOUR_FILE_NAME" ofType:@"m4a"];  
 AVAudioPlayer* objAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
 objAudio.delegate = self;  
 [objAudio play];

要将MP3转换为类似IMA4(您可以同时播放多个)的内容,您可以运行以下内容(在终端中,在豹子中):

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

了解更多信息,请访问Audio Play/Record

答案 1 :(得分:1)

是的,但是因为s ***(对于用户而言)很烦人。

(某种(伪代码))(我懒得查找方法名称/库)

-(void)playSounds{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    [self playSound1];
    [self playSound2];
    [pool release];
}

-(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]; }

请注意,这真的快而脏。如果你想要精确度,你可能必须在玩家准备就绪时通知你并写一个能够立即触发它们的功能。