IOS音频中断

IOS音频中断,ios,audio,interruption,Ios,Audio,Interruption,我正在使用Matt提供的旧AudioStreamer开发一个音频流应用程序,我正在尝试通过以下方式在接到电话时进行中断: - (void)MyAudioSessionInterruptionListener(void *inClientData, UInt32 inInterruptionState) { AudioStreamer *streamer = (AudioStreamer*)inClientData; if (inInterruptionState

我正在使用Matt提供的旧AudioStreamer开发一个音频流应用程序,我正在尝试通过以下方式在接到电话时进行中断:

- (void)MyAudioSessionInterruptionListener(void *inClientData, UInt32 inInterruptionState)
{
        AudioStreamer *streamer = (AudioStreamer*)inClientData;
        if (inInterruptionState == kAudioSessionBeginInterruption)
        {
            [streamer stop];    
            NSLog(@"kAudioSessionBeginInterruption");
        }
        else if (inInterruptionState == kAudioSessionEndInterruption)
        {
            [self playpause]; 
            NSLog(@"kAudioSessionEndInterruption");
        }
}
我的问题是我试图用[self playpause]调用函数playpause;但我得到了一个未声明的错误


如何在MyAudioSessionInterruptionListener中声明playpause?

它不是[self playpause]它应该是[streamer playpause],假设AudioStreamer类是具有该方法的类…listener方法是类外的静态C函数,因此不能在self上调用方法,因为self暗示您在类的实例中。如果使用该方法的类不是AudioStreamer,那么您必须在inClientData参数中传递该类,以便能够获得它


希望这有帮助

它不是[self playPause],它应该是[streamer playPause],假设AudioStreamer类是具有该方法的类……listener方法是类外的静态C函数,因此您不能在self上调用方法,因为self意味着您在该类的实例内。如果使用该方法的类不是AudioStreamer,那么您必须在inClientData参数中传递该类,以便能够获得它


希望这会有所帮助,所以在测试了所有可能性之后,最好使用通知

代码如下:

void MyAudioSessionInterruptionListener(void *inClientData, UInt32 inInterruptionState)
{
if (inInterruptionState == kAudioSessionBeginInterruption) {


    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopstreamer" object:nil];

    NSLog(@"kAudioSessionBeginInterruption");
}


else if (inInterruptionState == kAudioSessionEndInterruption) {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];

    NSLog(@"kAudioSessionEndInterruption");
}


}

因此,在测试了所有可能性之后,最好的方法是使用通知

代码如下:

void MyAudioSessionInterruptionListener(void *inClientData, UInt32 inInterruptionState)
{
if (inInterruptionState == kAudioSessionBeginInterruption) {


    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopstreamer" object:nil];

    NSLog(@"kAudioSessionBeginInterruption");
}


else if (inInterruptionState == kAudioSessionEndInterruption) {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];

    NSLog(@"kAudioSessionEndInterruption");
}


}

谢谢你这么快的回复。就像你说的,这个类不在AudioStreamer中,那么如何在inClientData参数中传递这个类呢?它只是个字节,所以也许数组可以满足你的需要,我可能会让AudioStreamer有一个代理,它会被通知中断,你可以让你的类成为代理,这样它会被通知中断,然后你可以调用AudioStreamer上的一个方法,它会调用代理方法谢谢你的快速回复。就像你说的,这个类不在AudioStreamer中,那么如何在inClientData参数中传递这个类呢?它只是个字节,所以也许数组可以满足你的需要,我可能会让AudioStreamer有一个代理,它会被通知中断,你可以让你的类成为代理,这样它会被通知中断,然后你可以在AudioStreamer上调用一个方法,它会调用代理方法这不是你问题的答案,你的问题的答案是我在上面发布的…你的问题是为什么你的代码行[self playpause]不起作用你是真的Daniel我的问题是关于[self playpause],谢谢你,因为你给了我正确的答案和细节,但我同时也在寻找解决方案,所以我只想分享为我工作的人的代码。。。如果有人需要。这不是你问题的答案,你问题的答案是我在上面发布的…你的问题是为什么你的代码行[self playpause]不起作用你是真的Daniel我的问题是关于[self playpause],谢谢你,因为你让我知道了你的答案和细节,但我同时也在寻找解决方案,所以我只想分享为我工作的代码。。。万一有人需要的话。