我試圖處理音頻流中斷,當用戶收到一個電話音頻暫停,然後它應該恢復時,通話結束。爲什麼我的AVPlayer類在AppDelegate中引用返回nil?
但我的參考到我MyAVPlayer
類返回零,在下面所示的代碼[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];
}
}
它不會讓我在void AudioSessionInterruptionListenerCallBack(void * inClientData,UInt32 inInterruptionState)函數內使用屬性myAVPlayer。 – Winston 2012-03-14 17:40:32
我根據您的建議修改了我的代碼,現在它正在工作。謝謝! – Winston 2012-03-14 19:04:08