如何实现音频单元的暂停功能

时间:2012-08-21 12:40:42

标签: core-audio audiounit audio-player

我的代码基于this example,并且已经实现了停止功能,有取消初始化和停止音频单元的功能:

    AudioOutputUnitStop(toneUnit);
    AudioUnitUninitialize(toneUnit);
    AudioComponentInstanceDispose(toneUnit);
    toneUnit = nil;

在示例中,我没有必要链接到暂停功能,因为只播放一个频率,因此暂停和停止之间没有区别。然而,在我的实现中,我正在播放一系列不同的频率,我希望能够暂停播放。

如何做到这一点?

2 个答案:

答案 0 :(得分:2)

  • 淡出(~10ms)AU的输出,在淡入淡出后输出静音
  • 记住您停止阅读输入信号的位置
  • 在您恢复之前重置AU
  • 从您上面记录的位置恢复(此处,输入信号到AU的淡入也会很好)

答案 1 :(得分:0)

播放时,您可以尝试将0写入缓冲区。这实际上是暂停。 这是您可以在回放回调中使用的一些代码。

   NSLog(@"Playing silence!");

   for (int i = 0 ; i < ioData->mNumberBuffers; i++){
    //get the buffer to be filled
    AudioBuffer buffer = ioData->mBuffers[i];
    UInt32 *frameBuffer = buffer.mData;

    //loop through the buffer and fill the frames
       for (int j = 0; j < inNumberFrames; j++){
           frameBuffer[j] = 0;
       }
   }
相关问题