我读过类似的文章,但没有找到可以解决我的问题的答案。
我正在用2个不同的MediaRecorder编写应用程序。一个用于噪声检测,另一个用于记录。我想做的是-当第一个MediaRecorder检测到高于4.0的噪声水平时(我正在使用Google的SoundMeter类进行检测),它将启动另一个MediaRecorder并开始录制。如果声级在4.0以下保持10秒钟,请停止录音并继续收听。所有这些都是在AsynTask中进行的,而无休止的while(true)循环只有在单击相应按钮时才会中断。
检测工作正常,但是在记录MediaRecorder上调用start()时抛出IllegalStateException。
这是AsyncTask:
private class NoiseDetection extends AsyncTask {
double currentSoundInputLevel;
@Override
protected Object doInBackground(Object[] params) {
int i = 0;
soundMeter = new SoundMeter();
try {
soundMeter.start();
} catch (IOException e) {
Log.e("error", e.getMessage());
}
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("error", e.getMessage());
}
if(isCancelled()){
soundMeter.stop();
if(currentlyRecording) {
soundRecorder.stop();
}
break;
}
currentSoundInputLevel = soundMeter.getAmplitudeEMA();
if(!currentlyRecording && currentSoundInputLevel > 4.0){
soundRecorder = new SoundRecorder();
try {
soundRecorder.start(getFileNameString());
currentlyRecording = true;
} catch (IOException e) {
Log.e("error", e.getMessage());
}
} else if(currentlyRecording && currentSoundInputLevel < 4.0) {
i++;
if(i > 10) {
soundRecorder.stop();
}
}
}
return null;
}
}
这是录音机:
public class SoundRecorder {
private MediaRecorder mRecorder = null;
public void start(String fileName) throws IOException {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mRecorder.setOutputFile(Environment.getExternalStorageDirectory().getPath() + "/" + fileName);
mRecorder.prepare();
mRecorder.start();
}
}
public void stop() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
}
在
mRecorder.start();
上引发了异常。我认为问题在于在while循环中进行所有操作的想法,但是我还没有提出实现上述目标的更好的想法。
另外,我尝试了不同的OutputFormats和AudioEncoders,但没有成功。 (对https://stackoverflow.com/a/23065021/1826152的引用)
另一个可能有用的注释是,该文件实际上是在sdcard目录中创建的。
我用于开发的手机是Nexus5。Android清单中的权限如下:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
更新:
现在,我试图通过创建RecordingHandler从while循环中删除SoundRecorder操作。 doInBackground()的新代码如下:
protected Object doInBackground(Object[] params) {
int i = 0;
soundMeter = new SoundMeter();
RecordingHandler recordingHandler = null;
try {
soundMeter.start();
} catch (IOException e) {
Log.e("error", e.getMessage());
}
while(true){
if(isCancelled()){
soundMeter.stop();
if(currentlyRecording && recordingHandler != null){
recordingHandler.kill();
}
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("error", e.getMessage());
}
if(!currentlyRecording && soundMeter.getAmplitudeEMA() > 4.0){
recordingHandler = new RecordingHandler(deviceId);
currentlyRecording = true;
recordingHandler.run();
}
}
return null;
}
而RecordingHandler本身如下:
public class RecordingHandler implements Runnable {
SoundRecorder soundRecorder;
SoundMeter soundMeter;
String deviceID;
boolean isKilled = false;
public RecordingHandler(String deviceID){
this.soundRecorder = new SoundRecorder();
this.soundMeter = new SoundMeter();
this.deviceID = deviceID;
}
@Override
public void run() {
int i = 0;
try {
soundMeter.start();
soundRecorder.start(getFileNameString());
} catch (IOException e) {
Log.e("error", e.getMessage());
}
while(true){
if(isKilled){
break;
}
if(soundMeter.getAmplitudeEMA() < 4.0){
i++;
if(i > 10){
break;
}
} else {
i = 0;
}
}
soundMeter.stop();
soundRecorder.stop();
EavesDrop.currentlyRecording = false;
}
public void kill(){
this.isKilled = true;
}
private String getFileNameString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return deviceID + "_" + sdf.format(new Date());
}
}
现在,从Recordinghandler抛出IllegalStateException-在
soundMeter.start();
行上。考虑到此soundMeter对象基本上不再在循环中处理,因此应消除while循环是罪魁祸首的原因。有什么我想念的吗? 问题可能出在多个MediaRecorder同时工作吗? 如您所见,引发异常的是SoundMeter,而不是SoundRecorder。实际上-无论我在RecordingHandler中首先放置哪个
start()
调用,都将引发相同的IllegalStateException。该问题可能与Android: Two instances of Media recorder at same time有关,不幸的是没有答案。
任何进一步的帮助将不胜感激!
最佳答案
好吧,看来我解决了这个问题。问题似乎出在同时运行多个MediaRecorder实例。我所做的是,我现在不使用单独的类进行检测和记录,因此记录器现在可以自己进行检测。
首先,我启动了初始的SoundMeter,它会监听直到获得4.0级以上的输入。然后,我停止初始的SoundMeter并制作一个新的SoundMeter(具有不同的输出目录),该声音记录开始记录并记录,直到电平一直低于4.0约10秒钟。然后第二个SoundMeter停止,后台任务可以再次启动初始SoundMeter。
这是解决我的问题的代码,
AsyncTask:
protected Object doInBackground(Object[] params) {
int i = 0;
soundMeter = new SoundMeter();
RecordingHandler recordingHandler = null;
try {
soundMeter.start("/dev/null");
} catch (IOException e) {
Log.e("error", e.getMessage());
}
while(true){
if(isCancelled()){
soundMeter.stop();
if(currentlyRecording && recordingHandler != null){
recordingHandler.kill();
}
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("error", e.getMessage());
}
if(!currentlyRecording && soundMeter.getAmplitudeEMA() > 4.0){
soundMeter.stop();
recordingHandler = new RecordingHandler(deviceId);
currentlyRecording = true;
recordingHandler.run();
} else if(!currentlyRecording && !soundMeter.isRunning()){
try {
soundMeter.start("/dev/null");
} catch (IOException e) {
Log.e("error", e.getMessage());
}
}
}
return null;
}
}
还有RecordingHandler.run():
@Override
public void run() {
int i = 0;
try {
soundMeter.start(getFileNameString());
} catch (IOException e) {
Log.e("error", e.getMessage());
}
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e("error", e.getMessage());
}
if(isKilled){
break;
}
if(soundMeter.getAmplitudeEMA() < 4.0){
i++;
if(i > 10){
break;
}
} else {
i = 0;
}
}
soundMeter.stop();
EavesDrop.currentlyRecording = false;
}
在release()方法文档下的http://developer.android.com/reference/android/media/MediaRecorder.html上的文档讨论了某些设备上不支持多个实例。因此,这可能是与特定设备有关的问题。