#Region Project Attributes
#ApplicationLabel: B4A 1
#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.
Dim TTS1 As TTS
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 = "1.wav"
Private mp As MediaPlayer
Private rp As RuntimePermissions
End Sub
Sub Globals
Dim Label1 As Label
Dim btnPlay As Button
Dim btnStartRecording As Button
Dim btn_leer_texto As Button
Dim EditText1 As EditText
Dim spnrLanguages As Spinner
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
'shared = rp.GetSafeDirDefaultExternal("")
'Dim testFolder As String = rp.GetSafeDirDefaultExternal("test")
'File.WriteString(testFolder, "test.txt","aaa")
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
Wait for activity_permissionResult (Permission As String, result As Boolean)
spnrLanguages.AddAll(Array As String("en", "fr", "de","spa"))
TTS1.Initialize("TTS1")
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
'Botón para leer el texto que hayamos escrito
Sub btn_leer_texto_Click
TTS1.Speak(EditText1.Text, True)
End Sub
'Selector de idioma
Sub spnrLanguages_ItemClick (Position As Int, Value As Object)
If TTS1.SetLanguage(Value, "") = False Then
ToastMessageShow("Language data not found.", True)
Return
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
'Botón para iniciar la grabación
Sub btnStartRecording_Click
buffers.Clear
output = StartWaveFile(File.DirRootExternal, 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
'Botón para detener la grabación
Sub btnStopRecording_Click
streamer.StopRecording
recording = False
output.Close
CloseWaveFile(File.DirRootExternal, mFileName)
timer1.Enabled = False
btnPlay.Enabled = True
Label1.Text = ""
End Sub
'Botón para reproducir lo que hemos grabado
Sub btnPlay_Click
mp.Load(File.DirRootExternal, 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