我看到這個問題問了很多次。但我沒有找到解決方案。IllegalStateException異常在MediaRecorder.start()
我有一個廣播接收器中,我通話錄音。 recorder.start()拋出IllegalStateException。我在Manifest中授予了所有權限。我叫recorder.prepare()(它不拋出任何異常。)
我錯過了什麼?
這裏是我的簡化代碼:
case TelephonyManager.CALL_STATE_OFFHOOK:
recordState = true;
MediaRecorder mRecorder = new MediaRecorder();
mRecorder
.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
mRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(filePath);
mRecorder.setOnErrorListener(new OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what,
int extra) {
}
});
mRecorder.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what,
int extra) {
// TODO Auto-generated method stub
}
});
mRecorder.prepare();
mRecorder.start();
recorder = mRecorder;
case TelephonyManager.CALL_STATE_IDLE:
if (recorder != null && recordState) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
這裏是我的logcat:
07-21 14:23:08.807: E/MediaRecorder(4202): start failed: -38
07-21 14:23:08.807: W/System.err(4202): java.lang.IllegalStateException
07-21 14:23:08.807: W/System.err(4202): at android.media.MediaRecorder.start(Native Method)
07-21 14:23:08.807: W/System.err(4202): at com.labelle.calllogsmanagement.MyReceiver$MyPhoneStateListener.onCallStateChanged(MyReceiver.java:165)
07-21 14:23:08.807: W/System.err(4202): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
07-21 14:23:08.807: W/System.err(4202): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 14:23:08.807: W/System.err(4202): at android.os.Looper.loop(Looper.java:137)
07-21 14:23:08.807: W/System.err(4202): at android.app.ActivityThread.main(ActivityThread.java:4856)
07-21 14:23:08.807: W/System.err(4202): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 14:23:08.807: W/System.err(4202): at java.lang.reflect.Method.invoke(Method.java:511)
07-21 14:23:08.817: W/System.err(4202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-21 14:23:08.817: W/System.err(4202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-21 14:23:08.817: W/System.err(4202): at dalvik.system.NativeStart.main(Native Method)
編輯 一個空文件是在給定的路徑創建。
編輯:
現在,我創建了一個虛擬應用程序有兩個按鈕,一個用於啓動和對方停止錄製。這裏是代碼,並在調用start()時拋出相同的異常:
package com.example.recordtest;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnErrorListener;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
MediaRecorder mRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File file = new File(Environment.getExternalStorageDirectory(),
"1234.3gp");
mRecorder.setOutputFile(file.getAbsolutePath());
mRecorder.setOnErrorListener(new OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
}
});
mRecorder.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
// TODO Auto-generated method stub
}
});
try {
mRecorder.prepare();
mRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mRecorder.stop();
mRecorder.reset();
mRecorder.release();
}
});
}
}
你有任何其他的後臺服務,等它是使用mediarecorder? – Rakhita
不,我沒有任何其他服務使用媒體記錄器 –
什麼是API級別? – Rakhita