Android Question Initial run of RecordToWaveFile example causes error if targetSDKversion>=23

toby

Well-Known Member
Licensed User
Longtime User
My test app is a modified version of the RecordToWaveFile example accompanying the Audio Library 1.5. My changes are necessary in order for it to work with newer versions of Android. What I've changed
1. MinSDK version from 4 to 14
2. TargetSDK version from 14 to 23
3. Get folder using rp.GetSafeDirDefaultExternal("recordings")
4. CheckAndRequest RECORD_AUDIO runtimepermission

When running the test app the first time on a device, this line of code
B4X:
streamer.StartRecording
will trigger the following error
error:
createRecord returned error -22
AudioFlinger could not create record track, status: -22
Error creating AudioRecord instance: initialization check failed with status -22.
Error code -20 when initializing native AudioRecord object.
A wave file of 44 bytes would be created in the specified folder.

Subsequent runs of the app on the same device would work without any problem.

No error if the TargetSDK number is 22 or lower, and error would occurs if it's between 23-29

Could someone tell me what's wrong and how to fix it. Test app attached.

Thank you very much in advance!

Logger connected to: asus ASUS_X017DA
--------- beginning of main
Not starting debugger since process cannot load the jdwp agent.
The ClassLoaderContext is a special shared library.
Connecting to perf service.
SetAppTypeFace- try to flip, app = b4a.example.recordwavefile
Typeface getFontPathFlipFont - systemFont = default#default
SetAppTypeFace- try to flip, app = b4a.example.recordwavefile
Typeface getFontPathFlipFont - systemFont = default#default
Class anywheresoftware.b4a.BA failed lock verification and will run slower.
Common causes for lock verification issues are non-optimized dex code
and incorrect proguard optimizations.
common created.
Starting remote logger. Port: 28284
Skia GL Pipeline
QUALCOMM build : cf57c9c, I1cb5c4d1cc
Build Date : 10/15/18
OpenGL ES Shader Compiler Version: EV031.25.03.01
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.UM.7.2.R1.09.00.00.442.049
Remote Branch : NONE
Reconstruct Branch : NOTHING
Build Config : S L 6.0.7 AArch64
Loading /vendor/lib64/hw/gralloc.sdm660.so from current namespace instead of sphal namespace.
type=1400 audit(0.0:483): avc: denied { search } for name="proc" dev="debugfs" ino=4856 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:eek:bject_r:qti_debugfs:s0 tclass=dir permissive=0
*** Debugger waiting for connection (0) ***
PFP: 0x005ff110, ME: 0x005ff066
android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
Initialized EGL, version 1.4
Swap behavior 2
Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
Loading /vendor/lib64/hw/gralloc.sdm660.so from current namespace instead of sphal namespace.
*** Debugger waiting for connection (1) ***
Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
createRecord returned error -22
AudioFlinger could not create record track, status: -22
Error creating AudioRecord instance: initialization check failed with status -22.
Error code -20 when initializing native AudioRecord object.
Use of stream types is deprecated for operations other than volume control
See the documentation of AudioTrack() for what to use instead with android.media.AudioAttributes to qualify your playback use case
Class androidx.core.content.ContextCompat failed lock verification and will run slower.
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
After accept
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
Error occurred on line: 105 (Main)
java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.media.AudioRecord.startRecording(AudioRecord.java:993)
at anywheresoftware.b4a.audio.AudioStreamer.StartRecording(AudioStreamer.java:98)
at b4a.example.recordwavefile.main._btnstartrecording_click(main.java:564)
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.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6615)
at android.view.View.performClickInternal(View.java:6592)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25907)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6732)
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:858)
null: 25
 

Attachments

  • RecordToWaveFile_toby_test.zip
    8.8 KB · Views: 244

toby

Well-Known Member
Licensed User
Longtime User
I solved it by first checking the RECORD_AUDIO runtime permission, then initializing the audio streamer object. I put them in reverse order incorrectly.
 
Upvote 0
Top