android中声音录制问题 在录制声音过程中出现低概率点击录音结束而出现force close 现象(40次出现三次),打log也并未发现与之相关的明显异常现象,不知哪位大侠遇到过类似问题,能否给些修改思路 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 线程上锁。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; } }} 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; } }} android activity动画切换效果 真机一闪而过,模拟器正常 如何修改CPU核心数? 关于短信功能问题 listview 获取焦点问题 请教一个关于SN号的问题 如何使用系统默认的图片浏览器,然后得到图片路径? 读取联系人信息问题 显示速度提升 listview中item项多需要滑动显示时最后一项看不见? Android NDK 中pthread后台线程访问主线程问题 关于android的IP地址的问题 android输入法问题
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;
}
}
}
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;
}
}
}