2012-03-14 2 views
0

오디오 스트리밍 중단을 처리하려고하는데 사용자가 오디오 일시 중지 호출을 받으면 호출이 끝날 때 다시 시작해야합니다.AppDelegate에서 내 AVPlayer 클래스 참조가 nil을 반환하는 이유는 무엇입니까?

MyAVPlayer 클래스에 대한 참조는 아래 코드 줄 [myAVPlayer pauseAACStreaming:self];[myAVPlayer playACCStreaming:self];에 nil을 반환합니다.

왜 내가 오디오를 재생했기 때문에 nil입니까? 그것을 할 수있는 더 좋은 방법이 있습니까? 내가 지금처럼, 사용자 정의 클래스 MyAVPlayer 내 AppDelegate.ha의 참조가

: 다음

@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

+0

귀하의 제안에 따라 코드를 수정했으며 현재 작동 중입니다. 감사! – Winston

2

그것은이다 인스턴스 변수를 실제로 할당하는 것!