2

I am using MediaPlayer class to play sound. But there is an issue in my code. If headset is connected is connected to my device and then it play sound on headset, not on device speaker.

So my question is how to play sound both in device speaker and headset, if headset is still connected to the device.

Code used to play sound

m_MediaPlayer = MediaPlayer.create(SettingsAlarmToneActivity.this, R.raw.tone1);
m_MediaPlayer.start();
Rahul
  • 1,667
  • 6
  • 21
  • 38
  • I think you can't because audio shouldn't go by speaker if headset is plugged in. – Max Oct 08 '13 at 07:12
  • Then how the caller tone ring in device speaker if the headset is plugged in. – Rahul Oct 08 '13 at 07:20
  • It is android build-in, I think you will need root access. – Max Oct 08 '13 at 07:23
  • @MaxMommersteeg Could u tell me how to do that. – Rahul Oct 08 '13 at 07:36
  • Do you want to play the same sound in both devices, or different sounds? In the first case, use the `RING` or `ALARM` stream type for your player. In the second, see http://stackoverflow.com/questions/14210696/simultaneously-using-a-headphone-and-speaker/14211450#14211450 – Michael Oct 08 '13 at 07:36
  • In my case i playing the same sound in both. – Rahul Oct 08 '13 at 07:37
  • Then it should be sufficient to set one of the stream types I mentioned using `setAudioStreamType`. You'll probably have to use a different way of constructing your `MediaPlayer` object, because `create` calls `prepare` internally, and `setAudioStreamType` has to be called before `prepare`. – Michael Oct 08 '13 at 07:41
  • @Michael Can u show me with an example...I am not able to get what need to do. – Rahul Oct 08 '13 at 07:45
  • I don't have an example, but I'm sure you can find some if you google for it. Or just follow [the documentation](http://developer.android.com/reference/android/media/MediaPlayer.html). – Michael Oct 08 '13 at 07:47
  • @Michael No suceess yet.. i tried using this line of code m_MediaPlayer = MediaPlayer.create(SettingsAlarmToneActivity.this, R.raw.tone1); m_MediaPlayer.setAudioStreamType(AudioManager.STREAM_RING); – Rahul Oct 08 '13 at 08:00

1 Answers1

0

The MediaRouter is Androids way to route audio signals to different devices, like internal speaker, headphone or Bluetooth speakers.

You'll need to run on API Level 16 or above (Android 4.1+). Here's, what the developers page says:

Media router. The new APIs MediaRouter, MediaRouteActionProvider, and MediaRouteButton provide standard mechanisms and UI for choosing where to play media.

To be honest, I haven't seen any example, where two actual devices were fired with an Audio signal, only that two MediaPlayer can now gapless play after each other.

jboi
  • 11,324
  • 4
  • 36
  • 43