[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 
[node8-android] Recording Audio from Bluetooth mic
This chunks of code are examples of simple audio recording on Android devices using bluetooth mic.
Prerequisites
This permisions are required by the application's AndroidManifest.xml
| uses-permission android:name="android.permission.RECORD_AUDIO" | 
| uses-permission android:name="android.permission.BLUETOOTH" | 
| uses-permission android:name="android.permission.BLUETOOTH_ADMIN" | 
| uses-permission android:name="android.permission.BLUETOOTH_CONNECT" | 
Ensure you have that lines included in s8/sources/appS8/uses-permissions.st
And used to build the snapshot of your system.
Check for permissions to record audio
(AndroidApplication requestPermissionsFor: 'android.permission.RECORD_AUDIO'
) = true ifFalse: [ ^self error: 'Needs permission to record audio' ]
 Activate Bluetooth SCO audio if available
| audioManager |
audioManager := MainActivity current getSystemService: #audio.
audioManager isBluetoothScoAvailableOffCall ifTrue: [
	audioManager startBluetoothSco.
	audioManager bluetoothScoOn: true.
	AM := audioManager
]
Note: AM global will be the audio manager after evaluating this chunk
Using MediaRecorder to record audio
| pathName recorder |
pathName := MainActivity current filesDir absolutePath ,'/recorded.mp4'.
recorder := MediaRecorder instance.
recorder
	audioSource: MediaRecorder audioSource @@ #VOICE_COMMUNICATION;
	outputFormat: MediaRecorder outputFormat @@ #MPEG_4;
	audioEncoder: MediaRecorder audioEncoder @@ #AAC;
	audioSamplingRate: 44100;
	audioEncodingBitRate: 128000;
	outputFile: pathName.
Rec := recorder
Prepare and start recoding
 Rec prepare; start 
Stop recoding
 Rec stop; release 
Reading the audio contents
self note: 'Size of audio file =' ,(MainActivity current filesDir absolutePath ,'/recorded.mp4') fileContents size ,'bytes.' 
Clean the globals we have used in this page
Smalltalk removeKey: #Rec ifAbsent: [].
Smalltalk removeKey: #AM ifAbsent: [].