0

我已经让用户选择他们想要在通知中使用什么声音,但无法让它发挥作用。它始终播放默认声音。他们在偏好中选择自己的声音

 <RingtonePreference
        android:defaultValue="content://settings/system/notification_sound"
        android:key="alarmsound"
        android:ringtoneType="notification"
        android:showDefault="true"
        android:summary="Choose Your Alarm Sound"
        android:title="Alarm Sound" />
</PreferenceCategory>

然后它在我的通知活动中被拉起

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String alarms = getPrefs.getString("alarmsound", "");

这将返回一个内容 Uri content://media/external/audio/media/10

我试图将其转换为文件路径

String filePath = null;
Uri _uri = Uri.parse(alarms);
Cursor cursor = this.getContentResolver().query(_uri, new String [] { android.provider.MediaStore.Audio.Media.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
Log.e(TAG5, filePath);

这返回 /storage/sdcard0/Notfications/hangout_ringtone.m4a

我试着把它放进去,.setSound(filePath);但它不起作用。它想要一个Uri,我知道这不是一条完整的路径。有任何想法吗?

4

2 回答 2

1

不确定其他人是否需要这个,但我能够在其他 stackoverflow 帖子和以下代码的帮助下解决我的问题。首先,我在通知生成器中禁用了任何默认声音

NotificationManager notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            getApplicationContext())


            .setContentText(notificationContent)
            .setContentTitle(notificationTitle)
            .setSmallIcon(smallIcon)
            .setAutoCancel(true)
            .setTicker(notificationTitle)
            .setLargeIcon(largeIcon)
            .setSound(null);

将声音设置为 null 会删除默认声音。然后我在下一行添加

RingtoneManager.getRingtone(this, Uri.parse(alarms)).play();

(alarms) 是一个来自我的 sharedpreferences 的字符串,如我原来的问题中所见。完整实现的代码是

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean vibrate = getPrefs.getBoolean("vibrate", true);

    String alarms = getPrefs.getString("alarmsound", "");
    Log.e(TAG4, alarms);




    NotificationManager notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            getApplicationContext())


            .setContentText(notificationContent)
            .setContentTitle(notificationTitle)
            .setSmallIcon(smallIcon)
            .setAutoCancel(true)
            .setTicker(notificationTitle)
            .setLargeIcon(largeIcon)
            .setSound(null);


            RingtoneManager.getRingtone(this, Uri.parse(alarms)).play();        

    if (vibrate == true ){


            notificationBuilder.setDefaults(
                    Notification.DEFAULT_LIGHTS
                            | Notification.DEFAULT_VIBRATE | Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent);


    } else {
                notificationBuilder.setDefaults(
                        Notification.DEFAULT_LIGHTS
                                | Notification.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);

    }
        Notification notification = notificationBuilder.build();
        notificationManager.notify(id, notification);
于 2013-07-05T19:40:27.003 回答
1

我相信这会为您解决问题:

String alarms = getPrefs.getString("alarmsound", "");
Uri _uri = Uri.parse(alarms);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
        .setContentText(notificationContent)
        .setContentTitle(notificationTitle)
        .setSmallIcon(smallIcon)
        .setAutoCancel(true)
        .setTicker(notificationTitle)
        .setLargeIcon(largeIcon)
        .setSound(_uri);

Notification notification = nBuilder.getNotification();

// Make sure you do *not* include the default sound 
// or the setSound call above will be ignored
notification.defaults |= Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
于 2015-03-18T01:09:36.483 回答