Italian [Risolto] [B4A] Registratore non registra

Aldo's

Active Member
Licensed User
Ho sviluppato, partendo da un esempio di Erel questo registratore.

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
    Private aFile(255) As String
    Private sFilePlay As String
    Private sDir As String = File.DirInternal
    Private mp As MediaPlayer

    Private mB4XDialog As B4XDialog

    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 sFileRec As String
    Private lDur As Long

    Private clvRec As CustomListView
    Private lblFile As B4XView
    Private lbl1 As B4XView
    Private lblRiga As B4XView
    Private lblTitolo As B4XView
    Private txtRiga As B4XView
    Private bttPlay As B4XView
    Private bttPause As B4XView
    Private bttStop As B4XView
    Private bttNuovo As B4XView
End Sub

Public Sub Initialize As Object
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("RegistratoreLO")
    mB4XDialog.Initialize(Root)
    streamer.Initialize("streamer", mSampleRate, mMono, mBitRate, streamer.VOLUME_VOICE_CALL)
    buffers.Initialize
    timer1.Initialize("timer1", 1000)
End Sub

private Sub B4XPage_Appear
    CaricaCLV
End Sub

Private Sub CaricaCLV
    Dim lFile As List = File.ListFiles(sDir)
    Dim iNum As Int = lFile.Size
    Dim iOK As Int = 0
    For index=0 To iNum-1
        Dim sAppo As String = lFile.Get(index)
        If sAppo.SubString(sAppo.Length - 4) = ".wav" Then
            aFile(index)=lFile.Get(index)
            iOK = iOK+1
        End If
    Next
    clvRec.Clear
    For i = 0 To iOK-1
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0,0,0,500dip,30dip)
        p.LoadLayout("ItemCLVRec")
        lblFile.Text=aFile(i)
        clvRec.Add(p,"")
    Next
End Sub

Private Sub clvRec_ItemClick (Index As Int, Value As Object)
    lbl1.As(Label).Text=aFile(Index)
    sFilePlay=aFile(Index)
    mp.Initialize
    mp.Load(sDir, sFilePlay)
End Sub

Private Sub bttPlay_Click
    mp.Play
End Sub

Private Sub bttPause_Click
    mp.Pause
End Sub

Private Sub bttStop_Click
    mp.Stop
    mp.Load(sDir, sFilePlay)
End Sub

Private Sub bttRec_Click
    Dim ime1 As IME
    Dim pnlDialog As B4XView = xui.CreatePanel("")
    pnlDialog.SetLayoutAnimated(0, 0, 0, 300dip, 100dip)
    pnlDialog.LoadLayout("InputboxLO")
    
    Dim Resum As ResumableSub = mB4XDialog.ShowCustom(pnlDialog, "Si", "No", "")
    mB4XDialog.Base.Color = xui.Color_white
    mB4XDialog.Base.SetColorAndBorder(xui.Color_white, 1dip, xui.Color_gray, 0)

    Dim btnYes As B4XView = mB4XDialog.GetButton(xui.DialogResponse_Positive)
    btnYes.Height = btnYes.Height * .8
    btnYes.Left = btnYes.Left * .99
    btnYes.Color = xui.Color_lightgray
    btnYes.TextColor = xui.Color_black
    
    Dim btnNo As B4XView = mB4XDialog.GetButton(xui.DialogResponse_Negative)
    btnNo.Height = btnNo.Height * .8
    btnNo.Left = btnNo.Left * .99
    btnNo.Color = xui.Color_lightGray
    btnNo.TextColor = xui.Color_black
    
    lblTitolo.As(Label).Color=xui.Color_ARGB(255,0,0,139)
    lblTitolo.As(Label).TextColor=xui.Color_white
    lblTitolo.As(Label).Text="  Nuova Registrazione"
    
    lblRiga.As(Label).Textcolor=xui.Color_ARGB(255,0,0,139)
    lblRiga.As(Label).Text="Nome della registrazione:"
    
    txtRiga.As(EditText).Color=xui.Color_White
    txtRiga.As(EditText).Textcolor=xui.Color_ARGB(255,0,0,139)
    txtRiga.As(EditText).Text=""
    txtRiga.As(EditText).RequestFocus
    ime1.Initialize("ime1")
    ime1.ShowKeyboard(txtRiga.As(EditText))
    
    Wait For (Resum) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        sFileRec=txtRiga.As(EditText).Text & ".wav"
        lbl1.As(Label).Text=sFileRec
    End If
    buffers.Clear
    output = StartWaveFile(sDir, sFileRec, mSampleRate, mMono, mBitRate)
    recording = True
    streamer.StartRecording
    recordingStart = DateTime.Now
    timer1.Enabled = True
    Timer1_Tick
    bttPlay.As(Button).Enabled = False
    bttNuovo.As(Button).Enabled = False
    bttPause.As(Button).Enabled = False
    bttStop.As(Button).Enabled = False
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 Timer1_Tick
    lDur=Round((DateTime.Now - recordingStart) / DateTime.TicksPerSecond)
End Sub

Private Sub bttStopRec_Click
    streamer.StopRecording
    recording = False
    output.Close
    CloseWaveFile(sDir, sFileRec)
    timer1.Enabled = False
    lbl1.As(Label).Text=sFileRec
    CaricaCLV
    bttPlay.As(Button).Enabled = True
    bttNuovo.As(Button).Enabled = True
    bttPause.As(Button).Enabled = True
    bttStop.As(Button).Enabled = 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
    sFilePlay=sFileRec
    mp.Initialize
    mp.Load(sDir,sFilePlay)
    sFileRec=""
End Sub

Premesso che ho chiesto il permesso RECORD_AUDIO e il CheckAndRequest mi restituisce True (quindi tutto ok), quando eseguo il debug non effettua alcuna registrazione. Mi scrive il file ma se provo ad ascoltarlo si sente un fruscio per pochi secondi e nient'altro.
Cosa sbaglio?
 

Aldo's

Active Member
Licensed User
Qualcosa senti e per N secondi, quindi sembrerebbe che qualcosa non funzioni (microfono?).
Sull'esempio di Erel mi ha registrato (solo che lui ha messo un file wav fisso, senza la possibilità di cambiare nome). Quindi il microfono dovrebbe funzionare...comunque provo col tablet e ti dico.

[Hai pubblicato tutto il sorgente, mi pare, per cui tanto valeva allegare il progetto ?]
Beh è per non farsi mancare niente. ?
 

LucaMs

Expert
Licensed User
Longtime User
Mah, non ho molta pazienza (nell'analizzare il tuo sorgente).
Metti qualche Log e qualche Breakpoint.

Ci sono cose che non mi quadrano/piacciono riguardo la CustomListView ma non credo sia questo il problema.
 

LucaMs

Expert
Licensed User
Longtime User
1707925945028.png


Perché qui non scrivi:

sFilePlay = FileName
mp.Load(Dir, FileName)

?

Prova.
 

Aldo's

Active Member
Licensed User
Ho provato a pulire qualcosa e a mettere qualche Log (che mi restituisce i valori che mi aspetto).
Non mi funziona!
Ho provato con un'altra app il microfono e funziona e ho provato con un altro device e non funziona.
Allego il progetto.
Qualcuno prova a fare il debug e a dirmi cosa ne viene fuori?
 

Attachments

  • Project.zip
    20.4 KB · Views: 121

Aldo's

Active Member
Licensed User
Ho riscritto e sono riuscito a farlo funzionare...allego il risultato.
 

Attachments

  • Project.zip
    20.5 KB · Views: 112
Top