通知声音无法播放

4

我知道这个问题已经被问了很多次,但我仍然无法让它正常工作。

在我的应用程序中,我创建了一个通知来提示用户某些事件,并且我想播放作为应用程序内原始资源打包的音频。

我创建了我的通知实例:

Notification notification = new Notification(
        R.drawable.some_image,
        "some meaningful name",
        System.currentTimeMillis() );

notification.setLatestEventInfo(...)

然后在该通知上设置一些属性:

notification.sound = Uri.parse( "android.resource://com.my.package/" + R.raw.some_mp3_file );
notification.flags = Notification.FLAG_INSISTENT;

最后我调用NotificationManager来显示通知:

notificationManager.notify( 1, notification );

通知确实会显示,但声音不会播放。

我做错了什么吗?我是否缺少某个 <uses-permission>?我没有看到我与其他人所做的任何不同之处,他们似乎已经让它工作了。

值得一提的是,我正在直接在Nexus 7上进行测试。


你成功解决了吗?我也有同样的问题。 - c0dehunter
2个回答

1

试着看一下下面的代码。可能会帮助你解决问题。

String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.update;
        CharSequence tickerText = "assignments";
        long when = System.currentTimeMillis();

        Notification assignmentNotification = new Notification(icon, tickerText, when);
       **For sound** assignmentNotification.defaults |= Notification.DEFAULT_SOUND;
        long[] vibrate = {0,100,200,300};
       **For vibrate** assignmentNotification.vibrate = vibrate;
        Context context = getApplicationContext();
        CharSequence contentTitle = "check assignments";
        CharSequence contentText = "chek ur app for assignments ";
        Intent notificationIntent = new Intent(context, ViewAssignmentnotificationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0);
        assignmentNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
       ** final int id = 2;

1
谢谢,但这不是我要问的。 我在我的应用程序中捆绑了一个特定的声音作为原始资源,我想与我的通知一起播放 - 而不是您的示例代码所显示的默认声音/铃声。 - user1672906

0
请查看这个回答;它告诉你必须显式指定通知声音才能播放那个音调;

1
看看我原帖中第二个代码块的第一行 - 那不就是我所做的吗? - user1672906
我接受;但请尝试我发布的链接中的默认URI;这样我们可能会缩小问题范围。 - Manoj Kumar
没有成功。我首先调用了RingtoneManager.setActualDefaultRingtoneUri(),将原帖中的相同Uri作为最后一个参数传入。然后我设置了notification.defaults |= Notification.DEFAULT_SOUND;...通知出现了,但声音没有播放。 - user1672906
你是在4.0平台上开发应用程序并在ICS或更高版本上进行测试吗? - Manoj Kumar
编译版本为3.2(API 13),运行在4.1.1(API 16)上。我有一种隐隐的感觉,这与此有些相关,但我还没有真正思考过... - user1672906
请在Gingerbread中尝试您的代码并查看结果,因为我已经下载了一个示例应用程序,在Gingerbread中可以正常工作,但在ICS中没有任何声音。 - Manoj Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接