【发布时间】:2014-11-14 13:49:30
【问题描述】:
我已经尝试了网站上几乎所有可用的答案,但不知何故我无法让通知声音正常工作。我正在测试的当前代码是这样的(通知内置在警报接收器中):
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 秒)有关吗? 感谢您的帮助!
【问题讨论】:
-
什么是声音格式?我遇到了可以通过 MediaPlayer 播放的声音,但不是来自通知的问题...例如,尝试用 wmv 替换您的声音文件。 (我不记得我遇到问题的确切文件格式)
-
我用 wav 替换,并尝试删除原始文件,但不起作用。这里有一点输出,如果路径错误,也许你们可以弄清楚。 D/NotificationManager:通知:notification.sound android.resource://*packagename(我必须隐藏它)*/raw/alert
-
澄清:R.raw.sound 表示您的声音文件位于 res/raw/sound.jpg 中。而这个声音可以通过 android 的 R.raw.sound 来实现
标签: java android audio notifications