2

I am developing an application in which I want to redirect audio output to phone speaker, but I don't have idea how to do this programmatically.

Any idea about it?

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
Prachi Rajput
  • 502
  • 1
  • 8
  • 20
  • Can you please elaborate? What do you mean "used as an on/off state"? You have it either playing a constant sound or not playing anything? Are you trying to use the silence switch as input to your program? – WolfLink Mar 21 '14 at 05:31
  • Speaker on/off at the moment it's playing the audio through the speaker which is at the bottom of the phone (loudspeaker) however it can also play via the small speaker you listen to when you are talking on the phone. – Prachi Rajput Mar 21 '14 at 05:38
  • I want to place a button flips between the two. I.e Headset and Speaker. – Prachi Rajput Mar 21 '14 at 05:38
  • Using the default speaker is not too hard to do so I will assume you are not having trouble with that. As for using the upper speaker, check out this question: http://stackoverflow.com/questions/18026578/play-audio-through-upper-phone-call-speaker – WolfLink Mar 21 '14 at 07:34

2 Answers2

1

You cannot turn ON/OFF speakers through your app. However, you can sense the mute switch of your device. By that you can know the current status of speaker. This Question will lead you towards the solution.

For sake of completeness, let me paste the answer here..

// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
    AudioSessionInterruptionListener    inInterruptionListener = NULL;
    OSStatus    error;
    if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
    {
        NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
    }
    else
    {
        gAudioSessionInited = YES;
    }
}

SInt32  ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
    NSLog(@"*** Error *** could not set Session property to ambient.");
}

Update: You need to override AudioRoute to turn on speakers when headset is plugged in. Use following code for the same.

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

Be sure to include the AudioToolbox lib in Xcode. I think it will solve your issue..

Let me know if more info needed..:)

Community
  • 1
  • 1
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • Thanks for your reply, actually i want to place a button flips between the two. I.e Headset and Speaker. – Prachi Rajput Mar 21 '14 at 05:25
  • @PrachiRajput.. then better look at [this question](http://stackoverflow.com/questions/18026578/play-audio-through-upper-phone-call-speaker) – Prince Agrawal Mar 21 '14 at 05:39
0
 -(IBAction)playAUdio
{
    if(ishandset)
    {

        AVAudioSession* session = [AVAudioSession sharedInstance];
        BOOL success;
        NSError* error;
        success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                                 error:&error];
              success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                             error:&error];
        success = [session setActive:YES error:&error];


    }
    else
    {

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

           }
    ishandset=!ishandset;
}
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86