#Region Project Attributes
#ApplicationLabel: DictaPhone
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private AT As AudioTrack
Private AR As AudioRecord
Private Buffers() As Byte
Private timer1 As Timer
Private recordingStart As Long
Private RP As RuntimePermissions
Dim MP As MediaPlayer
Dim Interrupt As Boolean
Dim OutFile As RandomAccessFile
Dim StartTime As Long
Dim BufferSize As Int
Dim SampleRate As Int
Dim ChannelConfig As Int
Dim AudioFormat As Int
Dim AudioSource As Int
Dim NoChnls,BitsPerSample,DataSize As Int
End Sub
Sub Globals
Dim Label1 As Label
Dim btnPlay As Button
Dim btnStartRecording As Button
Dim Record As Thread
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1.bal")
'Initialize Media player for playback
MP.Initialize
'Initialize the thread to record on
Record.Initialise("Rec")
'Set up recording parameters Not all parameters will be supported on all
'devices
AudioSource=AR.A_Src_Mic
SampleRate=44100
ChannelConfig=AR.Ch_Conf_Mono
NoChnls=1
AudioFormat=AR.Af_PCM_16
BitsPerSample=16
'Determine the required min buffer size
BufferSize=AR.GetMinBufferSize(SampleRate,ChannelConfig,AudioFormat)
If BufferSize < 0 Then
Msgbox("Buffer error, Hardware does not support recording with the given parameters","Buffer error")
Activity.Finish
End If
AR.Initialize(AudioSource,SampleRate,ChannelConfig,AudioFormat,BufferSize)
'Test settings
' AR.SetNotificationMarkerPosition(3000)
' AR.SetPositionNotificationPeriod(2000)
'delete the file if it exists to avoid problems with Random access files
If File.Exists(File.DirInternal,"test.wav") Then
File.Delete(File.DirInternal,"test.wav")
End If
'Initialize the output file
Log(File.DirDefaultExternal)
OutFile.Initialize2(File.DirInternal,"test.wav",False,True)
'Write the Wave file header
WriteWavHeader
'enable restriction of recording time for testing
StartTime=DateTime.Now
End Sub
Sub Recording
'ReSet the data size
DataSize=0
Log("Recording...")
'Do the recording
'I've read that the read methods are blocking and won't return until
' the buffer is full. Which appears to be the case in my testing.
' data has to be read pretty much immediately or it will get overwritten
Do While True
Dim RecData() As Byte
Dim Sum As Int
RecData=AR.ReadBytes(0,BufferSize)
OutFile.WriteBytes(RecData,0,RecData.Length,44+DataSize)
DataSize=DataSize+RecData.Length
For i = 0 To 480 Step 2 'Approx 5ms worth of data
Sum=Sum+(RecData(i)*256)+RecData(i+1)
Next
Record.RunOnGuiThread("Showvolume",Array As Object(Sum))
'Check if interupt requested
If Interrupt Then Exit
'Check if recording time is up
If DateTime.Now > StartTime+"10000" Then Exit
Loop
End Sub
Sub streamer_RecordBuffer (Buffer() As Byte)
'collect the recording data
'Buffers.Add(Buffer)
End Sub
Sub btnStartRecording_Click
AR.StartRecording
'enable restriction of recording time for testing
StartTime=DateTime.Now
'Start the thread to record on
Record.Start(Me,"Recording",Null)
recordingStart = DateTime.Now
timer1.Interval = 1
timer1.Enabled = True
Timer1_Tick
btnPlay.Enabled = False
End Sub
Sub Timer1_Tick
Label1.Text = "Recording: " & _
Round((DateTime.Now - recordingStart) / DateTime.TicksPerSecond) & " seconds"
End Sub
Sub btnStopRecording_Click
AR.Stop
timer1.Enabled = False
btnPlay.Enabled = True
Label1.Text = ""
End Sub
Sub btnPlay_Click
btnStartRecording.Enabled = False
'For Each b() As Byte In Buffers
' streamer.Write(b)
'Next
'streamer.Write(Null) 'when this "message" will be processed, the player will stop.
End Sub
Sub streamer_PlaybackComplete
Log("PlaybackComplete")
btnStartRecording.Enabled = True
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub WriteWavHeader
Dim Pos,IntLen,ShLen,StrLen As Int
Pos=0
IntLen=4
ShLen=2
StrLen=4
OutFile.WriteBytes(Array As Byte(Asc("R"),Asc("I"),Asc("F"),Asc("F")),0,StrLen,Pos)
Pos=Pos+IntLen
OutFile.WriteInt(0,Pos) 'Final size not yet known
Pos=Pos+IntLen
OutFile.WriteBytes(Array As Byte(Asc("W"),Asc("A"),Asc("V"),Asc("E")),0,StrLen,Pos)
Pos=Pos+IntLen
OutFile.WriteBytes(Array As Byte(Asc("f"),Asc("m"),Asc("t"),Asc(" ")),0,StrLen,Pos)
Pos=Pos+IntLen
OutFile.WriteInt(16,Pos) 'Sub chunk size 16 for PCM
Pos=Pos+IntLen
OutFile.WriteShort(1,Pos) 'Audio Format, 1 for PCM
Pos=Pos+ShLen
OutFile.WriteShort(1,Pos) 'No of Channels
Pos=Pos+ShLen
OutFile.WriteInt(SampleRate,Pos)
Pos=Pos+IntLen
OutFile.WriteInt(SampleRate*BitsPerSample*NoChnls/8,Pos) 'Byte Rate
Pos=Pos+IntLen
OutFile.WriteShort(NoChnls*BitsPerSample/8,Pos) 'Block align, NumberOfChannels*BitsPerSample/8
Pos=Pos+ShLen
OutFile.WriteShort(BitsPerSample,Pos) 'BitsPerSample
Pos=Pos+ShLen
OutFile.WriteBytes(Array As Byte(Asc("d"),Asc("a"),Asc("t"),Asc("a")),0,StrLen,Pos)
Pos=Pos+IntLen
OutFile.WriteInt(0,Pos) 'Data chunk size (Not yet known)
Log("Pos "&Pos)
End Sub
Is this because debugging is not possible with threads ?
RP.CheckAndRequest(RP.PERMISSION_RECORD_AUDIO)
Wait for Activity_PermissionResult(permission As String, Result As Boolean)
If Result = True Then
AR.Initialize(AudioSource,SampleRate,ChannelConfig,AudioFormat,BufferSize)
Log("permission ok")
Else
Log("permission false")
Return
End If
Sub Rec_Ended(endedOK As Boolean,Error As String)
Log("Ended " & endedOK)
'Stop recording and release resources
AR.Stop
AR.release
If endedOK Then
'Finish writing the WAVE Header
UpdateHeader
Log("Thread Rec EndedOK "&endedOK&" "&Error)
'Load the recorded file into Media player as a test
MP.Load(File.DirDefaultExternal,"test.wav")
MP.Play
End If
End Sub
It would be easier if you post the whole module, or better still, zip and post the project (File/Export as zip).
When does the error occur?
Couldn't find any reason that it wouldn't work.
Try the attached, hopefully this will work. It runs on my Nexus 7 and Samsung J3 (Android 8).
I have changed the channel config to stereo in case your device doesn't support mono recording. This is just a really basic example, you will need to consider how you want it to work and implement the workflow as you'd like.
Couldn't find any reason that it wouldn't work.
Try the attached, hopefully this will work. It runs on my Nexus 7 and Samsung J3 (Android 8).
I have changed the channel config to stereo in case your device doesn't support mono recording. This is just a really basic example, you will need to consider how you want it to work and implement the workflow as you'd like.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
1 <------------- logged by Initialize sub of Record
true
Playback Buffer Size 14144
1
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
0 <------------------- logged by IsReady sub of Record
Ar Not Ready
** Activity (main) Pause, UserClosed = false **
For some reason the Audiorecord object is not being initialized.
If you look at the code in the Record class, you will see that the AR.initialize call is in a try block, do you see the last exception in the logs when it fails?
Try
AR.Initialize(AudioSource,SampleRate,ChannelConfig,AudioFormat,BufferSize)
Catch
Log(LastException)
Return False
End Try
I put the check in because I was getting a crash when pressing the record button too soon.
If the initialize sub is returning true, the initilization can't be failing.
You can try commenting out the ready test in the bthRecord_Click sub and see what happens.
Probably not what you want to here, but there appear to be a lot of problems with the microphone on the Oneplus 5T.
Try googling for Oneplus 5T microphone.
This may have something to do with it. Do you have headphones with a mic you could try?
Damn!
As you can see from the code, the IsReady test just double checks that the AudioRecord object is initialized. So the initialization is not failing with a catchable error, but it's not working either. As I said, I added the test because it would crash if I tried to press the record button immediately after the app started.
You could try starting the app, and giving it a few seconds (maybe 15-20) before trying to record and see if that works.
Alternatively do you have another device to try it on, as much for my peace of mind than anything else.
The only other thing I can think of is to try different samplerates to see if it will work on a different one. You will have to change the sample rate in the playback class as well.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?