我最近一直在尝试制作一款游戏,允许用户说话,然后用有趣的声音播放他说的话。
问题是只有当有人在 iphone 附近讲话时才会进行录音(我已经管理了这部分,我一直有一个录音机处于活动状态,并得到它的平均值和峰值)。
问题是当我想播放用户回复的内容时。我一直在四处寻找,唯一的方法似乎是音频队列服务,因为它允许保存到内存中。从我的角度来看,将声音保存到文件并播放由于文件分配而存在问题。
我还没有找到任何关于如何保存到内存的教程,所以如果有人知道如何执行此操作或如何保存到文件并毫无问题地播放(现在声音被切断,前 0.5 秒听不到而且fps下降了20)。
我目前的代码是:
void AudioInputCallback(
void *inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime,
UInt32 inNumberPacketDescriptions,
const AudioStreamPacketDescription *inPacketDescs)
RecordState* recordState = (RecordState*)inUserData;
if(!recordState->recording)
{
printf("Not recording, returning\n");
}
printf("Writing buffer %d\n", (int)recordState->currentPacket);
//What to put here to save to memory?
recordState->currentPacket += inNumberPacketDescriptions;
AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL);
}
void AudioOutputCallback(
void* inUserData,
AudioQueueRef outAQ,
AudioQueueBufferRef outBuffer)
{
PlayState* playState = (PlayState*)inUserData;
if(!playState->playing)
{
printf("Not playing, returning\n");
return;
}
printf("Queuing buffer %d for playback\n",(int) playState->currentPacket);
AudioStreamPacketDescription* packetDescs;
UInt32 bytesRead;
UInt32 numPackets = 8000;
OSStatus status;
//What to put here to read from memory?
status = AudioQueueEnqueueBuffer(
playState->queue,
outBuffer,
0,
packetDescs);
playState->currentPacket += numPackets;
AudioQueueFreeBuffer(playState->queue, outBuffer);
}