在iOS 9中调用DisposeAUGraph()时导致kAUGraphErr_CannotDoInCurrentContext错误的原因是什么?

时间:2016-01-07 12:57:18

标签: ios audio audiotoolbox

我有一些使用AudioToolbox框架MusicPlayerMusicSequenceAUGraph播放MIDI文件的代码。

播放完成后的一段时间,下面的代码用于整理。此代码在iOS 6-8中没有问题。

但是,在iOS 9中,对DisposeAUGraph的调用失败,返回错误代码kAUGraphErr_CannotDoInCurrentContext

DisposeAUGraph的文档几乎不存在,但返回代码本身的文档说明:

  

为了避免在渲染线程中旋转或等待(一个坏主意!),许多对AUGraph的调用都可以返回:kAUGraphErr_CannotDoInCurrentContext。仅当从其渲染回调调用AUGraph API时才会生成此结果。这意味着它所需的锁在那时由另一个线程保持。如果您看到此结果代码,通常可以再次尝试操作 - 通常是NEXT渲染周期(因此同时可以清除锁定),或者您可以将该调用委托给应用程序中的另一个线程。你不应该旋转或暂停渲染线程。

下面的代码是 not AUGraph的渲染回调调用 - 实际上,没有这样的回调存在 - 代码是(目前,在我的调试代码中)由用户手动启动。

导致此错误的原因是什么?有什么方法可以避免它吗?

OSStatus result = MusicPlayerStop(g_player);

if (result != noErr)
    DebugLog("Error calling MusicPlayerStop");

UInt32 trackCount;
result = MusicSequenceGetTrackCount(g_sequence, &trackCount);
if (result != noErr)
    DebugLog("Error calling MusicSequenceGetTrackCount.");

while(trackCount > 0)
{
    MusicTrack track;
    result = MusicSequenceGetIndTrack (g_sequence, 0, &track);
    if (result != noErr)
        DebugLog("Error calling MusicSequenceGetIndTrack.");

    result = MusicSequenceDisposeTrack(g_sequence, track);
    if (result != noErr)
        DebugLog("Error calling MusicSequenceDisposeTrack.");

    result = MusicSequenceGetTrackCount(g_sequence, &trackCount);
    if (result != noErr)
        DebugLog("Error calling MusicSequenceGetTrackCount.");
}

result = DisposeMusicPlayer(g_player);
if (result != noErr)
    DebugLog("Error calling DisposeMusicPlayer.");
result = DisposeMusicSequence(g_sequence);
if (result != noErr)
    DebugLog("Error calling DisposeMusicSequence.");
result = DisposeAUGraph(g_processingGraph);
if (result != noErr)
    DebugLog("Error calling DisposeAUGraph.");

1 个答案:

答案 0 :(得分:0)

在iOS 9上运行时,通过重写我们的播放代码以使用较新的AVMIDIPlayer *代替MusicPlayer等来解决此问题。

*从iOS 8开始