Android Visualizer error only in Android 6.0

孤街醉人 提交于 2019-12-13 12:38:34

问题


i've found one issue related with Visualizer that takes place only in Android 6.0 devices. Don't really know why it happens, so any kind of help would be appreciated. Here is the error stack:

AudioEffect: set(): AudioFlinger could not create effect, status: -1
visualizers-JNI: Visualizer initCheck failed -3
Visualizer-JAVA: Error code -3 when initializing Visualizer.
java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
System.err: at android.media.audiofx.Visualizer.<init>(Visualizer.java:218)
System.err:     at com.xxx.xxxx.customviews.visualizer.VisualizerView.link(VisualizerView.java:101)

and here is the code that causes it:

public void link(MediaPlayer player) {

    if (player == null) {

        throw new NullPointerException("Cannot link to null MediaPlayer");
    }

    Equalizer mEqualizer = new Equalizer(0, player.getAudioSessionId());
    mEqualizer.setEnabled(true); // need to enable equalizer


    try {

        int aud = player.getAudioSessionId();
        **mVisualizer = new Visualizer(aud);**
        mVisualizer.setEnabled(false);
        mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
        Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener() {

            @Override
            public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
                updateVisualizer(bytes);
            }

            @Override
            public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
                updateVisualizerFFT(bytes);
            }
        };

        mVisualizer.setDataCaptureListener(captureListener, Visualizer.getMaxCaptureRate() / 2, true, true);
        mVisualizer.setEnabled(true);
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mVisualizer.setEnabled(false);
            }
        });
    }catch(Exception e) {       

        e.printStackTrace();        
    } 
}

Any help would be appreciated. Thanks in advance =)


回答1:


Maybe the underlying component(s) needs some permissions that do not automatically get granted in 6.0. Goto AppInfo for this app; navigate to permissions; ensure all permissions that this app has requested has been granted.




回答2:


Make sure you have this permission in your Manifest as the Visualizer requires it.

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

As this permission is 'dangerous' you also need to ask the user to grant the permission at runtime as well.




回答3:


Can you try adding to your permission list in your manifest file:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>



回答4:


This was driving me crazy for weeks...I was focusing on my Visualizer code, where the 6.0 permission system was the problem. I have tested this on 4.2.2, 5.0, and 6.0.1 on real devices. I still don't know why the permission dialog never comes up when I am testing the app on real devices. Also, manually turning on the Microphone permission solved the problem, in App Info settings.

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
                 {
                     requestPermissions(new String[]{android.Manifest.permission.RECORD_AUDIO}, 1);
        }


来源:https://stackoverflow.com/questions/33494631/android-visualizer-error-only-in-android-6-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!