views:

191

answers:

2

Hello I got some questions on playback music via speaker. i found an example in the following link http://stackoverflow.com/questions/1022992/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)!!

can somebody help me?

A: 

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.

iWasRobbed
A: 

Thanks for your reply. but i still don't get it. 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?

Alex