简体   繁体   English

Android:自定义通知声音无法播放

[英]Android: Custom notification sound not playing

I've tried pretty much all available answers on the site, but somehow I cannot get the notification sound to work. 我已经在网站上尝试了几乎所有可用的答案,但不知怎的,我无法获得通知声音。 The current code I'm testing is this (the notification is built in an alarm reciever): 我正在测试的当前代码是这样的(通知内置在警报接收器中):

public class AlarmReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 0;

@Override
public void onReceive(Context context, Intent intent) {


  NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity_.class), 0))
            .setContentText("temporary text")
            .setAutoCancel(true)
            .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                    + "://" + context.getPackageName() + "/raw/alert"))
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_stat_notification);

    Notification notification = builder.build();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notification);

The sound however, can be played via MediaPlayer, so the format is not the issue here. 然而,声音可以通过MediaPlayer播放,因此这里的格式不是问题。 Could it have to do with the lenght of the sound (30 seconds)? 这可能与声音的长度(30秒)有关吗? Thank you for your help! 谢谢您的帮助!

Try changing your .setSound() to this 尝试将.setSound()更改为此

.setSound(Uri.parse("android.resource://"
                            + context.getPackageName() + "/"
                            + R.raw.alert))

Hope this will work 希望这会奏效

READ HERE if the correct answer from Mukesh doesn't work for you!! 如果穆克什的正确答案不适合你,请在这里阅读 !!

The answer published from Mukesh is correct, but for me it wasn't working until I discovered I configured the notification wrong!! 从Mukesh发布的答案是正确的,但对我而言,直到我发现我将通知配置错误才能正常工作! (or there is an android BUG) (或者有一个Android BUG)

This snippet doesn't work 此代码段不起作用

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( context )
     .setDefaults( DEFAULT_VIBRATE | FLAG_SHOW_LIGHTS )
     .setContentTitle( title )
     .setContentText( message )

     .setSound( "android.resource://"
                        + context.getPackageName() + "/"
                        + R.raw.alert)).build();

- -

This snippet WORKS 这个片段工作

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( context )
     .setContentTitle( title )
     .setContentText( message )
     .setSound( "android.resource://"
                        + context.getPackageName() + "/"
                        + R.raw.alert)).build();

- -

.setDefaults( DEFAULT_VIBRATE | FLAG_SHOW_LIGHTS )

Observation: Like you see the line above I doesn't use the constanst DEFAULT_SOUND, but still does't work. 观察:就像你看到上面的那行我没有使用constanst DEFAULT_SOUND,但仍然无法正常工作。

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

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