【发布时间】:2015-01-05 13:19:29
【问题描述】:
我有一个闹钟应用程序,它会在闹钟响起时播放声音(类似闹钟的连续音频)。不幸的是,在 Lollipop 中,声音并没有完全播放,而是在几秒钟后停止。但是,如果手机已连接到电源,则不会发生这种情况,并且实际上会完全播放声音。该代码在早期版本的 Android 上运行良好。有人可以帮忙吗?这是我的通知代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setPriority(Integer.MAX_VALUE)
.setContentTitle(someTitle)
.setWhen(now)
.setIcon(R.drawable.some_icon);
Notification notif = mBuilder.build();
if(Build.VERSION.SDK_INT >= 21) {
notif.sound = audioFileUri;
notif.category = Notification.CATEGORY_ALARM;
AudioAttributes.Builder attrs = new AudioAttributes.Builder();
attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
attrs.setUsage(useAlarm ? AudioAttributes.USAGE_ALARM : AudioAttributes.USAGE_NOTIFICATION_EVENT);
notif.audioAttributes = attrs.build();
} else {
mBuilder.setSound(audioFileUri, useAlarm ? AudioManager.STREAM_ALARM : AudioManager.STREAM_NOTIFICATION);
notif = mBuilder.build();
}
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, notif);
【问题讨论】:
标签: android audio notifications android-notifications android-5.0-lollipop