无法使用 Kotlin 在 Android 上初始化 Visualizer

Can't initialise Visualizer on Android using Kotlin

我目前在尝试显示媒体播放的 "blast" 视图时遇到问题,使用此库:https://github.com/gauravk95/audio-visualizer-android

我正在 activity 使用 Kotlin

编码

我按照自述文件中的建议做了,但是当我的 activity 启动时遇到问题:

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.

我已经在清单中声明了 RECORD_AUDIO 和 MODIFY_AUDIO_SETTINGS 权限。

这是我的 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()
    }

当我用 setAudioSessionId 评论该行时,一切正常(但当然没有显示可视化)。

谁知道这个错误可能来自哪里?

确实,@Tenfour04 是对的。以下是所需的两行代码:

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

只需在设置 audioSessionId 之前添加,就可以了。

感谢您的帮助!