我已经让用户在他们的通知中选择了他们想要的声音,但却无法使其正常工作。它始终播放默认声音。他们在偏好中选择声音
<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
,我知道这不是一个完整的路径。有什么想法吗?
答案 0 :(得分: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();
(闹钟)是一个字符串,来自我的共享偏好,如我原来的问题所示。完整实现的代码是
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);
答案 1 :(得分: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;