Hi,
I am using to the sound.Recorder(file_path) function to record but it seems only to record from the phone microphone even if my headset is connected via Bluetooth. is there anyway to specify the microphone to be used? Or what would be the right way to make it work?
Many thanks for your help.
Forum Archive
Recording from Bluetooth headset
@tahti, unfortunately the sound module is not implemented for Bluetooth devices nor does it contain the settings to enable them, so you would need to use Appleās API directly.
Below is a quick example how. Not surprisingly, the recorder object here has the same record, pause and stop methods as the Recorder class in the sound module.
Note that I am assuming that the bt headset is the last input on the list (index -1). If this does not work for you, you can check the list of available inputs by enabling the commented-out line.
import objc_util
filename = 'tmp_audio.m4a'
AVAudioSession = objc_util.ObjCClass('AVAudioSession')
AVAudioRecorder = objc_util.ObjCClass('AVAudioRecorder')
audiosession = AVAudioSession.sharedInstance()
audiosession.setCategory_withOptions_error_(
"AVAudioSessionCategoryPlayAndRecord",
4, # Allow Bluetooth
None
)
# print(audiosession.availableInputs())
assert audiosession.setPreferredInput_error_(audiosession.availableInputs()[-1], None) == True
assert audiosession.setActive_error_(True, None) == True
recorder = AVAudioRecorder.alloc().initWithURL_settings_error_(objc_util.nsurl(filename), None, None)
recorder.recordForDuration_(10)
Are the == True really needed?
The linters will tell us that they should be is True (the old equality vs. identity thing) but my sense is that they can just be removed.
@ccc, that is quite True.
Many thanks for the answer! It did solve my problem and question. This great. Seems I will have to learn how to use apple APIs. This is great!