private String recholder = "not";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/recordingnijez.mp3";
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
myRecorder.setOutputFile(outputFile);
addListenerButton();
}
private void addListenerButton() {
recbtn = (Button)findViewById(R.id.recbut);
recbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start(v);
}
});
}
public void start(View view){
if ("recording".equals(recholder)){
try {
myRecorder.stop();
myRecorder.release();
//myRecorder = null;
recbtn.setText(getString(R.string.recBtn));
recholder="not";
Toast.makeText(getApplicationContext(), "Stop recording...",
Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
} catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}
else{
try {
File file = new File (Environment.getExternalStorageDirectory() + "/recordingnijez.mp3");
if (file.exists()){
file.delete();
myRecorder.prepare();
myRecorder.start();
recholder="recording";
recbtn.setText(getString(R.string.stopBtn));
}
else{
myRecorder.prepare();
myRecorder.start();
recholder="recording";
recbtn.setText(getString(R.string.stopBtn));
}
} catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
} catch (IOException e) {
// prepare() fails
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Start recording...",
Toast.LENGTH_SHORT).show();
}
}
当我尝试录制两次时有问题。我不知道如何解决它我尝试了一切
继承logcat
02-12 09:33:50.471: A/libc(1894): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 1894 (.example.mixpad)
02-12 09:33:51.191: E/Trace(1961): error opening trace file: No such file or directory (2)
02-12 09:33:51.331: D/dalvikvm(1961): GC_FOR_ALLOC freed 43K, 3% free 8017K/8195K, paused 3ms, total 3ms
02-12 09:33:51.359: I/dalvikvm-heap(1961): Grow heap (frag case) to 10.785MB for 3067212-byte allocation
02-12 09:33:51.379: D/dalvikvm(1961): GC_CONCURRENT freed 1K, 2% free 11011K/11207K, paused 13ms+3ms, total 21ms
02-12 09:33:51.499: D/libEGL(1961): loaded /system/lib/egl/libEGL_emulation.so
02-12 09:33:51.507: D/(1961): HostConnection::get() New Host Connection established 0xb8b8a110, tid 1961
02-12 09:33:51.531: D/libEGL(1961): loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-12 09:33:51.531: D/libEGL(1961): loaded /system/lib/egl/libGLESv2_emulation.so
02-12 09:33:51.611: W/EGL_emulation(1961): eglSurfaceAttrib not implemented
02-12 09:33:51.627: D/OpenGLRenderer(1961): Enabling debug mode 0
02-12 09:33:51.691: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8b908e0): name, size, mSize = 1, 1048576, 1048576
02-12 09:33:51.735: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8ba7d10): name, size, mSize = 2, 5184, 1053760
02-12 09:33:51.775: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8bccaf8): name, size, mSize = 4, 20736, 1074496
02-12 09:33:51.839: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8b8f730): name, size, mSize = 5, 3067200, 4141696
02-12 09:33:51.867: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8be9798): name, size, mSize = 6, 7488, 4149184
02-12 09:33:51.871: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8b88ff0): name, size, mSize = 7, 40984, 4190168
02-12 09:33:51.875: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8b8f7c0): name, size, mSize = 8, 37412, 4227580
02-12 09:33:51.883: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8ba8b60): name, size, mSize = 9, 33276, 4260856
02-12 09:33:51.887: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8b9c0d8): name, size, mSize = 10, 32712, 4293568
02-12 09:33:51.891: D/OpenGLRenderer(1961): TextureCache::get: create texture(0xb8b8b840): name, size, mSize = 11, 2304, 4295872
答案 0 :(得分:3)
我也遇到了这个问题,请使用myRecorder=null
,我认为因为myRecorder.release()
有错误,
你可以这样做:
myRecorder.stop();
myRecorder.release();
myRecorder = null;
和其他地方追加判决
if(myRecorder != null){ ... }
答案 1 :(得分:1)
尝试添加此内容:
mediaRecorder.stop();
mediaRecorder.reset(); //instead of release() and =null
然后在
public void onStop() {
mediaRecorder.release();
mediaRecorder = null;
答案 2 :(得分:1)
我有这个......
if (recorder != null) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
......并且工作正常!
答案 3 :(得分:0)
尝试在onCreate中首先调用super:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);