在录制声音过程中出现低概率点击录音结束而出现force close 现象(40次出现三次),打log也并未发现与之相关的明显异常现象,不知哪位大侠遇到过类似问题,能否给些修改思路

解决方案 »

  1.   

    线程上锁。package com.ppmeet.mic;import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;import com.ppmeet.encode.Encoder;import android.media.AudioFormat;
    import android.media.AudioRecord;
    import android.media.MediaRecorder;public class PcmRecorder implements Runnable { private Logger log = LoggerFactory.getLogger(PcmRecorder.class);
    private volatile boolean isRecording;
    private final Object mutex = new Object();
    private static final int frequency = 8000;
    private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; public PcmRecorder() {
    super();
    } public void run() {
    Encoder encoder = new Encoder();
    Thread encodeThread = new Thread(encoder);
    encoder.setRecording(true);
    encodeThread.start();
    synchronized (mutex) {
    while (!this.isRecording) {
    try {
    mutex.wait();
    } catch (InterruptedException e) {
    throw new IllegalStateException("Wait() interrupted!", e);
    }
    }
    }
    //录音相关了
    android.os.Process
    .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    int bufferRead = 0;
    int bufferSize = AudioRecord.getMinBufferSize(frequency,
    AudioFormat.CHANNEL_IN_MONO, audioEncoding);
    short[] tempBuffer = new short[bufferSize];
    AudioRecord recordInstance = new AudioRecord(
    MediaRecorder.AudioSource.MIC, frequency,
    AudioFormat.CHANNEL_IN_MONO, audioEncoding, bufferSize);
    recordInstance.startRecording();
    System.out.println(System.currentTimeMillis()+"kaishi");
    while (this.isRecording) {
    // bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
    bufferRead = recordInstance.read(tempBuffer, 0, 640);
    if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
    throw new IllegalStateException(
    "read() returned AudioRecord.ERROR_INVALID_OPERATION");
    } else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) {
    throw new IllegalStateException(
    "read() returned AudioRecord.ERROR_BAD_VALUE");
    } else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
    throw new IllegalStateException(
    "read() returned AudioRecord.ERROR_INVALID_OPERATION");
    } if (encoder.isIdle()) {
    encoder.putData(System.currentTimeMillis(), tempBuffer,
    bufferRead);
    } else {
    log.error("drop data!");
    }
    }
    recordInstance.stop();
    encoder.setRecording(false);
    } public void setRecording(boolean isRecording) {
    synchronized (mutex) {
    this.isRecording = isRecording;
    if (this.isRecording) {
    mutex.notify();
    }
    }
    } public boolean isRecording() {
    synchronized (mutex) {
    return isRecording;
    }
    }
    }
      

  2.   

    package com.ppmeet.mic;import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;import com.ppmeet.encode.Encoder;import android.media.AudioFormat;
    import android.media.AudioRecord;
    import android.media.MediaRecorder;public class PcmRecorder implements Runnable { private Logger log = LoggerFactory.getLogger(PcmRecorder.class);
    private volatile boolean isRecording;
    private final Object mutex = new Object();
    private static final int frequency = 8000;
    private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; public PcmRecorder() {
    super();
    } public void run() {
    Encoder encoder = new Encoder();
    Thread encodeThread = new Thread(encoder);
    encoder.setRecording(true);
    encodeThread.start();
    synchronized (mutex) {
    while (!this.isRecording) {
    try {
    mutex.wait();
    } catch (InterruptedException e) {
    throw new IllegalStateException("Wait() interrupted!", e);
    }
    }
    }
    //录音相关了
    android.os.Process
    .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    int bufferRead = 0;
    int bufferSize = AudioRecord.getMinBufferSize(frequency,
    AudioFormat.CHANNEL_IN_MONO, audioEncoding);
    short[] tempBuffer = new short[bufferSize];
    AudioRecord recordInstance = new AudioRecord(
    MediaRecorder.AudioSource.MIC, frequency,
    AudioFormat.CHANNEL_IN_MONO, audioEncoding, bufferSize);
    recordInstance.startRecording();
    System.out.println(System.currentTimeMillis()+"kaishi");
    while (this.isRecording) {
    // bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
    bufferRead = recordInstance.read(tempBuffer, 0, 640);
    if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
    throw new IllegalStateException(
    "read() returned AudioRecord.ERROR_INVALID_OPERATION");
    } else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) {
    throw new IllegalStateException(
    "read() returned AudioRecord.ERROR_BAD_VALUE");
    } else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
    throw new IllegalStateException(
    "read() returned AudioRecord.ERROR_INVALID_OPERATION");
    } if (encoder.isIdle()) {
    encoder.putData(System.currentTimeMillis(), tempBuffer,
    bufferRead);
    } else {
    log.error("drop data!");
    }
    }
    recordInstance.stop();
    encoder.setRecording(false);
    } public void setRecording(boolean isRecording) {
    synchronized (mutex) {
    this.isRecording = isRecording;
    if (this.isRecording) {
    mutex.notify();
    }
    }
    } public boolean isRecording() {
    synchronized (mutex) {
    return isRecording;
    }
    }
    }