为什么AppDelegate中的AVPlayer类引用返回nil?

时间:2012-03-14 15:23:45

标签: iphone objective-c delegates avplayer audioqueue

我试图处理音频流中断,当用户收到呼叫音频暂停时,它应该在呼叫结束时恢复。

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

为什么它是nil,因为我播放了音频?有没有更好的方法呢?

我在AppDelegate.h中有一个自定义类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];      
    }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

MyAVPlayer* streamer = (MyAVPlayer *)inClientData;

相反,你应该使用:

self.myAVPlayer = (MyAVPlayer *)inClientData;