play dial tones on STREAM_MUSIC when phone is silent 
    
      
      
      
        
        Daniel Gultsch 
      
      created 2 years ago 
    
   
  
  
  when the phone is silent only the first ~three tones are played when
attempting to play out the tone over STREAM_VOICE_CALL
it’s unclear exactly why this is the case (in the past we went back and forth
between STREAM_VOICE_CALL and STREAM_MUSIC) exactly to fix issues around silent
mode.
Apparently we failed to test this past three sounds.
This commit changes the stream back to music - but not generally as this was in
the past - but only for when the phone is on silent 
  
  
  
    
   
 
  Change summary 
  src/main/java/eu/siacs/conversations/xmpp/jingle/ToneManager.java | 33 
1 file changed, 22 insertions(+), 11 deletions(-)
 
 
  Detailed changes 
  
  
    
    @@ -14,9 +14,11 @@ import eu.siacs.conversations.Config;
 
 import static java.util.Arrays.asList;
 
+import androidx.core.content.ContextCompat; 
+ 
 class ToneManager {
 
-    private final ToneGenerator toneGenerator;
+    private ToneGenerator toneGenerator; 
     private final Context context;
 
     private ToneState state = null;
@@ -26,14 +28,6 @@ class ToneManager {
     private boolean appRtcAudioManagerHasControl = false;
 
     ToneManager(final Context context) {
-        ToneGenerator toneGenerator;
-        try {
-            toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, 60);
-        } catch (final RuntimeException e) {
-            Log.e(Config.LOGTAG, "unable to instantiate ToneGenerator", e);
-            toneGenerator = null;
-        }
-        this.toneGenerator = toneGenerator;
         this.context = context;
     }
 
@@ -172,10 +166,27 @@ class ToneManager {
     }
 
     public void startTone(final int toneType, final int durationMs) {
+        if (this.toneGenerator != null) { 
+            this.toneGenerator.release(); 
+        } 
+        final AudioManager audioManager = ContextCompat.getSystemService(context, AudioManager.class); 
+        final boolean ringerModeNormal = audioManager == null || audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL; 
+        this.toneGenerator = getToneGenerator(ringerModeNormal); 
         if (toneGenerator != null) {
             this.toneGenerator.startTone(toneType, durationMs);
-        } else {
-            Log.e(Config.LOGTAG, "failed to start tone. ToneGenerator doesn't exist");
+        } 
+    } 
+ 
+    private static ToneGenerator getToneGenerator(final boolean ringerModeNormal) { 
+        try { 
+            if (ringerModeNormal) { 
+                return new ToneGenerator(AudioManager.STREAM_VOICE_CALL,60); 
+            } else { 
+                return new ToneGenerator(AudioManager.STREAM_MUSIC,100); 
+            } 
+        } catch (final Exception e) { 
+            Log.d(Config.LOGTAG,"could not create tone generator",e); 
+            return null; 
         }
     }