【问题标题】:How to play notification sound from local file如何从本地文件播放通知声音
【发布时间】:2017-08-02 20:04:07
【问题描述】:
    sound = Uri.parse("file://" + audioFilePath.getAbsolutePath());
    /*sound = Uri.fromFile(audioFilePath);*/
    NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this);
            mBuilder
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(title)
                    .setContentText(description)
                    .setContentIntent(resultPendingIntent)
                    .setSound(sound);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mBuilder.setPriority(Notification.PRIORITY_HIGH);
            }
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());

声音文件存在于本地存储中。通知工作正常,但声音不播放。我尝试了两种从文件路径解析 Uri 的方法。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    如果您将声音放在资源文件夹中(对于通知声音,无论如何您都应该这样做)并使用 Notification.Builder 而不是 NotificationCompat.Builder,您应该能够像这样访问它:

    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 
                           getPackageName() + "/" + R.raw.name_of_sound);
    Notification.Builder mBuilder =
                    new Notification.Builder(this);
            mBuilder
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(title)
                    .setContentText(description)
                    .setContentIntent(resultPendingIntent)
                    .setSound(sound);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mBuilder.setPriority(Notification.PRIORITY_HIGH);
            }
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());
    

    Source

    【讨论】:

    • 有没有办法访问保存的本地文件?而不是实际将其添加到资源中?我的应用程序将文件保存在本地,我希望它能够播放。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多