我将启动录制服务,但在操作时失败,出现以下异常
06-28 09:14:47.646: E/MediaRecorder(3089): start failed: -38
06-28 09:14:47.646: W/System.err(3089): java.lang.IllegalStateException
06-28 09:14:47.646: W/System.err(3089): at android.media.MediaRecorder.start(Native Method)
06-28 09:14:47.646: W/System.err(3089): at tvbpv.test.EOrderSystem1.RecordService.startRecording(RecordService.java:87)
06-28 09:14:47.646: W/System.err(3089): at tvbpv.test.EOrderSystem1.RecordService.onStart(RecordService.java:49)
06-28 09:14:47.646: W/System.err(3089): at android.app.Service.onStartCommand(Service.java:438)
06-28 09:14:47.646: W/System.err(3089): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2371)
06-28 09:14:47.646: W/System.err(3089): at android.app.ActivityThread.access$1900(ActivityThread.java:127)
06-28 09:14:47.646: W/System.err(3089): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1222)
06-28 09:14:47.646: W/System.err(3089): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 09:14:47.646: W/System.err(3089): at android.os.Looper.loop(Looper.java:137)
06-28 09:14:47.646: W/System.err(3089): at android.app.ActivityThread.main(ActivityThread.java:4519)
06-28 09:14:47.646: W/System.err(3089): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 09:14:47.646: W/System.err(3089): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 09:14:47.646: W/System.err(3089): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-28 09:14:47.646: W/System.err(3089): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-28 09:14:47.646: W/System.err(3089): at dalvik.system.NativeStart.main(Native Method)
start failed: -38 06-28 09:14:47.646: W/System.err(3089): java.lang.IllegalStateException 是什么意思?
RecordService.java
private MediaRecorder recorder;
private File instanceRecord;
private boolean recording= false;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
startRecording();
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
stopRecording();
super.onDestroy();
}
private void prepareRecording()
{
try
{
recorder.prepare();
}
catch(IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void startRecording(){
try {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
Date today = Calendar.getInstance().getTime();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String reportDate = formatter.format(today);
File instanceRecordDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "TVB_PV_recordings");
if(!instanceRecordDirectory.exists()){
instanceRecordDirectory.mkdirs();
}
instanceRecord = new File(instanceRecordDirectory.getAbsolutePath() + File.separator + reportDate + "_sales_recording.mp4");
if(!instanceRecord.exists()){
instanceRecord.createNewFile();
}
recorder.setOutputFile(instanceRecord.getAbsolutePath());
prepareRecording();
recorder.start();
recording = true;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
public void stopRecording() {
if (recorder != null) {
try {
recorder.stop();
} catch(RuntimeException e) {
instanceRecord.delete(); //you must delete the outputfile when the recorder stop failed.
} finally {
recorder.release();
recorder = null;
}
}
}
启动服务
startService(new Intent(OrderSystemSubmit.this, RecordService.class));
其他活动:
stopService(new Intent(OrderSystemSubmit.this, RecordService.class));
允许
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
我还应该添加什么?