我正在尝试处理音频流中断,当用户接到电话时音频暂停,然后它应该在通话结束后恢复。
但是我对我的MyAVPlayer
类的引用返回 nil,在这些代码行中[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];
}
}