我读过类似的帖子,但没有找到可以解决我问题的答案。
我正在使用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;
}
}
这是SoundRecorder:
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();
上会抛出异常。
我认为问题在于在这个循环中做所有事情的想法,但我还没有想出实现上述目标的更好的想法。 此外,我尝试了不同的OutputFormats和AudioEncoders但没有成功。 (参考https://stackoverflow.com/a/23065021/1826152)
另一个可能有用的注释是文件实际上是在sdcard目录中创建的。
我用于开发的手机是Nexus 5. 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有关,但很遗憾没有答案。
非常感谢任何进一步的帮助!
答案 0 :(得分:0)
好吧,我似乎解决了这个问题。问题似乎是让多个MediaRecorder实例同时工作。我所做的是,我现在不使用单独的类进行检测和录音,因此录音机现在可以自己进行检测。
首先,我启动了初始SoundMeter,它可以监听直到它在4.0级上输入。然后我停止初始的SoundMeter并制作一个新的SoundMeter(具有不同的输出目录),它开始记录和记录,直到水平低于4.0为apprixamtely 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;
}
http://developer.android.com/reference/android/media/MediaRecorder.html上的文档,在release()方法文档下,它讨论了某些设备上没有支持的多个实例。所以它可能是一个特定的设备相关问题。