iOS Cannot play on speaker and set Audio Session to record
Desired functionality: 1) play audio over headphones when plugged in, 2) play audio over speaker when headphones not plugged in, and 3) record audio either through headphones or internal mic.
In order to get it to play audio on the speaker and headphones I use the following audio session type:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: &setCategoryError];
And then create an instance of audio player. This works great to play audio over headphones when plugged in and speaker when not, until I try to record.
When recording, I first set the audio session to AVAudioSessionCategoryPlayAndRecord
and then try to record with Novicane. Sometimes this succeeds, but other times the app crashes with no stack trace or debugger indication and gives a AUIOClient_StartIO
error.
My Googling of this error suggests that I need to keep the session AVAudioSessionCategoryPlayAndRecord
at all times. This does seem to keep the AUIOClient_StartIO
error from happening.
But whenever I do playback with AVAudioSessionCategoryPlayAndRecord
it starts playing out of the built-in earpiece instead of the speaker.
I know I can override the audio output channel to speaker (eg https://stackoverflow.com/a/1064941/895876), but then plugging in headphones does not automatically switch over to headphone audio.
How do I get all three features?
You have to use the AUHAL unit to set a specific input device as default input and then connect it with the AudioQueue.
Check out this Apple tech. note, it might help you..
If that doesn't work try AVAudioSession
using setPreferredInput:
This is off the back of my head, based on something i read recently, I didn't actually try it, but know that others succeeded trying it.
good luck.
链接地址: http://www.djcxy.com/p/79034.html