B4A Library AndroidRecording - Voice / Sound recording with Visualizer

The attached project wraps part of this Github project (I only wanted to wrap the voice visualizer part of the project). It actually records the sound too and will store the yourfilename.wav file in the root folder of your device. See the attached sample project where I have added a date/time stamp to the filename.

You can amongst others set the colors of the bars and if the bars should extend from the top or bottom of the view

Posting the following:
1. B4A library files - copy them to your additional library folder
2. B4A demo project
3. Java source code - you will have to create a folder named libs on the same folder level as the src folder and then copy android-support-v4.jar and simplesound-0.1.jar into this folder in order to compile the Java project with Eclipse / SLC / etc. (simplesound-0.1.jar is included in the attached zip file that contains the B4A library files)

You will also need a recent version of android-support-v4.jar in your additional library folder. You can download it from HERE.

Take note of the permissions added to the Manifest in case you start a new B4A project.

BARS EXTENDING FROM THE TOP OF THE VIEW:
1.png


BARS EXTENDING FROM THE BOTTOM OF THE VIEW:
2.png



Sample Code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aAndroidRecording
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #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.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ar1 As VisualizerView
    Private b1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
 
    ar1.VisualizerColor = Colors.Yellow
    ar1.VisualizerBarWidth = 2.0
    ar1.DrawBarsFromTop = False
    ar1.VisualizerDivisions = 2
    DateTime.DateFormat = "yyMMddHHmmss"
    ar1.RecordingFileName = "myRecording" & DateTime.Date(DateTime.Now)         'it will be stored as a .wav file
 
    'set up all of the above before calling ar1.setupVisualizer
    ar1.setupVisualizer

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
    ar1.ReleaseResources

End Sub


Sub b1_Click
 
    ar1.StartStopRecording
 
End Sub

The library:

AndroidRecording
Author:
Github: Julia Zudikova, Wrapped by: Johan Schoeman
Version: 1
VisualizerView
Fields:

  • ba As BA
Methods:
  • BringToFront
  • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
  • Initialize (EventName As String)
  • Invalidate
  • Invalidate2 (arg0 As Rect)
  • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
  • IsInitialized As Boolean
  • ReleaseResources
  • RemoveView
  • RequestFocus As Boolean
  • SendToBack
  • SetBackgroundImage (arg0 As Bitmap)
  • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
  • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
  • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
  • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
  • StartStopRecording
  • setupVisualizer
Properties:
  • Background As Drawable
  • Color As Int [write only]
  • DrawBarsFromTop As Boolean [write only]
    Draw the bars from the top or bottom of the view
    The default value false
  • Enabled As Boolean
  • Height As Int
  • Left As Int
  • RecordingFileName As String [write only]
  • Tag As Object
  • Top As Int
  • Visible As Boolean
  • VisualizerBarWidth As Float [write only]
    Set the width of the visualizer bars
    The default value is 5.0f
  • VisualizerColor As Int [write only]
    Set the color of the visualizer bars
    The default color is Color.argb(200, 227, 69, 53)
  • VisualizerDivisions As Int [write only]
    divisions - must be a power of 2 (i.e 2, 4, 8, etc). Controls how many lines to draw
    The default value is 2
  • Width As Int
You can download and test any posting of mine in this thread but if you want to use it then you need to
 

Attachments

  • TheJavaCode.zip
    27.9 KB · Views: 460
  • b4aAndroidRecording.zip
    8.7 KB · Views: 584
  • b4aLibFiles.zip
    98.5 KB · Views: 619
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Is it possible to keep recording in background ?
Not too sure by what you mean "in background". Can you give some more detail about what it is that you want with "in background"?
 

Fusseldieb

Active Member
Licensed User
Longtime User
Not too sure by what you mean "in background". Can you give some more detail about what it is that you want with "in background"?
Sorry for my english, but i whould like to know if it is possible to keep recording if i exit the app, or something like that
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
aslo I have the problem with save the file in my directory?!

I'm using this path and others but doesn't work

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ar1         As VisualizerView
    Private b1             As Button
    
    Public rp As RuntimePermissions
    
End Sub



Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    Activity.LoadLayout("main")
    
    ar1.VisualizerColor             = Colors.Yellow
    ar1.VisualizerBarWidth         = 2.0
    ar1.DrawBarsFromTop             = False
    ar1.VisualizerDivisions     = 2
    DateTime.DateFormat             = "yyMMddHHmmss"
    
    
    Dim sAppRoot     As String                                    = rp.GetSafeDirDefaultExternal("Data")
    Log("Directory " & sAppRoot)
    
    Dim afileName    As String        =    sAppRoot & "/myRecording/" & DateTime.Date(DateTime.Now)
    Log("file name " & afileName)
            
    'Log("file path " & File.combine(sAppRoot, DateTime.Date(DateTime.Now) ))
    
    ar1.RecordingFileName         =  afileName ' "/myRecording/" & DateTime.Date(DateTime.Now)         'it will be stored as a .wav file
            
    'set up all of the above before calling ar1.setupVisualizer
    ar1.setupVisualizer

End Sub

anyone can help me
thank
 

stevel05

Expert
Licensed User
Longtime User
You will need to Make the directory before using it FIle.MakeDir. And you should use File.Combine to make sure that you are using the correct path separators, and add the file extension.

B4X:
Dim afileName    As String        = File.Combine(sAppRoot,File.Combine("myRecording",DateTime.Date(DateTime.Now) & ".wav"))
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Thank Stevel
I done your advice but it's doesn't work.
See code and error

B4X:
  Dim sAppRoot     As String                                    = rp.GetSafeDirDefaultExternal("Data")
    Log("Directory " & sAppRoot)
    
    File.MakeDir(sAppRoot, "myRecording")
    
    
    Dim afileName    As String        = File.Combine(sAppRoot, File.Combine("myRecording", DateTime.Date(DateTime.Now) & ".wav"))
    Log("file name " & afileName)
         
    
    ar1.RecordingFileName         = afileName

the directory was created.

this is the path

/storage/emulated/0/Android/data/JHS.b4aAndroidRecording/files/Data/myRecording/181201093053.wav


immagine.png


thank
 

stevel05

Expert
Licensed User
Longtime User
Are there any messages in the logs?
 

stevel05

Expert
Licensed User
Longtime User
I haven't used the library, perhaps someone that has can help.
 
Top