Audio recording app quitting with error "Unfortunately TActivity has stopped"

时间:2015-05-04 19:31:37

标签: android audio-streaming audio-recording microphone

I have the following code which should on click buffer the microphone input and set it as view text

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.view.View.OnClickListener;

public class TActivity extends Activity
{
    TextView text;
    Button startButton,stopButton;
    byte[] buffer;
    AudioRecord recorder;

    int sampleRate = 44100;
    int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;    
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       
    int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
    boolean status = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        text = (TextView) findViewById(R.id.txt);
        startButton = (Button) findViewById (R.id.start_button);
        stopButton = (Button) findViewById (R.id.stop_button);

        minBufSize += 2048;

        startButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                status = false;
                recorder.release();
            }

        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                status = true;

                byte[] buffer = new byte[minBufSize];
                recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*10);
                recorder.startRecording();

                while(status == true) {
                    minBufSize = recorder.read(buffer, 0, buffer.length);
                    text.setText(minBufSize);
                }
            }
        });
    }
}

XML layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World">
    </TextView>

    <Button
    android:id="@+id/start_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="130dp"
    android:text="Start" >
    </Button>

    <Button
    android:id="@+id/stop_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="64dp"
    android:text="Stop" >
    </Button>

</LinearLayout>

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

I get following error

http://pastebin.com/RH6ARTeS

My code is very simple and dont know what can be wrong.. what is the issue there? if this turns out to be complicate.. do you know a way to set view text from microphone input? thanx

1 个答案:

答案 0 :(得分:0)

to get rid of the API level requirement compilation warnings, you can change this line in AndroidManifest.xml

<uses-sdk android:minSdkVersion="3" />

I'm afraid that without the debug output/stack trace of the run time error we can't tell you much more..