我正在尝试处理音频流中断,当用户接到呼叫时,音频会暂停,然后在呼叫结束时应该会恢复。
但是,在下面显示的代码[myAVPlayer pauseAACStreaming:self];
和[myAVPlayer playACCStreaming:self];
中,我对MyAVPlayer
类的引用返回nil。
既然我有音频播放,为什么是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];
}
}
这是因为您实际上没有将实例变量分配给任何东西!
问题是您有一个名为myAVPlayer
的属性,但您使用以下行分配的变量:
MyAVPlayer* streamer = (MyAVPlayer *)inClientData;
相反,您应该使用:
self.myAVPlayer = (MyAVPlayer *)inClientData;