3

Hey I'm trying to play a R.raw file thru my earpiece instead of the main phone speaker on my app but everyhting I throw at it isn't working. WIht my current code... it just plays very light static through the earpiece and not the R.raw I want.

Here is my code below and I set it in the manifest: <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

 AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            am.setSpeakerphoneOn(false);
        am.setMode(AudioManager.MODE_IN_CALL); 

        MediaPlayer tellSecret = MediaPlayer.create(this, R.raw.secret);
    tellSecret.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    tellSecret.start();

Any thoughts?

Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75
user961389
  • 423
  • 2
  • 8
  • 19
  • Did you get this to work? I notice you didn't accept the answer given, and I would love to know what you did do as I'm trying to do, well the opposite actually! - I want the sound to always use the loudspeaker :) – noelicus Oct 23 '13 at 14:58
  • @noelicus : Did you find a solution to your problem finally? I am trying to do what you did, but not able to find a solution. I asked a huge series of questions about work-arounds(http://stackoverflow.com/questions/20965530/how-to-mute-audio-in-headset-but-let-it-play-on-speaker-programmatically), but I am unable to find it. Please let me know. – SoulRayder Jan 07 '14 at 09:13
  • Hi @Gautham, yes I did - I've answered your question from your link now. Good luck! – noelicus Jan 07 '14 at 22:03

2 Answers2

1

Try setting the Audio Manager and the Media Player modes to be AudioManager.STREAM_MUSIC.

BC2
  • 892
  • 1
  • 7
  • 23
  • Or just make sure that both your AudioManager and the MediaPlayer are in the same mode. – BC2 Aug 06 '12 at 16:12
1

do not use 'create' api for media player, instead use 'setDataSource' and 'prepare' apis seperately. Also call 'setAudioStreamType' api on mediaplayer instance with STREAM_VOICE_CALL before calling 'setDataSource'.

In the end after starting media player, call setSpeakerphoneOn(false) on audio manager.