android - 如何确保我的通知声音在所有设备上可靠播放?

标签 android audio notifications firebase-cloud-messaging

我的应用设置了两个通知 channel ,每个 channel 都有自己的音效。但用户说声音在某些设备上无法正常工作,比如 Pixel。这是我的设置。我可以做些什么来改善 channel 设置以确保声音(保存在 res/raw/中的两个 .mp3 文件)尽可能可靠地播放(作为背景通知,而不是前景)?我是否缺少更好的音频设置或格式或配置?

我正在使用 Firebase 云消息传递。这是创建 channel 的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH

            val audioAttributes = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setFlags(FLAG_AUDIBILITY_ENFORCED)
                .build()

            val newChannel = NotificationChannel(channelId, name, importance)
            newChannel.description = description
            newChannel.importance = IMPORTANCE_HIGH

            newChannel.setSound(sound, audioAttributes)
            return newChannel
}

下面是获取声音 URI 的代码:
private fun createSoundUri(soundNum: Int, context: Context):Uri? {
val scheme = ContentResolver.SCHEME_ANDROID_RESOURCE
val packageName = context.packageName
val root = "$scheme://$packageName/"

return when(soundNum) {
    0 -> Uri.parse(root + R.raw.sound1)
    1 -> Uri.parse(root + R.raw.sound2)
    2 -> Settings.System.DEFAULT_NOTIFICATION_URI
    else -> null
    }
}

最佳答案

我使用 FCM 接收数据,然后在我的应用程序中启动了启动媒体播放器的 Activity 。
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.ringtone); mediaPlayer.setLooping(true); mediaPlayer.start();
也许你可以使用这个想法

关于android - 如何确保我的通知声音在所有设备上可靠播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62320516/

相关文章:

jquery - 互动音频进度栏html5

java - MediaPlayer和AudioTrack不会输出相同的声音

android - Android 上的简单速度测试应用程序建议

android - 如何知道在 Action 模式中何时按下后退箭头

android - 应用程序是否有像 IOS 供应商 id 这样的唯一 id?

javascript - 需要背景声音+播放一次

windows-phone-7 - 如何安排在指定时间向用户发送通知

android - 是否可以在 android 通知文本中显示笑脸/表情/图像跨度?

ios - 如何禁用 iOS 远程通知的默认通知警报 View ?

android - 问题 : Android's isConnected() used to get current state of WiFi often returns false even when the device is connected