谁能告诉我这是如何在 iPhone 上实现的?我正在尝试制作一款可以同时播放大约 12 个声音的游戏,但我不知道如何使用 AudioQueueServices。我知道您必须初始化、添加缓冲区、播放它们并使用 AudioQueueEnqueueBufferWithParameters 来同时播放,但我不知道如何将其转换为代码。任何有这方面代码的人或者可以向我解释它的人都会很棒!
如果您听说过音调网格/音调板,这正是我正在尝试做的事情。我知道应用程序商店中已经有一些应用程序可以执行此操作,但我不知道它是如何完成的。
TLDR;需要 AudioQueueServices 同时播放 10 种以上声音的帮助。
示例:
新播放
if (isPlaying == NO) {
err = AudioQueueNewOutput (&streamFormat, AudioEngineOutputBufferCallback, self, nil, nil, 0, &outputQueue);
if (err != noErr) NSLog(@"AudioQueueNewOutput() error: %d", err);
排队缓冲区
outputBuffersToRewrite = 3;
bufferByteSize = (sampleRate > 16000)? 2176 : 512; // 40.5 Hz : 31.25 Hz
for (i=0; i<3; i++) {
err = AudioQueueAllocateBuffer (outputQueue, bufferByteSize, &buffer);
if (err == noErr) {
[self generateTone: buffer];
err = AudioQueueEnqueueBuffer (outputQueue, buffer, 0, nil);
if (err != noErr) NSLog(@"AudioQueueEnqueueBuffer() error: %d", err);
} else {
NSLog(@"AudioQueueAllocateBuffer() error: %d", err);
return;
}
}
开始播放
isPlaying = YES;
err = AudioQueueStart(outputQueue, nil);
if (err != noErr) { NSLog(@"AudioQueueStart() error: %d", err); isPlaying= NO; return; }
} else {
NSLog (@"Error: audio is already playing back.");
设置流格式字段
BOOL isHighSampleRate = (sampleRate > 16000);
int bufferByteSize;
AudioQueueBufferRef buffer;
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = sampleRate;
streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
streamFormat.mBitsPerChannel = 16;
streamFormat.mChannelsPerFrame = 1;
streamFormat.mBytesPerPacket = 2 * streamFormat.mChannelsPerFrame;
streamFormat.mBytesPerFrame = 2 * streamFormat.mChannelsPerFrame;
streamFormat.mFramesPerPacket = 1;
streamFormat.mReserved = 0;
最佳答案
AudioQueue可以同时播放声音。
来自苹果:
如何同时播放多个声音?
使用音频队列服务 (AudioToolbox/AudioQueue.h) 中的接口(interface)。为您要播放的每种声音创建一个音频队列对象。然后使用 AudioQueueEnqueueBufferWithParameters 函数指定每个音频队列中第一个音频缓冲区的同时开始时间。
以下限制适用于 iPhone 操作系统中的同步声音,具体取决于音频数据格式:
AAC, MP3, and ALAC (Apple Lossless) audio: You may play multiple AAC, MP3, and ALAC format sounds simultaneously; playback of multiple sounds of these formats will require CPU resources for decoding.
Linear PCM and IMA/ADPCM (IMA4 audio): You can play multiple linear PCM or IMA4 format sounds simultaneously without CPU resource concerns.
关于iphone - 使用 AudioQueueServices 同时播放 (10+),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7382903/