0

I got some questions on playback music via speaker. i found an example in the following link How to get AVAudioPlayer output to the speaker but the question is how to make sure i successfully implemented playing music via "speaker"? I wrote the code as the link, but it seems no difference before and after i activating the "speaker" on iphone simulator (on macbook)!!

Update:

below is the way i activate the speaker.

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord ;    // 1
    
AudioSessionSetProperty (
        kAudioSessionProperty_AudioCategory,                        // 2
        sizeof (sessionCategory),                                   // 3
        &sessionCategory                                            // 4
    );

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;  // 1
 
AudioSessionSetProperty (
    kAudioSessionProperty_OverrideAudioRoute,                         // 2
    sizeof (audioRouteOverride),                                      // 3
    &audioRouteOverride                                               // 4
);

below is the way i deactivate the speaker

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;  // 1
 
AudioSessionSetProperty (
    kAudioSessionProperty_OverrideAudioRoute,                         // 2
    sizeof (audioRouteOverride),                                      // 3
    &audioRouteOverride                                               // 4
);

when i tried to check the content of kAudioSessionProperty_AudioRoute as NSLog(@"%@",kAudioSessionProperty_AudioRoute ); ,the simulator crashed.

I look up the documentation, CFStringRef is almost the same with NSString type. Therefore, it is reasonable to use NSLog to print the value of kAudioSessionProperty_AudioRoute.

As you said, kAudioSessionProperty_AudioRoute is supposed to be "headphone" or "speaker" I still can not make if the code i paste is right and the way i activated speaker is right. Can you help me?

2 Answers 2

3

Your macbook only has one set of speakers, so you'll only hear it from that. The phone has both the receiver earpiece and the speaker on the bottom (which you wanna use).

Just check what kAudioSessionProperty_AudioRoute is set as.

Apple states:

kAudioSessionProperty_AudioRoute... The name of the current audio route (such as “Headphone,” “Speaker,” and so on). A read-only CFStringRef value.

More info about the override to speaker property:

This property can be used only with the kAudioSessionCategory_PlayAndRecord (or the equivalent AVAudioSessionCategoryRecord) category. (...) By default, output audio for this category goes to the receiver—the speaker you hold to your ear when on a phone call. The kAudioSessionOverrideAudioRoute_Speaker constant lets you direct the output audio to the speaker situated at the bottom of the phone.

kAudioSessionProperty_OverrideCategoryDefaultToSpeaker .. Specifies whether or not to route audio to the speaker (instead of to the receiver) when no other audio route, such as a headset, is connected. By default, the value of this property is FALSE (0). A read/write UInt32 value.

This property retains its value through an audio route change (such as when plugging in or unplugging a headset), and upon interruption; it reverts to its default value only upon an audio session category change. This property can be used only with the kAudioSessionCategory_PlayAndRecord (or the equivalent AVAudioSessionCategoryRecord) category.

See also kAudioSessionProperty_OverrideAudioRoute.

1

I had the same problem ;

but finally fixed by overriding this way ....

void EnableSpeakerPhone ()
{
UInt32 dataSize = sizeof(CFStringRef);
CFStringRef currentRoute = NULL;
OSStatus result = noErr;

AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &dataSize, &currentRoute);

// Set the category to use the speakers and microphone.
UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
result = AudioSessionSetProperty (
                                  kAudioSessionProperty_AudioCategory,
                                  sizeof (sessionCategory),
                                  &sessionCategory
                                  );
assert(result == kAudioSessionNoError);

Float64 sampleRate = 44100.0;
dataSize = sizeof(sampleRate);
result = AudioSessionSetProperty (
                                  kAudioSessionProperty_PreferredHardwareSampleRate,
                                  dataSize,
                                  &sampleRate
                                  );
assert(result == kAudioSessionNoError);

// Default to speakerphone if a headset isn't plugged in.
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
dataSize = sizeof(route);
result = AudioSessionSetProperty (
                                  // This requires iPhone OS 3.1
                                  kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
                                  dataSize,
                                  &route
                                  );
assert(result == kAudioSessionNoError);

AudioSessionSetActive(YES);
}

then .. I created a new method called ( void DisableSpeakerPhone(); ) to reverse the (EnableSpeakerPhone) method

void DisableSpeakerPhone ()
{
UInt32 dataSize = sizeof(CFStringRef);
CFStringRef currentRoute = NULL;
OSStatus result = noErr;

AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &dataSize, &currentRoute);

// Set the category to use the speakers and microphone.
UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
result = AudioSessionSetProperty (
                                  kAudioSessionProperty_AudioCategory,
                                  sizeof (sessionCategory),
                                  &sessionCategory
                                  );
assert(result == kAudioSessionNoError);

Float64 sampleRate = 44100.0;
dataSize = sizeof(sampleRate);
result = AudioSessionSetProperty (
                                  kAudioSessionProperty_PreferredHardwareSampleRate,
                                  dataSize,
                                  &sampleRate
                                  );
assert(result == kAudioSessionNoError);

// Default to speakerphone if a headset isn't plugged in.
// Overriding the output audio route
// The Trick is here

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;  
dataSize = sizeof(audioRouteOverride);
AudioSessionSetProperty(
                        kAudioSessionProperty_OverrideAudioRoute,
                        dataSize,
                        &audioRouteOverride);

assert(result == kAudioSessionNoError);

AudioSessionSetActive(YES);
}

Now , Make the switcher or any btn call the methods directly ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.