On the iphone there are two modes of playing the external voice: handset mode and microphone mode, the handset is of course the use of the phone, the voice of course is very small,
However, in the open, iOS default is this mode, so when the play outside should be added code to reset, as follows: Avaudioplayer * audioPlayer1 = [[Avaudioplayer alloc] Initwithcontentsofurl:soundurl Error:nil];
Audioplayer1.volume = 1.0;
[AudioPlayer1 Preparetoplay];
UInt32 Audioroute = Kaudiosessionoverrideaudioroute_speaker;
Audiosessionsetproperty (Kaudiosessionproperty_overrideaudioroute, sizeof (Audioroute), &audioroute);// These two lines of code are set to the microphone mode
[AudioPlayer1 play];
In turn a detailed: http://blog.csdn.net/xy5811/article/details/8563137
Select a category
AVAudioSessionCategoryAmbient
or kAudioSessionCategory_AmbientSound
--for non-voice-based applications, apps that use this category will be muted with the mute key and the screen off. It does not stop other apps from playing sound, and can play sounds with other apps such as Ipod,safari.
AVAudioSessionCategorySoloAmbient
or kAudioSessionCategory_SoloAmbientSound
--similar to avaudiosessioncategoryambient, the difference is that it stops other apps from playing sounds. This category is the default category.
AVAudioSessionCategoryPlayback
or kAudioSessionCategory_MediaPlayback
--for voice-based applications, apps that use this category do not mute with the mute key and the screen off.
AVAudioSessionCategoryRecord
Or
kAudioSessionCategory_RecordAudio———
For apps that need to be recorded, after setting the category, other system sounds except call tones, alarms or calendar reminders will not be played.
AVAudioSessionCategoryPlayAndRecord
or kAudioSessionCategory_PlayAndRecord
-for applications that need to play sound and require recording, voice chat applications (such as) should use this category.
AVAudioSessionCategoryAudioProcessing
or kAudioSessionCategory_AudioProcessing————
when you need to use this category for offline speech processing, I don't quite understand what the offline speech processing concept is, and I hope to have the knowledge to explain it.
Note : Not an app can only use a category, the program should be based on the actual need to switch the different category, for example, when recording, it needs to be set to Avaudiosessioncategoryrecord, When the recording is finished, you should change the category to Avaudiosessioncategoryambient according to the program One of the avaudiosessioncategorysoloambient or Avaudiosessioncategoryplayback.
Set category
- <span style= "FONT-SIZE:14PX;" >nserror *setcategoryerror = nil;
- BOOL&NBSP;SUCCESS&NBSP;=&NBSP;[[AVAUDIOSESSION&NBSP;SHAREDINSTANCE]&NBSP;&NBSP;
Li class= "alt" > setcategory: avaudiosessioncategoryambient
- error: &setcategoryerror];
- &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
- if (!success) { /* handle the error in setcategoryerror */ }</span>
Switch to Speaker
As Apple's official document says, it's only when the category is set to Avaudiosessioncategoryplayandrecord that the sound is played from the speakers, which I haven't confirmed yet.
In summary, according to the official documentation, first set the category to Avaudiosessioncategoryplayandrecord, and then redirect the audio by rewriting the audio route property.
The Audio Route property has the following two, one is the default handset and the other is the speaker.
- enum {
- Kaudiosessionoverrideaudioroute_none = 0,
- Kaudiosessionoverrideaudioroute_speaker = ' SPKR '
- };
There are two ways to Override audio route:
- UInt32 audiorouteoverride = Kaudiosessionoverrideaudioroute_speaker;
- Audiosessionsetproperty (
- Kaudiosessionproperty_overrideaudioroute
- sizeof (audiorouteoverride),
- &audiorouteoverride
- );
- UInt32 Dochangedefaultroute = 1;
- Audiosessionsetproperty (
- Kaudiosessionproperty_overridecategorydefaulttospeaker,
- sizeof (Dochangedefaultroute),
- &dochangedefaultroute
- );
The difference between the two methods is:
When using Kaudiosessionproperty_overrideaudioroute, the audio route will reset back to the handset when any interrupts such as plug-in headphones have occurred and you must set it again.
Use Kaudiosessionproperty_overridecategorydefaulttospeaker unless you change the category, it will remain in effect.
IOS Audio headset with speaker switch