1

I'm working on my app that plays a sound file through the front speaker or the rear speaker, and that can be changed 'on the fly'. Here is the code:

    // mediaPlayer was created previously and then started to play ok

    private void switchTarget() {
        int pos = 0;
        try {
            if (mediaPlayer.isPlaying()) {
                pos = mediaPlayer.getCurrentPosition();
                mediaPlayer.stop();
            }
            mediaPlayer.reset();
            FRONT = !FRONT;
            mediaPlayer.setAudioStreamType(FRONT ? AudioManager.STREAM_VOICE_CALL : AudioManager.STREAM_RING);
            mediaPlayer.setDataSource(WakeUp.this, mediaPath);
            mediaPlayer.prepare();
            mediaPlayer.seekTo(pos);
            mediaPlayer.start();
        } catch (Exception e) {Toast.makeText(context, e.toString(),Toast.LENGTH_LONG).show();}
    }

The point is that:

- if I start with FRONT = false, that is, the sound starts playing through the rear speaker, I can switch and the sound keeps playing by the frontal one, as expected.
- when FRONT is initially true, switching to the rear speakers results in no sound at all, but switching again recovers the sound via the front speaker.

Summing up, after having played the audio file in the front speaker, switching to the rear one has no effect. Furthermore, as the time-position in the audio file is kept through switches, it seems that time has elapsed while was working the rear speaker, although no sound can be heard.

For instance, I start on the front speaker for 10 seconds (sounds ok), switch to rear speaker for 5 seconds (no sound), switch back to front speaker (ok, and the file resumes at second 15). I change FRONT variable and run the app again, 10 seconds on rear speaker (working), switch to front speaker (working too) and back to rear speaker (no sound).

Maybe, somehow, when front speaker has worked and stops, blocks the rear one.

I have even tried to keep two mediaPlayer variables, one for each speaker, and playing and stopping alternatively, with the same bad results.

Thanks in advance!

dondindon
  • 11
  • 1

2 Answers2

0

Use audioManager.setSpeakerPhoneOn(true) instead. This is the intended API to use for switching between speakerphone and earphone.

Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53
0

I spoke too soon, not working.

New code:

// onCreate
FRONT = true;
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(WakeUp.this, mediaPath);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
try {
    mediaPlayer.prepare();
} catch (Exception e) {}
mediaPlayer.start();
...
...

private void switchTarget() {
    FRONT = !FRONT;
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setSpeakerphoneOn(!FRONT);
}

If speaker is initially on, onCreate plays sound through the rear speaker, and after switching, through the front one, all fine, but when switching again rear speaker does not work.

And, if speaker is initially off, onCreate uses the front speaker, sound works, but then I switch to rear speaker and don't hear anything. Then I switch back to front speaker and sounds ok.

I'm lost...

dondindon
  • 11
  • 1