2013-10-31 42 views
19

Apple在蘋果開發者網站上沒有發佈任何其他代碼。AudioSessionInitialize的修復已過時?

+1

我認爲你必須使用AVAudioSession。但我不確定這是否也會初始化音頻會話。 – openfrog

+1

更正:蘋果文檔清楚地討論了棄用和它的替代在這裏https://developer.apple.com/library/ios/documentation/audiotoolbox/reference/audiosessionservicesreference/index.html – abbood

回答

7

您應該使用AVAudioSession。

以取代過時AudioSessionInitialize提供(例如,如果你需要指定AudioSessionInterruptionListener回調)的功能,你可以訂閱AVAudioSessionInterruptionNotification通知:喜歡處理:

- (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification 
{ 
    AVAudioSessionInterruptionType interruptionType = [[[notification userInfo] 
     objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue]; 
    if (AVAudioSessionInterruptionTypeBegan == interruptionType) 
    { 
    } 
    else if (AVAudioSessionInterruptionTypeEnded == interruptionType) 
    { 
    } 
} 
5

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidChangeInterruptionType:) 
     name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]]; 

和實現audioSessionDidChangeInterruptionType

的等效代碼
// C way 
UInt32 category = kAudioSessionCategory_MediaPlayback ; 
OSStatus result = AudioSessionSetProperty(
    kAudioSessionProperty_AudioCategory, sizeof(category), &category) ; 

if(result) // handle the error 

// Objective-C way 
NSError *nsError; 
[[AVAudioSession sharedInstance] 
    setCategory:AVAudioSessionCategoryPlayback error:&nsError]; 

if(nsError != nil) // handle the error 
+0

這樣做會給出錯誤「使用未聲明的標識符AVAudioSession」 –

+1

將AVFoundation添加到框架幷包含在要修復的頭文件中。 –

6

1. 此代碼

AudioSessionInitialize(NULL, NULL, interruptionCallback, self); 

[[AVAudioSession sharedInstance] setActive:YES error:nil]; 

2. 更換來回此代碼

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; 
AudioSessionSetProperty(
     kAudioSessionProperty_AudioCategory, 
     sizeof(sessionCategory), 
     &sessionCategory 
     ); 

更換

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; 
[[AVAudioSession sharedInstance] 
    setCategory:sessionCategory error:nil]; 
+3

第二個代碼快照的第一行應該改爲'NSString * sessionCategory = AVAudioSessionCategoryPlayAndRecord;' – deko

0
public void EnableSpeaker(bool speaker) 
{ 
    if (AVAudioSession.SharedInstance().SetActive(true, out NSError outError)) 
    { 
    AudioSession.OverrideCategoryDefaultToSpeaker = speaker; 
    } 
} 
相關問題