Sinch被叫方音频异常:扬声器无法正常发声问题排查
Hey there! Let's break down why the callee can only hear audio through the earpiece instead of the speaker, and how to fix this issue.
Why this happens
Your current code only enables the speaker on the caller's device in the onCallEstablished callback. Here's the key point: Sinch's audio controls are completely client-side independent. Each device (caller and callee) manages its own audio output settings separately—one side's configuration won't automatically apply to the other. The callee's app never receives the instruction to switch to speaker mode, so it defaults to using the earpiece.
Fixes to implement
Replicate the audio setup logic on the callee's end
You need to add the exact same audio configuration code to the callee'sonCallEstablishedmethod. This ensures both devices set up the correct audio stream and enable the speaker when the call connects:@Override public void onCallEstablished(Call call) { Log.d(TAG, "Call established"); mCallState.setText(call.getState().toString()); // Tie volume controls to the voice call stream setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); // Enable speaker output for this device getSinchServiceInterface().getAudioController().enableSpeaker(); }Verify required permissions are in place
Make sure the callee's app has the necessary permissions declared inAndroidManifest.xmlto modify audio settings and access the microphone:<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />If you're targeting Android 6.0 (API level 23) or higher, don't forget to request these permissions at runtime from the user.
Add a manual speaker toggle (optional but user-friendly)
Users might want to switch between earpiece and speaker manually. Add a UI button and use this code to toggle the speaker state:private void toggleSpeaker() { AudioController audioController = getSinchServiceInterface().getAudioController(); if (audioController != null) { boolean isSpeakerActive = audioController.isSpeakerEnabled(); audioController.enableSpeaker(!isSpeakerActive); // Update your UI to reflect the current state (e.g., change button icon/text) } }Ensure your app has audio focus
Other apps might be holding audio focus, which can block your app from using the speaker. Request audio focus when the call starts:AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int focusResult = audioManager.requestAudioFocus( null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN ); if (focusResult != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.w(TAG, "Failed to get audio focus—speaker functionality might be limited"); // Handle this scenario (e.g., show a toast alert to the user) }
内容的提问来源于stack exchange,提问作者Aman Shukla

