Android Question RAF.Size (Solved by Klaus !)

Marc DANIEL

Well-Known Member
Licensed User
Hello. In my MusicBoxPlayer application, the "SoundRecorder" function only works with "RAF.Size" values less than 400000. This happens without any error message. What should I change to be able to save larger files from "File.DirInternal" where they were saved, to external storage?

Sub BtnSave_Click:
Sub BtnSave_Click
    Msgbox2Async("Do you really want to save this sound?", "Save this sound", "Yes", "", "No", Null, True)
    Wait For Msgbox_Result (Result As Int)
    If Result=DialogResponse.POSITIVE Then
    mFileName = DateTime.GetYear(DateTime.Now) & "-" & DateTime.GetMonth(DateTime.Now) & "-" & DateTime.GetDayOfMonth(DateTime.Now)
    mFileName = mFileName & "-" & DateTime.Gethour(DateTime.Now) & "h" & DateTime.GetMinute(DateTime.Now) & ".wav"
    FileName = mFileName
        Log("Save: " & FileName)
        CopyFileToExternalStorage(File.DirInternal, FileName, Storage.root, "")
        btnPlay.Visible=False
        btnSave.Visible = False
        Label1.Text = "Recording: " & FileName & " on your external storage"
        Sleep(6000)
        Label1.Text = "To check if this file has been saved," & CRLF & "return to the MusicBoxCenter" & CRLF & "and display the file list."
        Sleep(6000)
        Label1.Text = ""
    End If
End Sub

RAF:
Sub StartWaveFile(Dir3 As String, FileName3 As String, SampleRate As Int, Mono As Boolean _
        , BitsPerSample As Int) As OutputStream
    File.Delete(Dir3, FileName3)
    Dim raf As RandomAccessFile
    raf.Initialize2(Dir3, FileName3, 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(Dir3, FileName3, True)
End Sub

Sub CloseWaveFile(Dir3 As String, FileName3 As String)
    Dim raf As RandomAccessFile
    raf.Initialize2(Dir3, FileName3, False, True)
    raf.WriteInt(raf.Size - 8, 4)
    raf.WriteInt(raf.Size - 44, 40)
    Log("RAF Size: "& raf.Size)
    raf.Close
End Sub

Sub BtnSR_Click:
Sub btnSR_Click
    btnStop.Enabled = True
    rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        xui.MsgboxAsync("No permission", "")
        Return
    Else
        mFileName = DateTime.GetYear(DateTime.Now) & "-" & DateTime.GetMonth(DateTime.Now) & "-" & DateTime.GetDayOfMonth(DateTime.Now)
        mFileName = mFileName & "-" & DateTime.Gethour(DateTime.Now) & "h" & DateTime.GetMinute(DateTime.Now) & ".wav"
        buffers.Clear
        Output = StartWaveFile(File.DirInternal, mFileName, mSampleRate, mMono, mBitRate)
        Recording = True
        streamer.StartRecording
        recordingStart = DateTime.Now
        timer1.Enabled = True
        Timer1_Tick
        btnPlay.Enabled = False
        btnSR.Enabled = False
        Log("SR: " & mFileName)
    End If
End Sub
 
Last edited:

emexes

Expert
Licensed User
I am wondering if the time changes between when you construct the filename when starting recording, and when you (re?)construct the filename when saving/copying the file.
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
Yes, I asked myself this question because it does not happen for short files of less than 12 seconds for example.

Long recordings are stored in "File.DirInternal" since I can listen to them in full afterwards, but the problem arises when I want to copy them to external storage.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I am wondering if the time changes between when you construct the filename when starting recording, and when you (re?)construct the filename when saving/copying the file.
This is exactly the problem.
I downloaded your project and tried to reproduce the problem.
You define mFileName in btnSR_Click.
Then, when you save the file you redefine the file name in BtnSave_Click, but when the minute has changed the file name is different and is of course not found in File.DirInternal.
I added a new global variable RecordedFileName, which is defined in btnSR_Click and use this one for the saving.
And it works.
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
Okay, Klaus, thanks for the help. Actually, as soon as the recording goes beyond a minute, my project crashes. I'll try to fix this immediately.
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
I fixed the bug, Klaus, and it works perfectly, I save long files!!! Thanks again for your flair and professionalism !

I will quickly update the download files
 
Upvote 0
Top