7

There's a weird bug in my app. I'm using AVAudioPlayer to play sounds (multiple instances of it), the sound works perfectly through headphones, but using the app without headphones produces no sound from the speaker. All of the audio clips are AAC encoded.

I have tried setting the AVAudioSession properties both through the Objective-C API ([AVAudioSession sharedInstance]) and the C API, but none of the the options seem to work.

SiimKallas
  • 934
  • 11
  • 23

3 Answers3

20

accepted answer (in ios7) did not work for me, the following code did work. (i do not have enough points to comment, posting this as separate answer)

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                 withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
                                       error:nil];
tmr
  • 1,500
  • 15
  • 22
  • +1 ! This also helped me with a semi-related issue with AVSpeechUtterance volume being extremely low. Thank you very much! See http://stackoverflow.com/a/28922543/2578205 – Glavin001 Mar 08 '15 at 02:50
5

ISSUE on iPhone - when sound was very low - seemed it was coming out the phone ear piece (not headphone jack) instead of bottom speaker - change to DEFAULT TO SPEAKER

//http://stackoverflow.com/questions/3104562/avaudiorecorder-avaudioplayer-sound-output-on-internal-speaker-how-to-chang


UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (
    kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
    sizeof (doChangeDefaultRoute),
    &doChangeDefaultRoute
);
Amar Singh
  • 5,464
  • 2
  • 26
  • 55
brian.clear
  • 5,277
  • 2
  • 41
  • 62
3

For those of you using Swift Syntax...

Below worked for me.

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker)
   } 
catch {
          print("can't default to speaker ")
      }
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63