android中声音录制问题

资深全栈码农 2011-09-30 12:03:58
在录制声音过程中出现低概率点击录音结束而出现force close 现象(40次出现三次),打log也并未发现与之相关的明显异常现象,不知哪位大侠遇到过类似问题,能否给些修改思路
...全文
414 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小裴同学 2011-09-30
  • 打赏
  • 举报
回复
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;
}
}
}
小裴同学 2011-09-30
  • 打赏
  • 举报
回复
线程上锁。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;
}
}
}

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧