androidkotlinvisualizer

Can't initialise Visualizer on Android using Kotlin


I am currently having trouble while trying to display a "blast" view from the media playing, using this library: https://github.com/gauravk95/audio-visualizer-android

I am working in an activity coded using Kotlin

I did as advised in the readme, but am having an issue when my activity starts:

E/AudioEffect: set(): AudioFlinger could not create effect e46b26a0-dddd-11db-8afd-0002a5d5c51b / `�?�v, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.codex_d.dev.android, PID: 31116
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codex_d.dev.android/com.codex_d.dev.android.runningActivities.AudioPlayerActivity}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
        at android.media.audiofx.Visualizer.<init>(Visualizer.java:221)
        at com.gauravk.audiovisualizer.base.BaseVisualizer.setAudioSessionId(BaseVisualizer.java:196)
        at com.codex_d.dev.android.runningActivities.AudioPlayerActivity.onCreate(AudioPlayerActivity.kt:26)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
I/Process: Sending signal. PID: 31116 SIG: 9
Process 31116 terminated.

I have declared both RECORD_AUDIO and MODIFY_AUDIO_SETTINGS permissions in the manifest.

Here is my code for the activity:

class AudioPlayerActivity : BaseAdventureActivity(R.layout.activity_audio_player, true) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val dataLocation = intent.getSerializableExtra(EXTRA_LOCATION) as DataLocation
        val path         = "sample_audio_happy"

        var mediaPlayer = MediaPlayer.create(this, dataLocation.getUri(path, this, adventure))
        mediaPlayer.start()
        if (mediaPlayer.audioSessionId != -1) blast.setAudioSessionId(mediaPlayer.audioSessionId)
    }

    override fun onDestroy() {
        super.onDestroy()
        blast.release()
    }

When I comment the line with setAudioSessionId everything works fine (but of course without showing the visualisation).

Would anyone know where this error could come from?


Solution

  • Indeed, @Tenfour04 is right. Here are the two lines of code that were required:

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, Array(1){Manifest.permission.RECORD_AUDIO}, 0)
    }
    

    Just add before stting audioSessionId, and it should work.

    Thanks for your help!