2017-08-30 137 views
-3

我正在嘗試爲通知設置自定義聲音。設置通知的自定義聲音

詳情:

我試圖通過用戶的選擇來設置的通知聲音(從他們的電話存儲)。 因爲我保存路徑到數據庫,當通知來了,顯示通知和設置聲音;我從存在通知ID獲取聲音。

從那我決定哪個聲音應播放聲音哪個用戶選擇或默認聲音....但這不是在所有工作......它播放沒有聲音甚至默認一個。

如何設置/播放用於通知的自定義聲音!?

谷歌研究後:從原始文件夾播放聲音的許多建議,但我沒有得到通過我保存用戶選擇聲音原始文件夾(當然程序化盟友)的答案,所以我可以播放原始文件夾中的聲音..

PS有許多與此相關的ANS但他們並沒有與我的要求

這符合我的代碼:

private void showNotification(Context context, Things things) { 

    try { 
     NotificationManager mNotifyMgr = 
       (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.icon) 
       //example for large icon 
       .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon)) 
       .setContentTitle(things.getTitle()) 
       .setContentText(things.getThing()).setSubText(things.getNotification()) 
       .setOngoing(false) 
       .setPriority(NotificationCompat.PRIORITY_DEFAULT) 
       .setAutoCancel(true); 
     Intent i = new Intent(context, HomeActivity.class); 
     PendingIntent pendingIntent = 
       PendingIntent.getActivity(
         context, 
         0, 
         i, 
         PendingIntent.FLAG_ONE_SHOT 
       ); 
     // example for blinking LED 
     mBuilder.setLights(0xFFb71c1c, 1000, 2000); 
     if (things.getRingtone() == null || things.getRingtone().equals("")) { 
      mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

     } else { 
      Ringtone r = RingtoneManager.getRingtone(context, Uri.parse(things.getRingtone())); 
      r.play(); 
      Toast.makeText(context, things.getRingtone(), Toast.LENGTH_SHORT).show(); 
     } 
     //mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
     mBuilder.setContentIntent(pendingIntent); 
     mNotifyMgr.notify(12345, mBuilder.build()); 
    } catch (Exception e) { 
     Log.d(getClass().getName(), "catch " + e.toString()); 
    } 
} 
+0

降票原因? – pie

+0

@Rucha Bhatt可以嗎? – pie

+0

嗨歡迎來到stackoverflow,在這裏我已經試圖通過編輯你的問題來幫助你,所以其他人可以很容易地理解,我從來沒有嘗試過,但我會嘗試和研究的東西,讓你知道.. thanx –

回答

0

我用這個代碼:

private Uri getSound(){ 
    String sound = appPreferences.getString(LocalPreferences.PREFERENCE_NOTIFICATIONS_SOUND, ""); 
    Uri soundUri = null; 
    try { 
     soundUri = Uri.parse(sound); 
    } catch (Exception ignored) {} 

    if (soundUri == null) { 
     soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    } 
    return soundUri; 
} 

爲了把聲音我用這個

 @Override 
public boolean onPreferenceTreeClick(Preference preference){ 
    switch (preference.getKey()) { 
     case LocalPreferences.PREFERENCE_NOTIFICATIONS_SOUND: 
      Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); 
      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); 
      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); 
      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); 
      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI); 

      String existingValue = appPreferences.getString(LocalPreferences.PREFERENCE_NOTIFICATIONS_SOUND, ""); 
      if (existingValue != null) { 
       if (existingValue.length() == 0) { 
        // Select "Silent" 
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null); 
       } else { 
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(existingValue)); 
       } 
      } 

      startActivityForResult(intent, REQUEST_CODE_ALERT_RINGTONE); 
      return true; 
     default: 
      return super.onPreferenceTreeClick(preference); 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if (requestCode == REQUEST_CODE_ALERT_RINGTONE && data != null) { 
     Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 
     if (ringtone != null) { 
      setRingtonePreferenceValue(ringtone.toString()); 
     } else { 
      // "Silent" was selected 
      setRingtonePreferenceValue(""); 
     } 
    } else { 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

private void setRingtonePreferenceValue(String ringtonePath){ 
    appPreferences.edit().putString(LocalPreferences.PREFERENCE_NOTIFICATIONS_SOUND, ringtonePath).apply(); 
    // For ringtone preferences, look up the correct display value 
    // using RingtoneManager. 
    if (! TextUtils.isEmpty(ringtonePath)) { 
     Ringtone ringtone = RingtoneManager.getRingtone(ringtonePreference.getContext(), Uri.parse(ringtonePath)); 

     if (ringtone == null) { 
      // Clear the summary if there was a lookup error. 
      ringtonePreference.setSummary(null); 
     } else { 
      // Set the summary to reflect the new ringtone display 
      // name. 
      String name = ringtone.getTitle(ringtonePreference.getContext()); 
      ringtonePreference.setSummary(name); 
     } 
    } else { 
     ringtonePreference.setSummary("Без звука"); 
    } 
} 

來源爲第二塊喜好:https://issuetracker.google.com/issues/37057453#c2

當然,在構建您的通知,你應該叫

 builder.setSound(getSound()) 

並檢查您的設備上是否啓用了聲音。

希望它可以幫助你!

+0

這一行** Uri鈴聲= data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); **總是返回空 – pie

+0

如果我使用上面提到的意圖,它會打開手機內置的音調列表正確嗎?但我想讓用戶從任何地方選擇設備.. – pie

+0

你的建議做了選擇內置鬧鐘鈴聲,而不是任何音頻文件的伎倆,但 – pie