Android 8通知setSound无法正常工作 [英] Android 8 notifications setSound not working
问题描述
我有以下代码,但每次我只听到默认的android声音.
I have the following code but everytime I just hear the default android sound.
// create channel
NotificationChannel channel = new NotificationChannel(ANDROID_CHANNEL_ID,
ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
// Sets whether notifications posted to this channel should display notification lights
channel.enableLights(true);
// Sets whether notification posted to this channel should vibrate.
channel.enableVibration(true);
// Sets the notification light color for notifications posted to this channel
channel.setLightColor(Color.GREEN);
// Sets whether notifications posted to this channel appear on the lockscreen or not
//channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/" + R.raw.aperturaabductores);
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel.setSound(uri,att);
这是我的声音 pablomonteserin.es/aperturaabductores.wav
推荐答案
我试图查看您的声音文件和我的声音文件之间的区别.我使用了Audacity软件. 您的声音文件的采样率为22050Hz,而我使用的声音文件的采样率为44100Hz.因此,我将您的声音文件采样率转换为44100Hz,并将其用作通知声音.现在可以了.
I tried to see the difference between your sound file and mine. I used Audacity software. Your sound file has sampling rate 22050Hz while the sound files i use are sampled at 44100Hz. So i converted your sound file sampling rate to 44100Hz and used that as notification sound. Now it works.
问题出在声音文件上.可能是Android O的新变化,因为它在较旧的Android版本上可以正常工作.
The problem is with the sound file. May be it's new change in Android O because it's working fine on older Android version.
这是重新采样的方法-
This is how to resample-
这篇关于Android 8通知setSound无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!