[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 
[coco8] Recording Audio from Bluetooth mic
This chunks of code are examples of simple audio recording on iOS devices using bluetooth mic.
Set Audio Session
"Set current session for audio play and recording"
| session |
session := AVAudioSession sharedInstance.
session setCategory: #AVAudioSessionCategoryPlayAndRecord
	withOptions: 4 error: nil.
session	setMode: #AVAudioSessionModeDefault error: nil.
session setActive: true error: nil.
Get available audio inputs
| session inputs |
session := AVAudioSession sharedInstance.
inputs := (session availableInputs
 asArrayOf: AVAudioSessionPortDescription)
 collect: [:each| each portName -> each portType ].
inputs asLiteral
Get current audio source
| session port |
session := AVAudioSession sharedInstance.
port := (session currentRoute inputs
 asArrayOf: AVAudioSessionPortDescription) first
 "portType"portName
 toString
Initializing a recorder to save audio in a file
| url settings recorder pError pathName |
pError := # #outArgument.
pathName := '*record.m4a' asLocalPathName.
#audio outputToFile: pathName. "the destination file must exist"
url := NSURL withPath: pathName.
settings := #AVFormatIDKey -> 16r61616320 "'aac ' = kAudioFormatMPEG4AAC"
	,(#AVSampleRateKey -> 44100) "44100 Hz"
	,(#AVNumberOfChannelsKey -> 1)
	,(#AVEncoderAudioQualityKey -> 16r7F000002). "AVAudioQualityHigh"
recorder := AVAudioRecorder
	withURL: url
	settings: settings
	error: pError.
R := recorder.
- Note:
- The file MUST exist (and be writable) before recorder creation
 - We use R global name to make the recorder available for the next chunks of code in this page
 
 
Prepare and start recoding
R prepareToRecord; record.
Stop recoding
R stop
Reading the audio contents
self note: 'Size of audio file =' ,'*record.m4a' fileContents size ,'bytes.' 
Clean the globals we have used in this page
 Smalltalk removeKey: #R ifAbsent: [].