0

我的应用程序无法启动并出现以下错误:

11-08 03:20:58.265: E/AndroidRuntime(306): FATAL EXCEPTION: main
11-08 03:20:58.265: E/AndroidRuntime(306): java.lang.NoClassDefFoundError: android.media.audiofx.Equalizer
11-08 03:20:58.265: E/AndroidRuntime(306):  at com.example.audiotest.MainActivity.onCreate(MainActivity.java:86)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.os.Looper.loop(Looper.java:123)
11-08 03:20:58.265: E/AndroidRuntime(306):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-08 03:20:58.265: E/AndroidRuntime(306):  at java.lang.reflect.Method.invokeNative(Native Method)
11-08 03:20:58.265: E/AndroidRuntime(306):  at java.lang.reflect.Method.invoke(Method.java:521)
11-08 03:20:58.265: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-08 03:20:58.265: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-08 03:20:58.265: E/AndroidRuntime(306):  at dalvik.system.NativeStart.main(Native Method)
11-08 03:20:58.285: E/AudioThread(306): Thread Loop Exception: java.lang.IllegalArgumentException: Invalid audio buffer size.

我的 OnCreate() 代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Audio, mic to speaker
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    initControls();
    mAudioThread = new AudioThread();
    mAudioThread.start();

    //end

    //Equalizer
    enabled = (CheckBox)findViewById(R.id.enabled);
    enabled.setOnCheckedChangeListener (this);

    flat = (Button)findViewById(R.id.flat);
    flat.setOnClickListener(this);

    bass_boost = (SeekBar)findViewById(R.id.bass_boost);
    bass_boost.setOnSeekBarChangeListener(this);
    bass_boost_label = (TextView) findViewById (R.id.bass_boost_label);

    sliders[0] = (SeekBar)findViewById(R.id.slider_1);
    slider_labels[0] = (TextView)findViewById(R.id.slider_label_1);
    //more sliders till [4]

    mEqualizer = new Equalizer(0, mAudioTrack.getAudioSessionId());
    mEqualizer.setEnabled(true);

    eq = new Equalizer (0, 0);
    if (eq != null)
      {
      eq.setEnabled (true);
      int num_bands = eq.getNumberOfBands();
      num_sliders = num_bands;
      short r[] = eq.getBandLevelRange();
      min_level = r[0];
      max_level = r[1];
      for (int i = 0; i < num_sliders && i < MAX_SLIDERS; i++)
        {
        int[] freq_range = eq.getBandFreqRange((short)i);
        sliders[i].setOnSeekBarChangeListener(this);
        slider_labels[i].setText (formatBandLabel (freq_range));
        }
      }
    for (int i = num_sliders ; i < MAX_SLIDERS; i++)
      {
      sliders[i].setVisibility(View.GONE);
      slider_labels[i].setVisibility(View.GONE);
      }

    bb = new BassBoost (0, 0);
    if (bb != null)
      {
      }
    else
      {
      bass_boost.setVisibility(View.GONE);
      bass_boost_label.setVisibility(View.GONE);
      }

updateUI();
         }

对于我的 AudioThread:

    public class AudioThread extends Thread {

    // Handler message constants
    public static final int PLAY = 0;
    public static final int STOP = 1;

    // Class variables
    private boolean mRunning = false;
    private boolean mPlayAudio = false;



    private static class AudioThreadHandler extends Handler {
        private final WeakReference<AudioThread> mThread;

        public AudioThreadHandler(AudioThread thread) {
            mThread = new WeakReference<AudioThread>(thread);
        }

        @Override
        public  void handleMessage(Message msg) {
            AudioThread thread = mThread.get();
            if (thread != null) {
                switch(msg.what) {
                case PLAY:
                    thread.mPlayAudio = true;
                    break;
                case STOP:
                    thread.mPlayAudio = false;
                    break;
                }
            }
        }
    };
    static final int bufferSize = 320000;
    final short[] buffer = new short[bufferSize];
    short[] readBuffer = new short[bufferSize];
    private final AudioThreadHandler mAudioThreadHandler = new AudioThreadHandler(this);


    @Override
    public void run() {
        try {
            int buffersize = AudioRecord.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
            AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
            AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
            atrack.setPlaybackRate(11025);
            byte[] buffer = new byte[buffersize];
            arec.startRecording();
            atrack.play();
            mRunning = true;
            while(mRunning) { 
                if (mPlayAudio) {
                arec.read(buffer, 0, buffersize);
                atrack.write(buffer, 0, buffer.length);
                 }else {
                    }
                }

        } catch (Exception e) {
            Log.e("AudioThread", "Thread Loop Exception: " + e);
        }
    }

    public void close() {
        mRunning = false;
    }

    public Handler getHandler() {
        return mAudioThreadHandler;
    }
}

抱歉,一切似乎都很混乱。但是这个错误已经困扰了我很长时间。希望你们真的能帮上忙。如果您愿意,请查看我的项目,并为我做错的事情提供说明。

4

4 回答 4

0
  • 仔细检查并确保您已导入此类:android.media.audiofx.Equalizer
  • 尝试清理您的项目( Project -> Clean... ),重建您的 apk

编辑:您的错误行在这里:

mEqualizer = new Equalizer(0, mAudioTrack.getAudioSessionId());

我想你忘了创建mAudioTrack的实例,所以你在那里发现了null..

mAudioTrack = new AudioTrack(streamType, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes, mode);
于 2013-11-08T03:56:03.447 回答
0

API 级别 10, 新 API

希望这对你有用

于 2013-11-08T04:01:08.970 回答
0

问题可能在这里-

mEqualizer = new Equalizer(0, mAudioTrack.getAudioSessionId());

您需要扩展mMediaPlayer.getAudioSessionId()而不是mAudioTrack.getAudioSessionId()

请检查链接 - EqualizerSettings MediaPlayer

于 2013-11-08T04:35:59.977 回答
0

minSdkVersion="8"在清单中有,但Equalizer已在 API 级别 9 中添加。

这不会直接导致崩溃,但在 API 8 级设备中运行代码会。将最低 SDK 版本更改为 9,并在 API 级别 9 或更高级别的设备上进行测试。或者只是删除对Equalizer.

于 2013-11-08T08:20:24.187 回答