问题
I am looking for a way to play audio from an Android device's speaker, even when headphones are inserted.
In fact, the typical behavior is that, when headphones are inserted, no audio is outputted form the speakers. However some apps, such the default Clock app (com.google.android.deskclock), are able to route the audio to the speaker even when the headphones are inserted.
How can I programmatically get this behavior?
I am looking for a solution that works (at least) on Nexus 5 devices, running KitKat (Android 4.4)
Thanks.
回答1:
Probably a look in the sourcecode of this app will help. I found a class AsyncRingtonePlayer (https://android.googlesource.com/platform/packages/apps/DeskClock/+/master/src/com/android/deskclock/AsyncRingtonePlayer.java) which might do the work.
The trick is to use a different Audiostream, I guess. Lets have a look a the startAlarm-method:
private void startAlarm(MediaPlayer player) throws IOException {
// do not play alarms if stream volume is 0 (typically because ringer mode is silent).
if (mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
if (Utils.isLOrLater()) {
player.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
}
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_ALARM,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
player.start();
}
}
The audio stream type of the player object will be set to AudioManager.STREAM_ALARM. I guess that this could be the solution. I do not tested it, but this the first thing I found in the source code.
Please be also aware of some needed permissions as listed in the AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<!-- WRITE_SETTINGS is required to record the upcoming alarm prior to L -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- READ_PHONE_STATE is required to determine when a phone call exists prior to M -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- READ_EXTERNAL_STORAGE is required to play custom ringtones from the SD card prior to M -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
(https://android.googlesource.com/platform/packages/apps/DeskClock/+/master/AndroidManifest.xml)
回答2:
Removing code not necessary for Android 4.4
//context = a valid context
AudioManager am = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
MediaPlayer mp = new MediaPlayer();
Uri loc = Uri.parse("android.resource://com.example.test/" + R.raw.music);
try {
mp.setDataSource(context, loc);
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(true);
mp.prepare();
} catch (IllegalArgumentException | SecurityException| IllegalStateException | IOException e) {
e.printStackTrace();
}
am.requestAudioFocus(null, AudioManager.STREAM_ALARM,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
mp.start();
来源:https://stackoverflow.com/questions/33429701/android-how-to-route-the-audio-to-speakers-when-headphones-inserted