Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
869 stars 568 forks source link

[HELP] I cannot capture the audio from the bluetooth microphone [iOS] #413

Closed krit-l closed 3 years ago

krit-l commented 4 years ago

I need Help for : To record audio using a bluetooth microphone

Hi,

I downloaded "flutter_sound" 5.1.1 and adjust the code in "demo1.dart" to change to "bluetootha2dp"

  recorderModule.setAudioFocus(device: AudioDevice.blueToothA2DP); // added

  await recorderModule.startRecorder(
    toFile: path,
    codec: _codec,
    bitRate: 8000,
    sampleRate: 8000,
  );
  print('startRecorder');

However, it does not work. It still capture the voice from the embedded microphone.

Then, I tried to OpenAudioSession


 Future<void> init() async {
    //playerModule = await FlutterSoundPlayer().openAudioSession();
    recorderModule.openAudioSession(
        focus: AudioFocus.requestFocusTransient, 
        category: SessionCategory.playAndRecord, 
        mode: SessionMode.modeDefault, 
        audioFlags: allowBlueToothA2DP,  // adjusted
        device: AudioDevice.blueToothA2DP // adjusted
    );   
    await _initializeExample(false);
    if (Platform.isAndroid) {
      copyAssets();
    }
  }

Still, it does not work.

Is there anything I missed? If anyone could guide me or provide a source code, would be very appreciated.

Sorry that I am totally new to Flutter and iOS.

Regards, Krit

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 90 days with no activity. Leave a comment or this will be closed in 7 days.

Larpoux commented 3 years ago

Keep this issue open

Larpoux commented 3 years ago

@krit-l : I found a bug which can explain why you have difficulties to select the audio-device on iOS. I am going to fix that, and I will release a new version in a few days.

Larpoux commented 3 years ago

Please re-open this issue if you have still problems

zahidch052 commented 1 year ago

I'm facing same issue in flutter android app, can you suggest any solution?

Kashif-Asghar60 commented 1 year ago

I have the same issue for android, i cant use Bluetooth mic for input. The app switches to built-in mic for audio input. Can you try to help me out here? Link to my problem : https://stackoverflow.com/questions/75637854/how-can-i-use-bluetooth-mic-in-flutter

zahidch052 commented 1 year ago

I have the same issue for android, i cant use Bluetooth mic for input. The app switches to built-in mic for audio input. Can you try to help me out here? Link to my problem : https://stackoverflow.com/questions/75637854/how-can-i-use-bluetooth-mic-in-flutter

@Kashif-Asghar60 I resolved this issue by using Flutter channel to turn call mode on when Bluetooth is connected for recording.

Add this code into your kotlin file and call this channel from flutter

import android.annotation.SuppressLint import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothClass import android.bluetooth.BluetoothDevice import android.content.Context import android.media.AudioManager import androidx.annotation.NonNull import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel class MainActivity : FlutterActivity() {

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)

    // Register a new method channel to communicate with the Flutter app
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "example.com/channel").setMethodCallHandler { call, result ->
        when (call.method) {
            "switchToBluetoothMicrophone" -> {
                switchToBluetoothMicrophone()
                result.success(null)
            }
            else -> {
                result.notImplemented()
            }
        }
    }
}

@SuppressLint("MissingPermission")
private fun switchToBluetoothMicrophone() {
    // Get the Bluetooth adapter
    val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    print("message1");
    // Ensure that Bluetooth is enabled on the device
    if (bluetoothAdapter.isEnabled) {
        // Get a list of paired Bluetooth devices
        val pairedDevices: Set<BluetoothDevice> = bluetoothAdapter.bondedDevices
        print("message2")
        // Iterate over the list of paired devices and find the Bluetooth microphone
        for (device in pairedDevices) {
            print("message3")

// if (device.bluetoothClass.deviceClass == BluetoothClass.Device.AUDIO_VIDEO_MICROPHONE) { // print("message4") // Set the Bluetooth microphone as the default audio input device val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager.mode = AudioManager.MODE_IN_CALL audioManager.isBluetoothScoOn = true audioManager.startBluetoothSco() audioManager.isMicrophoneMute = false audioManager.isSpeakerphoneOn = false break // } } } } }

Kashif-Asghar60 commented 1 year ago

I used the native approach with kotlin and it worked , Hats-off to zahid for sharing the kotlin code.

anooravk commented 1 year ago

I used the native approach with kotlin and it worked , Hats-off to zahid for sharing the kotlin code.

how are you handling it from flutter side?

Kashif-Asghar60 commented 1 year ago

Drop your email , i will share the project file with you.

Kashif-Asghar60 commented 1 year ago

I used method channels to call it from kotlin

anooravk commented 1 year ago

I used method channels to call it from kotlin

im doing the same but it still uses the phone mic instead of connected bluetooth device

anooravk commented 1 year ago

Drop your email , i will share the project file with you.

sure that would be very helpful thank you so much. @anooravk@gmail.com

Kashif-Asghar60 commented 1 year ago

yea , i had a similar problem. i fixed it. But my solution only works for call quality audio via Bluetooth. I mean the sound which is playing via Bluetooth headset while using mic will be in low quality , not bassy (high quality)

anooravk commented 1 year ago

that would work I only need to use Bluetooth mic with speech recognition

sveinbjornt commented 1 year ago

@Kashif-Asghar60 Could you share your code with me also? Struggling to get Bluetooth mic recording working in Flutter. sveinbjornt@gmail.com

Kashif-Asghar60 commented 1 year ago

sveinbjornt@gmail.com

Check your email for the file; I've just sent it.

JainnyAryan commented 1 year ago

@Kashif-Asghar60 Could you share your code with me also? Struggling to get Bluetooth mic recording working in Flutter. sveinbjornt@gmail.com

To me as well please, aryan.one0793@gmail.com

Kashif-Asghar60 commented 1 year ago

@Kashif-Asghar60 Could you share your code with me also? Struggling to get Bluetooth mic recording working in Flutter. sveinbjornt@gmail.com

To me as well please, aryan.one0793@gmail.com

shared!

sveinbjornt commented 1 year ago

I had great luck with this in my audio session initialization code using audio_session:

  if (Platform.isAndroid) {
    // Configure support for Bluetooth headset on Android
    AndroidAudioManager().startBluetoothSco();
    AndroidAudioManager().setBluetoothScoOn(true);
  }
Guz-TheMan commented 11 months ago

@Kashif-Asghar60 Could you share please your code with me also? Struggling to get Bluetooth mic recording working in Flutter. correo.aog@gmail.com

avishayw commented 9 months ago

@Kashif-Asghar60 I'd be happy if you shared your code with me as well av2558@gmail.com

@sveinbjornt I tried your solution. It does enable recording via bluetooth headset, but it activates bluetooth communication mode which is more suited for voice/video calls. I used it and after disposing the recorder got a notification from my headset "Call has ended". Not so good for user experience with a simple recording.

Kashif-Asghar60 commented 9 months ago

@Kashif-Asghar60 I'd be happy if you shared your code with me as well av2558@gmail.com

@sveinbjornt I tried your solution. It does enable recording via bluetooth headset, but it activates bluetooth communication mode which is more suited for voice/video calls. I used it and after disposing the recorder got a notification from my headset "Call has ended". Not so good for user experience with a simple recording.

My solution does the same, it captures the audio from the mic in call quality mode. And somehow i accidentally deleted the project file from my drive, so i cant share it anymore :(

dweberlj commented 8 months ago

I had great luck with this in my audio session initialization code using audio_session:

  if (Platform.isAndroid) {
    // Configure support for Bluetooth headset on Android
    AndroidAudioManager().startBluetoothSco();
    AndroidAudioManager().setBluetoothScoOn(true);
  }

@sveinbjornt thank you for your Android code snippet. Do you have a suggestion on how to code this for iOS?

sveinbjornt commented 8 months ago

@sveinbjornt thank you for your Android code snippet. Do you have a suggestion on how to code this for iOS?

I never had any problems with Bluetooth audio recording on iOS.

dweberlj commented 8 months ago

@sveinbjornt did you have to setup flutter_sound for Bluetooth? My code to open the record from initState() is below but it still uses the iPhone microphone.

  Future<void> openTheRecorder() async {
    var status = await Permission.microphone.request();
    if (status != PermissionStatus.granted) {
      throw RecordingPermissionException('Microphone permission not granted');
    }
    // }
    await _recorder.openAudioSession();
    _mRecorderIsInited = true;
  }