0

我正在尝试处理音频流中断,当用户接到电话时音频暂停,然后它应该在通话结束后恢复。

但是我对我的MyAVPlayer类的引用返回 nil,在这些代码行中[myAVPlayer pauseAACStreaming:self];[myAVPlayer playACCStreaming:self];如下所示。

为什么会这样nil,因为我已经播放了音频?有更好的方法吗?

我在我的 AppDelegate.ha 中引用了一个自定义类 MyAVPlayer,如下所示:

@class MyAVPlayer;

@interface AppDelegate : NSObject <UIApplicationDelegate> 

{

   MyAVPlayer *myAVPlayer;

} 

@property (nonatomic, retain)  MyAVPlayer *myAVPlayer;

然后,在 AppDelegate.m 我有:

#import "MyAVPlayer.h"

void AudioSessionInterruptionListenerCallBack(void *inClientData, UInt32 inInterruptionState);

@implementation AppDelegate

@synthesize myAVPlayer;

void AudioSessionInterruptionListenerCallBack (void *inClientData, UInt32 inInterruptionState)
{
    NSLog(@"Audio session interruption");

    MyAVPlayer* streamer = (MyAVPlayer *)inClientData;
    [streamer handleInterruptionChangeToState:inInterruptionState];
}

- (void)applicationWillResignActive:(UIApplication *)application
{

    AudioSessionInitialize (
                            NULL,                         
                            NULL,                          
                            AudioSessionInterruptionListenerCallBack,  
                            self                       
                            );
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{

    AudioSessionInitialize (
                            NULL,                         
                            NULL,                          
                            AudioSessionInterruptionListenerCallBack,  
                            self                       
                            );
}


- (void)handleInterruptionChangeToState:(AudioQueuePropertyID)inInterruptionState 
{

     NSLog(@"handleInterruptionChangeToState");

    if (inInterruptionState == kAudioSessionBeginInterruption)
    { 
        [myAVPlayer pauseAACStreaming:self];  
    }

    else if (inInterruptionState == kAudioSessionEndInterruption) 
    {
        AudioSessionSetActive( true );

               [myAVPlayer playACCStreaming:self];      
    }
}
4

2 回答 2

2

这是因为您实际上并没有将实例变量分配给任何东西!

于 2012-03-14T15:34:16.927 回答
1

问题是您有一个名为 的属性myAVPlayer,但是您使用以下行分配的变量:

MyAVPlayer* streamer = (MyAVPlayer *)inClientData;

相反,您应该使用:

self.myAVPlayer = (MyAVPlayer *)inClientData;
于 2012-03-14T15:49:23.723 回答