2012-03-14 27 views
0

我试图处理音频流中断,当用户收到一个电话音频暂停,然后它应该恢复时,通话结束。为什么我的AVPlayer类在AppDelegate中引用返回nil?

但我的参考到我MyAVPlayer类返回零,在下面所示的代码[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];  
    } 
} 

回答

1

的问题是,你有一个名为myAVPlayer属性,但该变量,你正在使用线路分配:

MyAVPlayer* streamer = (MyAVPlayer *)inClientData; 

相反,你应该使用:

self.myAVPlayer = (MyAVPlayer *)inClientData; 
+0

它不会让我在void AudioSessionInterruptionListenerCallBack(void * inClientData,UInt32 inInterruptionState)函数内使用属性myAVPlayer。 – Winston 2012-03-14 17:40:32

+0

我根据您的建议修改了我的代码,现在它正在工作。谢谢! – Winston 2012-03-14 19:04:08

2

这是因为你不实际上将实例变量分配给任何东西!

相关问题