我已經嘗試了幾乎所有可用的答案在網站上,但不知何故我不能讓通知聲音工作。當前的代碼我測試是這個(通知是建立在報警reciever):Android:自定義通知聲音不播放
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替換聲音文件。 (我不記得確切的文件格式,我有問題) – 2014-11-14 14:01:59
刪除/原 – eduyayo 2014-11-14 14:04:19
我換成了一個WAV,並試圖刪除原料,不工作。 這裏有一點輸出,也許你們可以找出路徑錯誤的東西。 D/NotificationManager:notify:notification.sound android.resource:// * packagename(我必須隱藏它)*/raw/alert – 2014-11-14 14:14:06