我创建了一个用MediaRecorder记录sond的类。它开始录制没有任何问题,但是当它运行Stop命令时,它什么都不做。没有返回错误和异常,但它不会停止录制,因此下次运行时,启动失败。
继承我的班级:
import java.util.Random;
import android.media.MediaRecorder;
import android.os.Environment;
public class AudioRecorder {
String Path = Environment.getExternalStorageDirectory().getAbsolutePath();
MediaRecorder mr = new MediaRecorder();
Random generator = new Random();
String File;
String arq;
public void startsRecording() {
//audio source = mic
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
//format 3gp
mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//encoder
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//Path
arq = String.valueOf(generator.nextInt(1000)) + ".3gp";
File = Path + "/" + arq;
mr.setOutputFile(File);
System.out.println("Started Recording: " + File);
try {
mr.prepare();
mr.start();
System.out.println("Start");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
public void stopsRecording() {
System.out.println("Stops Recording");
if (mr != null) {
System.out.println("Not null");
try {
System.out.println("Try");
mr.stop();
System.out.println("Stop");
} catch(IllegalStateException e){
System.out.println("Error");
System.out.println(e.toString());
}
catch(RuntimeException r) {
System.out.println("Error");
System.out.println(r.toString());
}
catch(Exception f){
System.out.println("Error");
System.out.println(f.toString());
}
finally {
mr.release();
System.out.println("Release");
mr = null;
}
} else {
System.out.println("IS null");
}
}
}
这是输出:
01-29 12:39:15.258: I/System.out(702): Started Recording: /mnt/sdcard/619.3gp
01-29 12:39:15.469: I/System.out(702): Start
01-29 12:39:27.458: I/System.out(702): Stops Recording
01-29 12:39:27.458: I/System.out(702): Not null
01-29 12:39:27.458: I/System.out(702): Try
它没有做任何进一步的事情。
在Android 2.3中,该代码有效。
可能是什么问题?
编辑:我在另一个班级宣布:
AudioRecorder AR = new AudioRecorder();
然后用以下方法启动这些方法:
AR.startsRecording();
和
AR.stopsRecording();