开发者

How can I play a custom tone in iOS?

I'm trying to find some way to play tones in iOS that are created dynamically. Being able to play an audio file isn't going to cut it for me. I need a lot finer control. I know there are ways to handle audio streams at an individual packet level, which would work for me if I had some way to create the audio packets. If anyone even has some good pointers or places to go for more in开发者_如何学运维formation, I'd appreciate it.


Sure Matt Gallagher has written a tone generator using a RemoteIO audio unit.

http://cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html


What you probably want is to use an AudioQueue. It is an API a bit down in the audio stack, but allows you to generate any audio you want. I have used it in the app SC68 Player to generate the audio stream for an Atari ST sound chip emulator, to play old music from games and scene demos from the 80-ies.

The basic idea is:

  1. Spawn a background thread where you spawn a CFRunLoop to receive on demand callbacks to fill the audio queues buffer. Use AudioQueueNewOutput()
  2. Start/pause the audio queue from any thread. Use AudioQueueStart()/AudioQueueStart() pause.
  3. Implement the callback to fill the audio buffer.

The callback will look something like this:

static void AudioQueueCallback(void* inUserData, 
                               AudioQueueRef inAQ, 
                               AudioQueueBufferRef inBuffer) 
{
    [soundGenerator fillNextFrameBuffer:inBuffer->mAudioData 
                                   size:inBuffer->mAudioDataBytesCapacity];
    inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
    AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜