Android - 应用程序无法启动

时间:2013-11-08 03:30:43

标签: java android

我的应用不会启动以下错误:

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 个答案:

答案 0 :(得分:0)

  • 仔细检查并确保您已导入此课程: android.media.audiofx.Equalizer
  • 尝试清理您的项目(项目 - &GT;清洁...),重建你的apk

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

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

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

mAudioTrack = new AudioTrack(streamType, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes, mode);

答案 1 :(得分:0)

API级别10,新API

希望这对你有用

答案 2 :(得分:0)

问题可能在这里 -

  

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

您需要延长mMediaPlayer.getAudioSessionId()而不是mAudioTrack.getAudioSessionId()

请检查链接 - EqualizerSettings MediaPlayer

答案 3 :(得分:0)

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

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

的引用