2011-03-10 95 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"); 
相关问题