#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