Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4

I am getting below error when running app on API 16.

 java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                      at android.app.ActivityThread.access$600(ActivityThread.java:130)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:4745)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4
                      at android.media.audiofx.Visualizer.<init>(Visualizer.java:199)
                      at xyz.activity.AudioPreviewActivity.setupVisualizerFxAndUI(AudioPreviewActivity.java:83)
                      at xyz.activity.AudioPreviewActivity.initAudio(AudioPreviewActivity.java:60)
                      at xyz.activity.AudioPreviewActivity.onCreate(AudioPreviewActivity.java:40)
                      at android.app.Activity.performCreate(Activity.java:5008)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                      at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                      at android.os.Handler.dispatchMessage(Handler.java:99) 
                      at android.os.Looper.loop(Looper.java:137) 
                      at android.app.ActivityThread.main(ActivityThread.java:4745) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:511) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                      at dalvik.system.NativeStart.main(Native Method)   

Error line-

mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());

Code

private void setupVisualizerFxAndUI() {

    mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
    mMediaPlayer
            .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mVisualizer.setEnabled(false);
                }
            });

    mMediaPlayer.start();
    mMediaPlayer.setLooping(true);
    mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(
                    new Visualizer.OnDataCaptureListener() {
                        public void onWaveFormDataCapture(Visualizer visualizer,
                                                          byte[] bytes, int samplingRate) {
                            mVisualizerView.updateVisualizer(bytes);
                        }

                        public void onFftDataCapture(Visualizer visualizer,
                                                     byte[] bytes, int samplingRate) {
                        }
                    }, Visualizer.getMaxCaptureRate() / 2, true, false);
        }

I already have added below permissions in manifest-

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
like image 636
Android Developer Avatar asked Mar 12 '17 17:03

Android Developer


1 Answers

-4 Means you gave a bad parameter. Although getAudioSessionId can be called in any state, have you called prepare and start on the MediaPlayer you are using, to ensure it has a good state before attempting to visualise?

You may also need to add the android.permission.RECORD_AUDIO permission when using the Visualiser this way and android.permission.INTERNET if the audio source isn't local

like image 110
Nick Cardoso Avatar answered Nov 09 '22 08:11

Nick Cardoso