So I'm working on an alarm clock app and I have a trouble with making exoplayer to use speakerphone when I have earphones jack in the device. I found this solution, but as comments says it's not actually a reliable way to do this. I also found that somebody had a similar problem here and that there is a method SimpleExoPlayer.setAudioStreamType()
, but unfortunatelly it doesn't work in ExoPlayer 2.0.4. Any ideas?
1 Answers
Alright, I found a solution! When you create an exoPlayer you can either use the default renderers (that is what ExoPlayerFactory.newSimpleInstance()
for) or create your own renderers to do something specific. The problem with the default audio renderer (which is MediaCodecAudioRenderer
) is that it has AudioManager.STREAM_MUSIC
set by default and ExoPlayer 2.x doesn't have any methods to change it (although there was such a method in ExoPlayer 1.x).
In order to fix that I had to create my own audio renderer and pass AudioManager.STREAM_ALARM
as one of its parameters. After that, you can use ExoPlayerFactory.newInstance()
to and pass the created renderer to it.
Here's a class that you can use to get an instance of ExoPlayer that will always play music through the speaker phone. It will also use the alarm volume level.
public class AlarmExoPlayerFactory {
public static ExoPlayer newInstance() {
Renderer[] renderers = new Renderer[1];
Renderer audioRenderer = new MediaCodecAudioRenderer(MediaCodecSelector.DEFAULT, null, true,
null, null, null, AudioManager.STREAM_ALARM);
renderers[0] = audioRenderer;
Handler handler = new Handler();
TrackSelector trackSelector = new DefaultTrackSelector(handler);
return ExoPlayerFactory.newInstance(renderers, trackSelector);
}
}

- 820
- 2
- 14
- 22