应用程序在启动时崩溃

时间:2014-05-20 07:33:41

标签: java android audio crash

我有一个基于音频的简单应用。语法明智,一切都还好。但是当我尝试在模拟器上运行它时,应用程序在启动时崩溃并且LogCat错误说:无法创建记录轨道,状态:-1

有什么问题?请帮忙。

package com.example.demo;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.app.Activity;

public class MainActivity extends Activity {
private static final int sampleRate = 8000;
private AudioRecord audio;
private int bufferSize;
private double lastLevel = 0;
private Thread thread;
private static final int SAMPLE_DELAY = 75;
public MediaPlayer mp;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        bufferSize = AudioRecord
                .getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
    } catch (Exception e) {
        android.util.Log.e("TrackingFlow", "Exception", e);
    }

}
protected void onResume() {

 super.onResume();
    audio = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);

    audio.startRecording();
    thread = new Thread(new Runnable() {
        public void run() {
            while(thread != null && !thread.isInterrupted()){
                //Let's make the thread sleep for a the approximate sampling time
                try{Thread.sleep(SAMPLE_DELAY);}catch(InterruptedException ie){ie.printStackTrace();}

                readAudioBuffer();//After this call we can get the last value assigned to the lastLevel variable

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                /*  if(lastLevel > 0 && lastLevel <= 50){
                            //mouthImage.setImageResource(R.drawable.mouth4);
                    }else
                     if(lastLevel > 50 && lastLevel <= 100){
                            //mouthImage.setImageResource(R.drawable.mouth3);
                    }else
                    if(lastLevel > 100 && lastLevel <= 170){
                            //mouthImage.setImageResource(R.drawable.mouth2);
                    }*/
                          if(lastLevel > 170){
                            //mouthImage.setImageResource(R.drawable.mouth1);
                            mp=MediaPlayer.create(getBaseContext(), R.raw.aud);
                            mp.start();




                        }

                    }

                });

            }

            }

    });

    thread.start();
}   
/**
 * Functionality that gets the sound level out of the sample
 */
private void readAudioBuffer() {

    try {
        short[] buffer = new short[bufferSize];

        int bufferReadResult = 1;

        if (audio != null) {

            // Sense the voice...
            bufferReadResult = audio.read(buffer, 0, bufferSize);
            double sumLevel = 0;
            for (int i = 0; i < bufferReadResult; i++) {
                sumLevel += buffer[i];
            }
            lastLevel = Math.abs((sumLevel / bufferReadResult));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    super.onPause();
    thread.interrupt();
    thread = null;
    try {
        if (audio != null) {
            audio.stop();
            audio.release();
            audio = null;

        }
    } catch (Exception e) {e.printStackTrace();}
}
}

LogCat:

05-20 12:46:52.453: E/AudioRecord(345): AudioFlinger could not create record track, status: -1
05-20 12:46:52.453: E/AudioRecord-JNI(345): Error creating AudioRecord instance: initialization check failed.
05-20 12:46:52.467: E/AudioRecord-Java(345): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
05-20 12:46:52.474: D/AndroidRuntime(345): Shutting down VM
05-20 12:46:52.474: W/dalvikvm(345): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-20 12:46:52.543: E/AndroidRuntime(345): FATAL EXCEPTION: main
05-20 12:46:52.543: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to resume activity {com.example.demo/com.example.demo.MainActivity}: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.os.Looper.loop(Looper.java:123)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-20 12:46:52.543: E/AndroidRuntime(345):  at java.lang.reflect.Method.invokeNative(Native Method)
05-20 12:46:52.543: E/AndroidRuntime(345):  at java.lang.reflect.Method.invoke(Method.java:507)
05-20 12:46:52.543: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-20 12:46:52.543: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-20 12:46:52.543: E/AndroidRuntime(345):  at dalvik.system.NativeStart.main(Native Method)
05-20 12:46:52.543: E/AndroidRuntime(345): Caused by: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.media.AudioRecord.startRecording(AudioRecord.java:496)
05-20 12:46:52.543: E/AndroidRuntime(345):  at com.example.demo.MainActivity.onResume(MainActivity.java:44)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.Activity.performResume(Activity.java:3832)
05-20 12:46:52.543: E/AndroidRuntime(345):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
05-20 12:46:52.543: E/AndroidRuntime(345):  ... 12 more
05-20 12:46:55.936: I/Process(345): Sending signal. PID: 345 SIG: 9

1 个答案:

答案 0 :(得分:1)

检查您是否已授予AUDIO_RECORD权限,请检查此已回答的问题AudioFlinger could not create record track, status: -1 , Need help to ifx