【问题标题】:Why my AVPlayer class reference in AppDelegate returns nil?为什么我在 AppDelegate 中的 AVPlayer 类引用返回 nil?
【发布时间】:2012-03-14 15:23:45
【问题描述】:

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

但是我对 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];      
    }
}

【问题讨论】:

    标签: iphone objective-c delegates avplayer audioqueue


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      MyAVPlayer* streamer = (MyAVPlayer *)inClientData;
      

      相反,您应该使用:

      self.myAVPlayer = (MyAVPlayer *)inClientData;
      

      【讨论】:

      • 它不允许我在 void AudioSessionInterruptionListenerCallBack (void *inClientData, UInt32 inInterruptionState) 函数中使用属性 myAVPlayer。
      • 我根据您的建议修改了我的代码,现在它可以工作了。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多