Android : “cannot initialize visualizer engine”

左心房为你撑大大i 提交于 2019-12-10 13:25:07

问题


I've started programming for Android 3 days ago, and today I wanted to do something more difficult using some classes from android Api. I find class Visualizer and at first attemp I had a problem. I read many post on different forums people who had the same problems : cannot initialize visualizer engine.

I added requierd uses-permission to a Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.program.fourier"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".mainFFT"
            android:label="@string/title_activity_main_fft" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Next I've tried to fix this problem but I can't. This is my full code:

package org.program.fourier;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

import android.media.audiofx.Visualizer;
import android.media.audiofx.Visualizer.OnDataCaptureListener;
import android.media.MediaPlayer;
import android.media.AudioManager;

public class mainFFT extends Activity {

    MediaPlayer mPlayer;
    Visualizer vis;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_fft);

        mPlayer = MediaPlayer.create(this, R.raw.sight);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        try
        {
            mPlayer.prepare();
        }
        catch(Exception ex){ Log.w("ExCePtIoN", ex+""); }

        try
        {
            vis = new Visualizer(mPlayer.getAudioSessionId());
        //vis.setDataCaptureListener(this, 20, true, true);
        }
        catch(Exception ex){ Log.w("ExCePtIoN", ex+""); }


        mPlayer.start();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_fft, menu);
        return true;
    }


}

And last problem is that it throw IllegalStateException when I want to execute mPlayer.prepare() method.

this is full LogCat messages:

07-06 18:33:17.141: E/Trace(833): error opening trace file: No such file or directory (2)
07-06 18:33:17.591: E/MediaPlayer(833): prepareAsync called in state 8
07-06 18:33:17.591: W/ExCePtIoN(833): java.lang.IllegalStateException
07-06 18:33:17.621: E/AudioEffect(833): set(): AudioFlinger could not create effect, status: -22
07-06 18:33:17.621: E/visualizers-JNI(833): Visualizer initCheck failed -4
07-06 18:33:17.621: E/Visualizer-JAVA(833): Error code -4 when initializing Visualizer.
07-06 18:33:17.621: W/ExCePtIoN(833): java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4
07-06 18:33:18.482: I/Choreographer(833): Skipped 337 frames!  The application may be doing too much work on its main thread.
07-06 18:33:18.551: D/gralloc_goldfish(833): Emulator without GPU emulation detected.

回答1:


Working example from Github

 public void link(MediaPlayer player)
  {
    if(player == null)
    {
      throw new NullPointerException("Cannot link to null MediaPlayer");
    }

    // Create the Visualizer object and attach it to our media player.
    mVisualizer = new Visualizer(player.getAudioSessionId());
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

    // Pass through Visualizer data to VisualizerView
    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);

    // Enabled Visualizer and disable when we're done with the stream
    mVisualizer.setEnabled(true);
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
    {
      @Override
      public void onCompletion(MediaPlayer mediaPlayer)
      {
        mVisualizer.setEnabled(false);
      }
    });
  }

and also check AudioFxDemo in Android developer site and .




回答2:


You need to call mPlayer.setDataSource() before you call mPlayer.prepare().
You will find all instructions for changing states at: http://developer.android.com/reference/android/media/MediaPlayer.html



来源:https://stackoverflow.com/questions/11368411/android-cannot-initialize-visualizer-engine

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