错误:不幸的是应用停止了

时间:2014-08-23 10:42:42

标签: android

我是Android新手!我正在尝试编写原始音频数据。有没有人知道
的原因 错误。我也收到以下错误:            父视图不是TextView            致命异议:主要            java.lang.RuntimeException:无法启动活动            ......还有11个 提前致谢

 Thanks in advance.


import android.os.Bundle;
import android.app.Activity;

import android.os.Environment;
import android.widget.Button;
import android.view.View;
import android.media.MediaRecorder;
import java.io.IOException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.media.AudioFormat;
import android.media.AudioRecord;

public class MainActivity extends Activity {
private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
short[] audioData;

private AudioRecord recorder = null;
private int bufferSize = 0;
private Thread recordingThread = null;
private boolean isRecording = false;

int[] bufferData;
int bytesRecorded;

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

    setButtonHandlers();
    enableButtons(false);

    bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING) * 3;

    audioData = new short[bufferSize]; // short array that pcm data is put
                                        // into.

}

private void setButtonHandlers() {
    ((Button) findViewById(R.id.Start)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.Stop)).setOnClickListener(btnClick);
}

private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}

private void enableButtons(boolean isRecording) {
    enableButton(R.id.Start, !isRecording);
    enableButton(R.id.Stop, isRecording);
}

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
        file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_WAV);
}

public void convert() {

}

private String getTempFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
        file.mkdirs();
    }

    File tempFile = new File(filepath, AUDIO_RECORDER_TEMP_FILE);

    if (tempFile.exists())
        tempFile.delete();

    return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}

private void startRecording() {
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING, bufferSize);

    recorder.startRecording();

    isRecording = true;

    recordingThread = new Thread(new Runnable() {

        public void run() {
            writeAudioDataToFile();
        }
    }, "AudioRecorder Thread");

    recordingThread.start();
}

private void writeAudioDataToFile() {
    byte data[] = new byte[bufferSize];
    String filename = getTempFilename();
    FileOutputStream os = null;

    try {
        os = new FileOutputStream(filename);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int read = 0;

    if (null != os) {
        while (isRecording) {
            read = recorder.read(data, 0, bufferSize);

            if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                try {
                    os.write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private void stopRecording() {
    if (null != recorder) {
        isRecording = false;

        recorder.stop();
        recorder.release();

        recorder = null;
        recordingThread = null;
    }

    copyWaveFile(getTempFilename(), getFilename());
    // deleteTempFile();
}

private void deleteTempFile() {
    File file = new File(getTempFilename());
    file.delete();
}

private void copyWaveFile(String inFilename, String outFilename) {
    FileInputStream in = null;
    FileOutputStream out = null;
    long totalAudioLen = 0;
    long totalDataLen = totalAudioLen + 36;
    long longSampleRate = RECORDER_SAMPLERATE;
    int channels = 2;
    long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;

    byte[] data = new byte[bufferSize];

    try {
        in = new FileInputStream(inFilename);
        out = new FileOutputStream(outFilename);
        totalAudioLen = in.getChannel().size();
        totalDataLen = totalAudioLen + 36;

        // AppLog.logString("File size: " + totalDataLen);

        WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
                longSampleRate, channels, byteRate);

        while (in.read(data) != -1) {
            out.write(data);
        }

        in.close();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen,
        long totalDataLen, long longSampleRate, int channels, long byteRate)
        throws IOException {
    // another code

}

private View.OnClickListener btnClick = new View.OnClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.Start: {
            // AppLog.logString("Start Recording");
            enableButtons(true);
            startRecording();
            break;
        }
        case R.id.Stop: {
            // AppLog.logString("Stop Recording");
            enableButtons(false);
            stopRecording();
            break;

        }
        }
    }
};

}

1 个答案:

答案 0 :(得分:0)

在onCreate函数中:

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

setButtonHandlers();
enableButtons(false);

bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
        RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING) * 3;

audioData = new short[bufferSize]; // short array that pcm data is put
                                    // into.

}

正如@Raghunandan和@Hasnain指出的那样

的setContentView(R.layout.p1);已经使用//进行了评论。现在,活动的java文件无法找到布局xml文件,也无法知道屏幕上显示的内容,因此应用程序崩溃

在setContentView(R.layout.p1)之前删除//;它应该工作