2011-03-10 49 views
1

我知道這個問題之前已經有所處理,但我覺得我們可以把這個問題稍微好一點。設置iPhone通過揚聲器開始播放,但允許耳機覆蓋

這是我現在的代碼。 iPhone會自動播放揚聲器(不是耳機),但是當我將耳機插入時沒有任何反應。我希望它能夠通過揚聲器進行播放,但如果插入耳機,則會通過耳機傳輸。我們如何更改此代碼以正確執行此操作?

// ORIGINAL CODE 
    UInt32 category = kAudioSessionCategory_PlayAndRecord; 
    error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category); 
    if (error) printf("couldn't set audio category!"); 

// I put this chunk in of code myself. To change the audio to go the speakers (not the earpiece). 
// I don't think this is the best way to do this.  


    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); 

謝謝!

回答

1

我就是這麼做的:

OSStatus error = AudioSessionInitialize(NULL, NULL, NULL, NULL); 

if (error) printf("ERROR INITIALIZING AUDIO SESSION! %d\n", error); 
else 
{ 
UInt32 category = kAudioSessionCategory_PlayAndRecord; 
// UInt32 category = kAudioSessionCategory_MediaPlayback; 

error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category); 
if (error) printf("couldn't set audio category!"); 


// It is bugs when I unplug the headphones! 
UInt32 doChangeDefaultRoute = 1;   
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute); 




error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self); 
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error); 
UInt32 inputAvailable = 0; 
UInt32 size = sizeof(inputAvailable); 

// we do not want to allow recording if input is not available 
error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable); 
if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error); 
btn_record.enabled = (inputAvailable) ? YES : NO; 

// we also need to listen to see if input availability changes 
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self); 
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error); 

error = AudioSessionSetActive(true); 
if (error) printf("AudioSessionSetActive (true) failed"); 
相關問題