2012-03-14 103 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

這是因爲你不實際上將實例變量分配給任何東西!

相關問題