Android Question Convert to B4a code

N. Franz

Member
I'm trying to create an audio viewer. I found this code but I can't implement it in B4a
I need help.

Java code:
protected void onDraw(Canvas canvas) {
        if (bytes != null) {
            float barWidth = getWidth() / density;
            float div = bytes.length / density;
            paint.setStrokeWidth(barWidth - gap);

            for (int i = 0; i < density; i++) {
                int bytePosition = (int) Math.ceil(i * div);
                int top = getHeight() +
                        ((byte) (Math.abs(bytes[bytePosition]) + 128)) * getHeight() / 128;
                float barX = (i * barWidth) + (barWidth / 2);
                canvas.drawLine(barX, getHeight(), barX, top, paint);
            }
            super.onDraw(canvas);
        }
    }
 

TILogistic

Expert
Licensed User
Longtime User
see:
 
Upvote 2

N. Franz

Member
I've been trying again and again. But I'm not sure I made it.
For some reason I can't clean the panel to redraw, it just overdraws.
Codigo:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private MP As MediaPlayer
    Private RP As RuntimePermissions
    Private Visualizer As Vizualizer
    Private JO As JavaObject
    Private mDensity As Int = 10
    Private Gap As Int = 4
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Panel As B4XView
    Private CV As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    JO.InitializeContext
    Activity.LoadLayout("Layout")
    If Not(RP.Check(RP.PERMISSION_RECORD_AUDIO)) Then
        RP.CheckAndRequest(RP.PERMISSION_RECORD_AUDIO)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Not(Result) Then ExitApplication
    End If
    MP.Initialize
    MP.Load(File.DirAssets,"Happiness.mp3")
    MP.Play
    CV.Initialize(Panel)
    Visualizer.Initialize(Me,MP.As(JavaObject).GetFieldJO("mp").RunMethod("getAudioSessionId",Null))
    Visualizer.SetDataCaptureListener(Visualizer.GetMaxCaptureRate / 2,True,False)
    Visualizer.Enabled = True
End Sub

Sub Wave_Capture(Bytes() As Byte,SamplingRate As Int)
    If Bytes = Null Then Return

    Dim BarWidth As Float = Panel.Width / mDensity
    Dim Div As Float = Bytes.Length / mDensity ' 102.4
    For i = 0 To mDensity -1
        Dim BytePosition As Int = Ceil(i*Div)
        Dim Top As Int = JO.RunMethod("getHeight",Array(Bytes(BytePosition),Panel.Height))
        Dim BarX As Float = (i * BarWidth) + (BarWidth/2)

        CV.DrawLine(BarX,Panel.Height,BarX,Top,Colors.Cyan,BarWidth - Gap)
        Panel.As(Panel).Invalidate
    Next

End Sub
#if java
public int getHeight(byte Byte,int Height){
            int top = Height +
                        ((byte) (Math.abs(Byte) + 128)) * Height / 128;
            return top;
        }
#End If
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
At first glance I don't see the difference between collecting the sound information and displaying (part of) the collected sound information. Due to the lack of this you will automatically get the overwrite effect. You should start displaying in the panel until the full width is used and then you should start working with an offset. That requires a choice of whether you use real-time and, above all, the choice of how you are going to use that necessary offset.
 
Upvote 0
Top