媒体录制器在线程内启动失败-22

时间:2013-04-18 08:38:02

标签: android multithreading mediarecorder android-audiorecord

您好我正在尝试制作视频录制器安卓应用>由于某些原因,我需要获得它的音频,我已经尝试了一些库,但没有真正提取我的音频。所以现在我的方法是,我可以分别录制视频和音频而不是使用本机库吗?主线程记录视频,另一个线程记录音频。当我在线程内部调用媒体记录器类的start()方法时,我收到错误,我得到Start Failed -2​​2。以下是我的代码。

public class MainActivity extends Activity {

 private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;
    public MediaRecorder mrec ;
    ImageView recordingBt;
    public MediaRecorder mrec2 = new MediaRecorder();
    File video;
    private Camera mCamera;
    File audiofile = null;

    File audiofile2 = null;
    boolean recording=false;
    TextView tv;
    View myview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recordingBt= (ImageView) findViewById(R.id.camera);
    surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
    tv=(TextView) findViewById(R.id.textView1);

     myview = findViewById(R.id.MyBigView);
}

@Override
protected void onStart(){
    super.onStart();
    Log.i("Recording Activity", "we are inside Start function of Recording Activity");



    Log.i("Recording Activity", "Opening camera");


     mCamera = Camera.open();

     Log.i("Recording Activity", "Camera Opened");
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        recordingBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(recording==false){
                    try {
                        Log.i("Recording Activity", "Recording Start");
                        startRecording();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else{
                    stopRecording();

                }

            }
        });}
public void startRecording() throws IOException 
    {

        recording=true;
        String path= Environment.getExternalStorageDirectory().toString();
        File sampleDir = new File(path+"/DCIM/Squlium Booth");
        sampleDir.mkdir();


         Log.i("Setting Path", sampleDir.toString());

            try {
              audiofile = File.createTempFile("SqB", ".3gp", sampleDir);
            } catch (IOException e) {

              return;}
            Log.i("Recording Activity", "Seting Media Recorder attributes");   
        mrec = new MediaRecorder();  // Works well

        mCamera.unlock();
        mrec.setCamera(mCamera);
    mrec.setPreviewDisplay(surfaceHolder.getSurface());
     mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
         mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
         mrec.setMaxDuration(60000);
        mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
        mrec.setOutputFile(audiofile.getAbsolutePath()); 
        mrec.prepare();
        mrec.start();
        startProgress(myview);


    }

 protected void stopRecording() {
     recording=false;
        mrec.stop();
        mrec.release();
        mCamera.lock();
        mCamera.release();
        mrec2.stop();
        mrec.release();

    }
 public void startProgress(View view) {
        // Do something long
        Runnable runnable = new Runnable() {

          @Override
          public void run() {

                String path= Environment.getExternalStorageDirectory().toString();
                File sampleDir = new File(path+"/DCIM/Squlium Booth");
                sampleDir.mkdir();
                try {
                      audiofile2 = File.createTempFile("myAudio", ".mp3", sampleDir);
                    } catch (IOException e) {

                      return;}

                int minBufferSize = AudioRecord.getMinBufferSize(8000, 
                         AudioFormat .CHANNEL_IN_MONO, 
                         AudioFormat.ENCODING_PCM_16BIT);

                mrec2.setAudioSource(MediaRecorder.AudioSource.MIC);

                mrec2.setAudioSamplingRate(16000);
                mrec2.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                mrec2.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mrec2.setOutputFile(audiofile2.getAbsolutePath()); 
                try {
                    mrec2.prepare();
                } catch (IllegalStateException e1) {
                    Log.i("Illegal", "Exception");
                    e1.printStackTrace();
                } catch (IOException e1) {
                    Log.i("Iinput output", "Exception");
                }
                Log.i("Inside Run Method", "Starting Thread soon there will be issue");
                mrec2.start();
                Log.i("Inside Run Method", "After start method inside thread");
              Log.i("Inside Thread", "Alive");

            }

        };
        new Thread(runnable).start();

 }

0 个答案:

没有答案