How to add functionality for recording audio into wav file programmatically in API 23 (Android 6)?
: startRecording() called on an uninitialized AudioRecord
Looks like you forget to initialize
Sure you copy all code from example?
#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 streamer As AudioStreamer
Private buffers As List
Private timer1 As Timer
Private recordingStart As Long
Private output As OutputStream
Private recording As Boolean
Private mBitRate As Int = 16
Private mSampleRate As Int = 22050
Private mMono As Boolean = True
Private mFileName As String = "sos.wav"
Private mp As MediaPlayer
End Sub
Sub Globals
Dim Label1 As Label
Dim btnPlay As Button
Dim btnStartRecording As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("sos")
If FirstTime Then
streamer.Initialize("streamer", mSampleRate, mMono, mBitRate, streamer.VOLUME_MUSIC)
buffers.Initialize
timer1.Initialize("timer1", 1000)
mp.Initialize2("mp")
End If
End Sub
Sub StartWaveFile(Dir As String, FileName As String, SampleRate As Int, Mono As Boolean _
, BitsPerSample As Int) As OutputStream
File.Delete(Dir, FileName)
Dim raf As RandomAccessFile
raf.Initialize2(Dir, FileName, False, True)
raf.WriteBytes("RIFF".GetBytes("ASCII"), 0, 4, raf.CurrentPosition)
raf.CurrentPosition = 8 'skip 4 bytes for the size
raf.WriteBytes("WAVE".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
raf.WriteBytes("fmt ".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
raf.WriteInt(16, raf.CurrentPosition)
raf.WriteShort(1, raf.CurrentPosition)
Dim numberOfChannels As Int
If Mono Then numberOfChannels = 1 Else numberOfChannels = 2
raf.WriteShort(numberOfChannels, raf.CurrentPosition)
raf.WriteInt(SampleRate, raf.CurrentPosition)
raf.WriteInt(SampleRate * numberOfChannels * BitsPerSample / 8, raf.CurrentPosition)
raf.WriteShort(numberOfChannels * BitsPerSample / 8, raf.CurrentPosition)
raf.WriteShort(BitsPerSample, raf.CurrentPosition)
raf.WriteBytes("data".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
raf.WriteInt(0, raf.CurrentPosition)
raf.Close
Return File.OpenOutput(Dir, FileName, True)
End Sub
Sub CloseWaveFile(Dir As String, FileName As String)
Dim raf As RandomAccessFile
raf.Initialize2(Dir, FileName, False, True)
raf.WriteInt(raf.Size - 8, 4)
raf.WriteInt(raf.Size - 44, 40)
raf.Close
End Sub
Sub streamer_RecordBuffer (Buffer() As Byte)
If recording Then output.WriteBytes(Buffer, 0, Buffer.Length)
'the above check is required as the last message will arrive after we call StopRecording.
End Sub
Sub btnStartRecording_Click
buffers.Clear
output = StartWaveFile(File.DirInternalCache, mFileName, mSampleRate, mMono, mBitRate)
recording = True
streamer.StartRecording
recordingStart = DateTime.Now
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
streamer.StopRecording
recording = False
output.Close
CloseWaveFile(File.DirInternalCache, mFileName)
timer1.Enabled = False
btnPlay.Enabled = True
Label1.Text = ""
End Sub
Sub btnPlay_Click
mp.Load(File.DirInternalCache, mFileName)
mp.Play
End Sub
Sub mp_Complete
Log("PlaybackComplete")
btnStartRecording.Enabled = True
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Use code Tags when Posting code!
Edit your post to make the code read able. Or better. Export the project as zip
You dont need to do this for me as i put you on my ignorelist
can i send u exported project as an email?Please upload the project (File - Export as zip).
here is the project erel....I'm sorry but it is not possible. Try to reproduce it in a small project and upload it.
Note that the layout file is missing from the zip.
The RECORD_AUDIO permission is a "dangerous" permission. If you set the targetSdkVersion to 23 then you must request this permission at runtime:
Runtime Permissions (Android 6.0+ Permissions)
but you start recording without waiting for the permission-request-resultadded runtime permission "RECORD_AUDIO permission", but still error...
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
If Permission = Starter.rp.PERMISSION_RECORD_AUDIO Then
If Result Then
' NOW you are allowed to record audio...
' Wait to this and set a process global variable to be allowed....
' Start record ONLY after this code here runs...
End If
End If
End Sub
The issue has nothing to do with Firebase. If you set the targetSdkVersion to 23 then you need to handle runtime permissions. See the tutorial.any update?
tq erel,The issue has nothing to do with Firebase. If you set the targetSdkVersion to 23 then you need to handle runtime permissions. See the tutorial.
but you start recording without waiting for the permission-request-result
B4X:Sub Activity_PermissionResult (Permission As String, Result As Boolean) If Permission = Starter.rp.PERMISSION_RECORD_AUDIO Then If Result Then ' NOW you are allowed to record audio... ' Wait to this and set a process global variable to be allowed.... ' Start record ONLY after this code here runs... End If End If End Sub