不打自定义通知声音 [英] Custom notification sound not playing

查看:231
本文介绍了不打自定义通知声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个状态栏通知,自定义播放声音。在 MP3播放的文件中的 RES /生/ 的。但是,当我通知用户的声音不播放。我tryied用的MediaPlayer,和它的作品,但我不希望把它用的MediaPlayer播放。

I'm trying to make a custom sound play on a status bar notification. The .mp3 file is in res/raw/. But when I notify the user the sound is not played. I've tryied with MediaPlayer, and it works, but I dont want to make it play with MediaPlayer.

下面是我的方法:

public void showNotification()
{
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.feedback;        // icon from resources
        CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence contentTitle = mContext.getString(R.string.statusbar_notification);  // message title
        CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed);      // message text

        Intent notificationIntent = new Intent(mContext, Main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);

        //notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.sound = Uri.parse("android.resource://" + getPackageName() + "/R.raw.notificationsound");

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1, notification);
}

感谢。

推荐答案

从文档ContentResolver的:

From the documentation for ContentResolver:

这个URI应该是以下格式之一:   android.resource://程序包/ ID_NUMBER

The Uri should be one of the following formats: android.resource://package_name/id_number

正在传递字符串R.raw.notificationsound,这意味着什么。 相反,试试这个:

You are passing the String "R.raw.notificationsound" which means nothing. Instead try this:

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound );

这篇关于不打自定义通知声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆