简体   繁体   English

如何指定内部扬声器来播放音频文件?

[英]How can I specify internal speakers to play an audio file?

In my Android app, I want to play sounds on the phone internal speakers even when the user is connected to the device through a Bluetooth device or other external speakers.在我的 Android 应用程序中,即使用户通过蓝牙设备或其他外部扬声器连接到设备,我也想在手机内部扬声器上播放声音。 Can this be done?这可以做到吗?

I came up with way of playing audio on the phone speakers even when I have a connected Bluetooth audio device:即使连接了蓝牙音频设备,我也想出了在手机扬声器上播放音频的方法:

fun createPhoneSpeakerPreferredPlayer(context: Context, resource: Int): MediaPlayer{
    val mediaPlayer = MediaPlayer.create(context, resource)
    val audioDeviceType = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) AudioDeviceInfo.TYPE_BUILTIN_SPEAKER_SAFE else AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
    val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
    val devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)
    //create mapped devices variable to allow better logging.  The devices collection is not being serialized well (or at all)
    val mappedDevices = (devices.map {
        @Suppress("unused")
        object {
        val id = it.id
        val type = it.type
        val productName = it.productName
    }})
    val jsonRepresentation = Gson().toJson(mappedDevices)
    Log.d(Tag, "devices: $jsonRepresentation")
    val playbackDevice = devices.firstOrNull { it.type == audioDeviceType }
    Log.d(Tag, "selectedDevice: {id=${playbackDevice?.id}}, type=\"${playbackDevice?.type}\", productName=\"${playbackDevice?.productName}\"")
    mediaPlayer.preferredDevice = playbackDevice
    return mediaPlayer
}

to use that method and play audio:使用该方法并播放音频:

val mediaPlayer = createPhoneSpeakerPreferredPlayer(this.context, R.raw.myAudioId)
mediaPlayer.looping = true
mediaPlayer.start()

This seems to work if I have a Bluetooth speaker connected, but once I start playing audio in the background, like watching Netflix, or listening to a podcast, the audio no longer plays on the phone speaker.如果我连接了蓝牙扬声器,这似乎可以工作,但是一旦我开始在后台播放音频,比如看 Netflix 或收听播客,音频就不再在手机扬声器上播放。

Here is a sample app I made with two buttons.这是我用两个按钮制作的示例应用程序。 One to play music through the phone speaker and one to play through whatever speaker is connected:一种通过手机扬声器播放音乐,另一种通过连接的任何扬声器播放:

https://github.com/danwize/play-sound-android/blob/main/SoundPlayer/app/src/main/java/com/example/soundplayer/MainActivity.kt https://github.com/danwize/play-sound-android/blob/main/SoundPlayer/app/src/main/java/com/example/soundplayer/MainActivity.kt

I'm seeing that the first time I create a media player and play sound this way, the sound does play on the phone speakers.我看到我第一次创建媒体播放器并以这种方式播放声音时,声音确实会在手机扬声器上播放。 The second audio plays on the Bluetooth speaker.第二个音频在蓝牙扬声器上播放。 When I'm not playing any video or music, but still connected to a Bluetooth device, both my sounds play on the phone speakers.当我没有播放任何视频或音乐,但仍连接到蓝牙设备时,我的两种声音都会在手机扬声器上播放。 Here are the logs to show that I am correctly setting the preferred device:以下是显示我正确设置首选设备的日志:

2022-09-07 11:21:33.969 6101-6101/com.myApp D/MediaPlayerExtensions: devices: [{"id":2,"productName":"Pixel 3","type":1},{"id":3,"productName":"Pixel 3","type":2},{"id":11,"productName":"Pixel 3","type":18},{"id":369,"productName":"Jabra Elite 85h","type":8},{"id":363,"productName":"Jabra Elite 85h","type":7}]
2022-09-07 11:21:33.969 6101-6101/com.myApp D/MediaPlayerExtensions: selectedDevice: {id=3}, type="2", productName="Pixel 3"
2022-09-07 11:22:14.290 6101-6101/com.myApp D/MediaPlayerExtensions: devices: [{"id":2,"productName":"Pixel 3","type":1},{"id":3,"productName":"Pixel 3","type":2},{"id":11,"productName":"Pixel 3","type":18},{"id":369,"productName":"Jabra Elite 85h","type":8},{"id":363,"productName":"Jabra Elite 85h","type":7}]
2022-09-07 11:22:14.290 6101-6101/com.myApp D/MediaPlayerExtensions: selectedDevice: {id=3}, type="2", productName="Pixel 3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM