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

他の誰かがこれを必要とするかどうかはわかりませんが、他のスタックオーバーフローの投稿と次のコードの助けを借りて問題を解決できました。まず、通知ビルダーでデフォルトのサウンドを無効にしました

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 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 に答える