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

时间:2019-10-11 14:24:08

标签: android kotlin visualizer

我目前在尝试使用以下库来显示媒体播放中的“爆炸”视图时遇到麻烦:https://github.com/gauravk95/audio-visualizer-android

我正在从事使用Kotlin编码的活动

我按照自述文件的建议进行操作,但是活动开始时遇到了问题:

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权限。

这是我的活动代码:

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注释行时,一切正常(但当然不显示可视化效果)。

有人知道这个错误可能从哪里来吗?

1 个答案:

答案 0 :(得分:0)

实际上,@ Tenfour04是正确的。这是所需的两行代码:

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

只需在添加audioSessionId之前添加,它就可以工作。

感谢您的帮助!