diff --git a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioTrack.java b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioTrack.java index 123120ee64..6a27e352d8 100644 --- a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioTrack.java +++ b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioTrack.java @@ -591,10 +591,18 @@ public void setSpeakerMute(boolean mute) { public boolean updateAudioTrackUsage(int usage) { Logging.d(TAG, "updateAudioTrackUsage(usage=" + usage + ")"); + // if audioTrack is not created yet + // update the current audioAttributes with audioAttributes which the user has chosen + // return true because when native calls initPlayout it will pick the updated audioAttribute + if (audioTrack == null) { + audioAttributes = getAudioAttributes(usage); + return true; + } + // Check if the usage is already the same if (audioAttributes != null && audioAttributes.getUsage() == usage) { Logging.d(TAG, "Usage is already set to " + usage + ", no update needed"); - return true; + return false; } // Check if playout was active before reconfiguration @@ -610,14 +618,7 @@ public boolean updateAudioTrackUsage(int usage) { } // Update audio attributes with new usage - int contentType = AudioAttributes.CONTENT_TYPE_SPEECH; - if (usage == AudioAttributes.USAGE_MEDIA) { - contentType = AudioAttributes.CONTENT_TYPE_MUSIC; - } - audioAttributes = new AudioAttributes.Builder() - .setUsage(usage) - .setContentType(contentType) - .build(); + audioAttributes = getAudioAttributes(usage); // Use cached values from native initPlayout int sampleRate = cachedSampleRate; @@ -647,6 +648,16 @@ public boolean updateAudioTrackUsage(int usage) { return true; } + private AudioAttributes getAudioAttributes(int usage) { + int contentType = AudioAttributes.CONTENT_TYPE_SPEECH; + if (usage == AudioAttributes.USAGE_MEDIA) { + contentType = AudioAttributes.CONTENT_TYPE_MUSIC; + } + return new AudioAttributes.Builder() + .setUsage(usage) + .setContentType(contentType) + .build(); + } // Releases the native AudioTrack resources. private void releaseAudioResources() {