Android Question AudioRecordApp.Record give Object should first be initialized

mtechteam

Member
Licensed User
I copied the sample code for AudioRecordApp directly from the help in the library and I am getting the following error.

java.lang.RuntimeException: Object should first be initialized (List).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.collections.List.getSize(List.java:129)
at anywheresoftware.b4a.audio.AudioRecordApp.Record(AudioRecordApp.java:97)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$1.run(BA.java:335)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)


Apparently a list within the AudioRecordApp has not been initialized. Any ideas what is going on?
(The VideoRecordApp using Record3 is working fine)
 

MikeSW17

Active Member
Licensed User
1) Are you talking about this Library? https://www.b4x.com/android/forum/threads/audiorecord-library.13993/
2) What happens when you run the attached sample (artest) - As it's *very* old, you'll need at least to edit the sdk versions in the manifest.

So much of the code is 'old' you may need to many updates to the test project code.

When I try the artest project, with just sdk version changes, it crashes at line 85,
B4X:
    OutFile.Initialize2(File.DirDefaultExternal,"test.wav",False,True)
B4A tells us all references to 'DirDefaultExternal' needs to be changed.
 

mtechteam

Member
Licensed User
Thank you. I did find that and as I mentioned above, the Video one is working for me (because of that thread). How should I change the Audio example to also work? Or does it need a new "Record3" method as well?
 

Erel

Administrator
Staff member
Licensed User
I don't recommend using this intent. Seems like there isn't a matching app by default.

You can try this code (B4XPages):
B4X:
Private Sub Button1_Click
    Dim in As Intent
    in.Initialize("android.provider.MediaStore.RECORD_SOUND", "")
    Dim pm As PackageManager
    Log(pm.QueryIntentActivities(in))
    StartActivityForResult(in)
    Wait For ion_Event (MethodName As String, Args() As Object)
    Log(MethodName)
    Log(Args(0))
End Sub

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub
 

mtechteam

Member
Licensed User
ion was not declared, so I declared it as an object (Dim ion as Object) in Process_Globals. This allowed it to compile, but when attempting to run it, I received this:

(The line is "Return jo.RunMethod("getBA", Null)" - it's not line 95 below as given because I removed the top two region definitions.)

Error occurred on line: 95 (Main)
java.lang.RuntimeException: Method: getBA not found in: org.techteam.itsyours.main
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at org.techteam.itsyours.main._getba(main.java:468)
at org.techteam.itsyours.main._startactivityforresult(main.java:552)
at org.techteam.itsyours.main$ResumableSub_RecordAudio_Click.resume(main.java:516)
at org.techteam.itsyours.main._recordaudio_click(main.java:478)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$1.run(BA.java:335)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

I changed
Return jo.RunMethod("getBA", Null)
to
Return jo.RunMethod("GetBA", Null)
but that didn't seem to make any difference.

Here is my code (minus the starter service and accompanying FileProvider class):
B4X:
Sub Process_Globals
    Dim audioRecorder As AudioRecordApp
    Dim videoRecorder As VideoRecordApp
    Dim MP As MediaPlayer
    Dim ion As Object
End Sub

Sub Globals
    Dim vv As VideoView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        audioRecorder.Initialize("audioRecorder")
        videoRecorder.Initialize("videoRecorder")
    End If
    vv.Initialize("vv")
    Activity.AddView(vv, 0, 0, 100%x, 100%y)
    Activity.AddMenuItem("Record Video", "RecordVideo")
    Activity.AddMenuItem("Record Audio", "RecordAudio")
    ToastMessageShow("Press on Menu button...", True)

End Sub

Sub RecordVideo_Click
    Dim folder As String = Starter.Provider.SharedFolder
    Dim FileName As String = "1.mp4"
    videoRecorder.Record3(folder, FileName, -1, Starter.Provider.GetFileUri(FileName))
End Sub

Sub videoRecorder_RecordComplete (Success As Boolean)
    Log(Success)
    If Success Then
        MP.Initialize2("MP")
        MP.Load(Starter.Provider.SharedFolder, "1.mp4")
        MP.Play
        'vv.LoadVideo(File.DirRootExternal, Starter.Provider.GetFileUri("1.mp4"))
        'vv.Play
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub RecordAudio_Click
    Dim in As Intent
    in.Initialize("android.provider.MediaStore.RECORD_SOUND", "")
    Dim pm As PackageManager
    Log(pm.QueryIntentActivities(in))
    StartActivityForResult(in)
    Wait For ion_Event (MethodName As String, Args() As Object)
    Log(MethodName)
    Log(Args(0))
End Sub

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub
 

MikeSW17

Active Member
Licensed User
(The line is "Return jo.RunMethod("getBA", Null)" - it's not line 95 below as given because I removed the top two region definitions.)
I don't follow how line 95 isn't really line 95. The line given in the error log will relate to the code being executed?
Are you sure you've recompiled and installed the app?
 

mtechteam

Member
Licensed User
Upgraded to B4a 10.5. Perfect. That works great. Can you tell me how to know the name of the file? It shows as "Voice 007" on the display to record and stored it in the Voice Recorder folder. But programmatically how can I know that? And using the newer scheme of FileProvider is probably needed as well?
 

mtechteam

Member
Licensed User
Output for this code
B4X:
Private Sub Button1_Click
    Dim in As Intent
    in.Initialize("android.provider.MediaStore.RECORD_SOUND", "")
    Dim pm As PackageManager
    Log("#1 " & pm.QueryIntentActivities(in))
    StartActivityForResult(in)
    Wait For ion_Event (MethodName As String, Args() As Object)
    Log("#2 " & MethodName)
    Log("#3 " & Args(0))
    Dim resultCode As Int = Args(0)
    If resultCode = -1 Then ' Ok
        Dim in As Intent = Args(1)
        Log("#4 " & in)
        Log("#5 " & in.GetData)
    End If
End Sub
is:


#1 (ArrayList) [com.sec.android.app.voicenote/.activity.SimpleActivity]
** Activity (main) Pause event (activity is not paused). **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
#2 ResultArrived
#3 -1
#4 (Intent) Intent { dat=content://media/external/audio/media/271 flg=0x1 }
#5 content://media/external/audio/media/271
** Activity (main) Resume **
** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**
 

mtechteam

Member
Licensed User
Got it. I have a file that I can play in various programs. Thank you for all the follow through. As many have said before, B4X is a great product suite!
 
Top