'Salva il file WAV sul device, memoria interna
Dim output As OutputStream
output = StartWaveFile(MP.DBFolder&"/audio", mFileName, MP.mSampleRate, MP.mMono, MP.mBitRate)
For Each buffer() As Byte In buffers
output.WriteBytes(buffer, 0, buffer.Length)
Next
output.Close
CloseWaveFile(MP.DBFolder&"/audio", mFileName)
'Predispone output di WAV file
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
'Finalizza e chiude WAV file
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)
Log(raf.size)
raf.Close
End Sub
Wow, grazie per la spiegazione e la condivisione.Ciao. Quello che chiedi è esattamente ciò che ho fatto per JustNotes, la mia app personale per la registrazione di note testuali ed audio.
Il file audio prodotto dal componente è un WAV. La particolarità di AudioStreamer è che la registrazione avviene in blocchi (i buffer che vedi menzionati) generati automaticamente. Quindi, quando salvi, devi salvare tutti i blocchi.
Il formato WAV ha una sua struttura specifica e per tale motivo il salvataggio su file avviene in due tempi, prima i buffer e poi la lunghezza totale.
B4X:'Salva il file WAV sul device, memoria interna Dim output As OutputStream output = StartWaveFile(MP.DBFolder&"/audio", mFileName, MP.mSampleRate, MP.mMono, MP.mBitRate) For Each buffer() As Byte In buffers output.WriteBytes(buffer, 0, buffer.Length) Next output.Close CloseWaveFile(MP.DBFolder&"/audio", mFileName) 'Predispone output di WAV file 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 'Finalizza e chiude WAV file 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) Log(raf.size) raf.Close End Sub
If mp.IsInitialized And File.Exists(DBFolder&"/audio", mfilename) Then
Try
mp.Load(DBFolder&"/audio", mfilename)
mp.Play
Catch
Log(LastException)
End Try
End If