我的应用程序基本上是一个接一个地播放音符,我想显示每次播放的音符。每个音符都是 UI 中的一个正方形,我有一个 1 像素的视图,从左到右移动到正在播放的音符。
问题是我不确定如何同时做这两件事。我无法从我的样本渲染功能发送消息,因为这可能会很慢并且可能导致我的音频播放出现故障。有没有人有任何建议我应该如何更新我的 UI 以反映正在播放的内容?
我的音乐播放代码基于此示例,我认为 RenderTone 方法在不同的线程中运行。每次播放 1000 个样本时,我想将我的 1 像素视图移动 1 个像素(只是一个示例,也可能更少),但我不知道如何向 UI 发送消息并发送关于有多少样本的更新被玩过。
因此,在运行以下代码的变体时,我必须以某种方式更新我的 UI。
OSStatus RenderTone(
    void *inRefCon, 
    AudioUnitRenderActionFlags *ioActionFlags, 
    const AudioTimeStamp *inTimeStamp, 
    UInt32 inBusNumber, 
    UInt32 inNumberFrames, 
    AudioBufferList *ioData)
{
    // Fixed amplitude is good enough for our purposes
    const double amplitude = 0.25;
    // Get the tone parameters out of the view controller
    ToneGeneratorViewController *viewController =
        (ToneGeneratorViewController *)inRefCon;
    double theta = viewController->theta;
    double theta_increment =
        2.0 * M_PI * viewController->frequency / viewController->sampleRate;
    // This is a mono tone generator so we only need the first buffer
    const int channel = 0;
    Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;
    // Generate the samples
    for (UInt32 frame = 0; frame < inNumberFrames; frame++) 
    {
        buffer[frame] = sin(theta) * amplitude;
        theta += theta_increment;
        if (theta > 2.0 * M_PI)
        {
            theta -= 2.0 * M_PI;
        }
    }
    // Store the updated theta back in the view controller
    viewController->theta = theta;
    return noErr;
}