【问题标题】:Using Android Service to record audio - File is not created使用 Android 服务录制音频 - 未创建文件
【发布时间】:2013-06-06 21:52:31
【问题描述】:

我现在使用应用程序开始录制为 Android 服务而不是主线程。但是使用这个停止服务后:我找不到录音文件

    stopService(new Intent(this,RecordService.class));
 //   stopService(new Intent(this,TimeService.class));
    Log.d("TAG" , "RecordSerivce finished");
    Log.d("TAG" , "TimeService finished");
    pd = ProgressDialog.show(Main4.this, "EOrder", "Uploading , please wait");  


     new Thread(new Runnable() {  
         @Override  
         public void run() {  
             spandTimeMethod();
             handler.sendEmptyMessage(0);                 
         }  

     }).start();  

以下是我的服务代码:

private MediaRecorder recorder;
@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();
    stopSelf();
    super.onDestroy();
}

private void startRecording(){

    try {
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    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 + "sound");

    if(!instanceRecordDirectory.exists()){
        instanceRecordDirectory.mkdirs();
    }

    File instanceRecord = new File(instanceRecordDirectory.getAbsolutePath() + File.separator + reportDate + "_Recondsound.mp4");
    if(!instanceRecord.exists()){
        instanceRecord.createNewFile();
    }
    recorder.setOutputFile(instanceRecord.getAbsolutePath());



        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }catch (Exception e){
          e.printStackTrace();
    }
}

private void stopRecording() {
    if (recorder != null) {       
        recorder.stop();
        recorder.release();
        recorder = null;
    }
}

【问题讨论】:

  • 你有 WRITE_EXTERNAL_STORAGE 权限吗?
  • 是的,我已经添加了这个权限
  • 那么也许 logcat 打印出一些有趣的东西?
  • 06-11 14:17:51.885:W/MediaRecorder(6977):mediarecorder 因未处理的事件而消失
  • recorder.stop();记录器.reset();记录器.release();

标签: android audio service 3gp


【解决方案1】:

我在这里找到问题

Format formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");

你必须使用这个:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多