2

我有一个奇怪的问题,当我从设备上的 XCode 构建和运行我开发的 iOS 应用程序时,它运行良好。但是,在重新启动设备并运行应用程序时,我根本听不到音频。如果我随后终止该应用程序并从设备重新启动它,我将再次开始获取音频。我最初认为这是一个中断处理程序问题,但现在不确定了。任何帮助,将不胜感激!

这是我的中断处理程序以防万一。

static void MyInterruptionListener (void *inUserData,
                                UInt32 inInterruptionState) {

printf ("Interrupted! inInterruptionState=%ld\n", inInterruptionState);
pediViewController *pediController = (__bridge pediViewController*)inUserData;
switch (inInterruptionState) {
    case kAudioSessionBeginInterruption:
        CheckError (AudioOutputUnitStop (pediController.effectState.rioUnit),
                    "Couldn't start RIO unit");
    case kAudioSessionEndInterruption:
        // TODO: doesn't work!
        CheckError(AudioSessionSetActive(true),
                   "Couldn't set audio session active");
        CheckError (AudioOutputUnitStart (pediController.effectState.rioUnit),
                    "Couldn't start RIO unit");
        break;
    default:
        break;
};

}

4

2 回答 2

1

您应该使用音频路由更改回调而不是中断处理程序来处理进出后台的应用程序。

无论如何,当我遇到与您类似的问题时,这对我有用(如果我正确理解您的问题)。

这是一个例子

AudioSessionAddPropertyListener (
                                     kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                     (__bridge void*) self
                                     );

void audioRouteChangeListenerCallback (
                                       void                      *inUserData,
                                       AudioSessionPropertyID    inPropertyID,
                                       UInt32                    inPropertyValueSize,
                                       const void                *inPropertyValue
                                       ) {

    // Ensure that this callback was invoked because of an audio route change
    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;


    AudioEngine *audioObject = (__bridge AudioEngine *) inUserData;

    // if application sound is not playing, there's nothing to do, so return.
    if (NO == audioObject.isPlaying) {

        NSLog (@"Audio route change while application audio is stopped.");
        return;

    } else {

        CFDictionaryRef routeChangeDictionary = inPropertyValue;

        CFNumberRef routeChangeReasonRef =
        CFDictionaryGetValue (
                              routeChangeDictionary,
                              CFSTR (kAudioSession_AudioRouteChangeKey_Reason)
                              );

        SInt32 routeChangeReason;

        CFNumberGetValue (
                          routeChangeReasonRef,
                          kCFNumberSInt32Type,
                          &routeChangeReason
                          );
             if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {

            NSLog (@"Audio output device was removed; stopping audio playback.");
            NSString *MixerHostAudioObjectPlaybackStateDidChangeNotification = @"MixerHostAudioObjectPlaybackStateDidChangeNotification";
            [[NSNotificationCenter defaultCenter] postNotificationName: MixerHostAudioObjectPlaybackStateDidChangeNotification object: audioObject]; 

        } else {

            NSLog (@"A route change occurred that does not require stopping application audio.");
        }
    }
}
于 2012-09-11T14:52:02.227 回答
0

升级到 iOS 6 后问题就消失了。我通过注释掉音频回调中的每一行代码进行了一些密集的调试,并通过重新启动 iPad 并再次运行应用程序 3 次来进行测试。最后,它通过在设备上更新到 iOS 6 并在其上运行来解决。这样做我也得到了巨大的性能提升!

于 2012-09-12T08:50:42.160 回答