Skip to content

Commit

Permalink
Merge pull request #1569 from LiPengfei0106/master
Browse files Browse the repository at this point in the history
MicrophoneSource support setPreferredDevice
  • Loading branch information
pedroSG94 authored Sep 4, 2024
2 parents e65ad62 + 64caf7a commit a83c85b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.pedro.encoder.input.audio;

import android.annotation.SuppressLint;
import android.media.AudioDeviceInfo;
import android.media.AudioFormat;
import android.media.AudioPlaybackCaptureConfiguration;
import android.media.AudioRecord;
Expand All @@ -27,6 +28,8 @@
import android.os.HandlerThread;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.pedro.encoder.Frame;

/**
Expand Down Expand Up @@ -185,6 +188,15 @@ private void init() {
}
}

@RequiresApi(api = Build.VERSION_CODES.M)
public boolean setPreferredDevice(AudioDeviceInfo deviceInfo){
if(audioRecord == null) {
Log.w(TAG, "audioRecord not created");
return false;
}
return audioRecord.setPreferredDevice(deviceInfo);
}

public void mute() {
muted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.pedro.library.util.sources.audio

import android.media.AudioDeviceInfo
import android.media.MediaRecorder
import android.os.Build
import androidx.annotation.RequiresApi
import com.pedro.encoder.Frame
import com.pedro.encoder.input.audio.GetMicrophoneData
import com.pedro.encoder.input.audio.MicrophoneManager
Expand All @@ -25,10 +28,11 @@ import com.pedro.encoder.input.audio.MicrophoneManager
* Created by pedro on 12/1/24.
*/
class MicrophoneSource(
var audioSource: Int = MediaRecorder.AudioSource.DEFAULT
var audioSource: Int = MediaRecorder.AudioSource.DEFAULT,
): AudioSource(), GetMicrophoneData {

private val microphone = MicrophoneManager(this)
private var preferredDevice: AudioDeviceInfo? = null

override fun create(sampleRate: Int, isStereo: Boolean, echoCanceler: Boolean, noiseSuppressor: Boolean): Boolean {
//create microphone to confirm valid parameters
Expand All @@ -39,13 +43,22 @@ class MicrophoneSource(
return true
}

@RequiresApi(api = Build.VERSION_CODES.M)
fun setPreferredDevice(deviceInfo: AudioDeviceInfo?): Boolean {
preferredDevice = deviceInfo
return microphone.setPreferredDevice(deviceInfo)
}

override fun start(getMicrophoneData: GetMicrophoneData) {
this.getMicrophoneData = getMicrophoneData
if (!isRunning()) {
val result = microphone.createMicrophone(audioSource, sampleRate, isStereo, echoCanceler, noiseSuppressor)
if (!result) {
throw IllegalArgumentException("Failed to create microphone audio source")
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
microphone.setPreferredDevice(preferredDevice)
}
microphone.start()
}
}
Expand Down

0 comments on commit a83c85b

Please sign in to comment.