Android 8通知设置声音无法工作。

27

我有以下代码,但每次我只听到默认的安卓声音。

        // 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


测试通过。在模拟器中运行正常。 - Arnav M.
我的模拟器和设备都无法工作。你在Android 8上测试过吗?请注意,我想加载自定义声音。 - Ricardo
我用了自定义的声音,我知道它是8。提供那个声音,我会试一下。 - Arnav M.
这是我的声音 http://pablomonteserin.es/aperturaabductores.wav - Ricardo
对我来说,不同之处在于将声音文件设置在 Android Oreo 的通道上。 - Pierre
2个回答

7
我使用了Audacity软件来比较你的音频文件和我的音频文件。你的音频文件采样率为22050Hz,而我使用的音频文件采样率为44100Hz。因此,我将你的音频文件采样率转换为44100Hz,并将其用作通知声音。现在它可以正常工作了。
问题出在音频文件上。可能是Android O中的新更改,因为在旧版本的Android上它能够正常工作。
以下是重新采样的方法-enter image description here

你能发布你的代码吗?因为我仍然在8.1.0版本上遇到了问题。 - Mehroze Yaqoob
我尝试了所有可能的解决方案,经过数小时的尝试,这个方法救了我!你救了我的命!! :) 记得在尝试后卸载应用程序,否则通知通道将无法更新...谢谢老兄! - Luca Biasotto
请问您能提供您的音频文件吗? - Dinesh.P

5
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification.Builder notificationBuilder =
                    new Notification.Builder(MyApplication.getInstance().getApplicationContext(), NOTIFICATION_CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(pTitle)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            //.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                            //.setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);

            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
            // Configure the notification channel.
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();
            notificationChannel.setSound(defaultSoundUri,att);
            notificationChannel.setDescription(messageBody);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
            if (imageThumbnail != null) {
                notificationBuilder.setStyle(new Notification.BigPictureStyle()
                        .bigPicture(imageThumbnail).setSummaryText(messageBody));
            }
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        } else {
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(MyApplication.getInstance().getApplicationContext())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(pTitle)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
            if (imageThumbnail != null) {
                notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(imageThumbnail).setSummaryText(messageBody));
            }
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        }

3
默认音频 URI 是什么? - Bipin Bharti
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - Prashant Gosai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接