【问题标题】:Android: Custom notification sound not playingAndroid:自定义通知声音未播放
【发布时间】: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


【解决方案1】:

尝试将您的 .setSound() 更改为此

.setSound(Uri.parse("android.resource://"
                            + context.getPackageName() + "/"
                            + R.raw.alert))

希望这会奏效

【讨论】:

  • 到目前为止尝试过 mp3 和 wma。我是否必须更具体,我对编解码器等了解不多。
  • 好吧,如果你认为声音的长度会有所不同,那么尝试一个持续时间更短的,如果它有效,那么持续时间将是问题,记住对 .setSound() 使用相同的代码正如我发布的那样。
  • 对不起,我走了,但较短的版本也不起作用。 (5秒)。会不会太过分了?到目前为止,我已经消除了 URI 错误的选项,因为我对媒体播放器使用了相同的选项,并且它正在工作。
  • 好吧,我最终在接收器中使用了一个播放器,并设置了一个 deleteIntent 来处理声音的停止,但是您的回答帮助我获得了正确的 Uri,谢谢。
  • 很高兴为您提供帮助.. :)
【解决方案2】:

阅读此处如果 Mukesh 的正确答案对您不起作用!!

Mukesh 发布的答案是正确的,但对我来说,直到我发现我配置了错误的通知才有效! (或者有安卓BUG)

这个 sn-p 不起作用

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( context )
     .setDefaults( DEFAULT_VIBRATE | FLAG_SHOW_LIGHTS )
     .setContentTitle( title )
     .setContentText( message )

     .setSound( "android.resource://"
                        + context.getPackageName() + "/"
                        + R.raw.alert)).build();

-

这个 sn-p 工作

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( context )
     .setContentTitle( title )
     .setContentText( message )
     .setSound( "android.resource://"
                        + context.getPackageName() + "/"
                        + R.raw.alert)).build();

-

.setDefaults( DEFAULT_VIBRATE | FLAG_SHOW_LIGHTS )

观察: 就像你看到上面的那行一样,我没有使用常量 DEFAULT_SOUND,但仍然不起作用。

【讨论】:

  • 谢谢!!!!非常沮丧,它没有播放我选择的声音,只有系统通知声音。即使设置为 SOUND,也请关闭 setDefaults,瞧,效果很好!
  • @Tano 你有没有试过在7.0 (N)8.0 (O) 上面玩Device stored file path URI 得到FileUriExposedException 任何想法
  • 很好,但是通知声音的长度有限制吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多