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: 579
  • b4aAndroidRecording.zip
    8.7 KB · Views: 705
  • b4aLibFiles.zip
    98.5 KB · Views: 751
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
none

If you want can try directly the applicazion.
thank
Does the original project in post #1 work? The sound file should be created and stored in the "root" folder of your device. I seem to remember that is how the original Java Lib was set up.
 

Johan Schoeman

Expert
Licensed User
Longtime User
From the Java lib

B4X:
    public static boolean checkExternalStorageAvailable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        else {
            return false;
        }
    }
    
    public static String getFileName(boolean isAudio) {
        String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        return String.format("%s/%s", storageDir, (isAudio) ? AUDIO_FILE_NAME : VIDEO_FILE_NAME);
    }
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi Johan

Yes, the original example work, but save the file in the root. Can you help me to change the directory where the file is saved?

thank
 

hatzisn

Expert
Licensed User
Longtime User
Hi everyone,

can somebody inform me on how to deal with github? I found this page in stackoverflow:

https://stackoverflow.com/questions/4644351/android-music-track-visualization


that creates a visual output for a playing sound file in the device and I do not know what to do with it and how can I manipulate it.
It is obvious that my knowledge in this is totally sub-zero... Can someone more advanced inform me and give me some advice (also where
to look for the code) ?

Thanks in advance...


Edit - Found it - Duuuh? - src...
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan

Yes, the original example work, but save the file in the root. Can you help me to change the directory where the file is saved?

thank
Try with the attached lib files (V1.01). You will have to use the normal B4A "File" code to create the folder, etc.


B4X:
    ar1.VisualizerColor = Colors.Yellow
    ar1.VisualizerBarWidth = 2.0
    ar1.DrawBarsFromTop = False
    ar1.VisualizerDivisions = 2
    DateTime.DateFormat = "yyMMddHHmmss"
    Dim filename As String = File.Combine(File.DirRootExternal & "/Myfolder", "myRecording" & DateTime.Date(DateTime.Now)) & ".wav"
    ar1.RecordingFileName = filename
 

Attachments

  • AndroidRecording.xml
    10.7 KB · Views: 329
  • AndroidRecording.jar
    32.5 KB · Views: 336
Top