我已经尝试了网站上几乎所有可用的答案,但不知何故我无法让通知声音正常工作。我正在测试的当前代码是这样的(通知内置在警报接收器中):
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);
然而,声音可以通过 MediaPlayer 播放,所以格式不是这里的问题。 会不会与声音的长度(30 秒)有关? 感谢您的帮助!
最佳答案
尝试将您的 .setSound() 更改为此
.setSound(Uri.parse("android.resource://"
+ context.getPackageName() + "/"
+ R.raw.alert))
希望这能奏效
关于java - Android:自定义通知声音不播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26931490/