如何停止视频录制?

时间:2013-12-09 11:49:24

标签: android android-video-player

我创建了一个视频录制应用。在按钮上单击它将开始录制并结束。一旦它结束,我可以毫无问题地再次开始录制。

我的问题是在完成上一条记录之前单击按钮:应用程序获得强制关闭对话框。

如果在完成录制之前单击按钮,我想要停止录制。

这是我的代码

public void StrBtnClick1(View view) throws IOException
{  

    try {
            startRecording();
             new Thread(new Runnable() {
                 private int progressStatus = 0;
                public void run() {
                    while (progressStatus < 100 ) {  
                        progressStatus += 1;
                        // Update the progress bar and display the current value in the text view
                        handler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressStatus);

                            }  
                        });
                        try {     

                            Thread.sleep(150);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    release();
                    flage = 2;
                    //releaseMediaRecorder(); 

                }  
            }).start();
        } catch (Exception e) {

            Toast.makeText(getApplicationContext(), "Camera not available111!", Toast.LENGTH_LONG).show();
            release1();
            flage=2;

        }       
}

public void ChangeBtnClick(View view) throws IOException
{  
    //Changecamera();   
}

protected void startRecording() throws IOException 
{
    mrec = new MediaRecorder();  // Works well

    if (flage==1){ 
    mCamera.unlock();
    mrec.setCamera(mCamera);
    }  

    mrec.setPreviewDisplay(surfaceHolder.getSurface());
    mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mrec.setAudioSource(MediaRecorder.AudioSource.MIC); 

    mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    mrec.setPreviewDisplay(surfaceHolder.getSurface());
    mrec.setMaxDuration(15000);

    mrec.setOutputFile("/sdcard/Myvideo.MP4"); 

    mrec.prepare();  
    mrec.start();      
}

protected void release1() {   
    /*mrec.reset();
    mrec.release();
    mCamera.release();*/
    if (mCamera != null){

       mCamera.stopPreview();
       mCamera.release();
       }
}
protected void release() {   

    mrec.release();
    mCamera.release();

}

private void releaseMediaRecorder(){  
    if (mrec != null) {
        mrec.reset();   // clear recorder configuration
       // mrec.release(); // release the recorder object
        mrec = null;
        mCamera.lock();           // lock camera for later use
    }
}

private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (mCamera != null){
        Parameters params = mCamera.getParameters();
        mCamera.setParameters(params);
    }
    else {
        Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
        finish();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview();
    mCamera.release();
}public void onBackPressed() {
    // TODO Auto-generated method stub
    //super.onBackPressed();

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle("Exit");
    builder.setInverseBackgroundForced(true);
   // builder.setMessage("Are you sure do you want to exit?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            // TODO Auto-generated method stub
            dialog.dismiss();
            releaseMediaRecorder();  
            finish();

        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            // TODO Auto-generated method stub
            dialog.dismiss();

        }
    });

  // builder.setIcon(R.drawable.icon);
   builder.show();

}

错误日志

E/System(22590): android.media.MediaRecorder.finalize() timed out after 10000 ms; limit is 10000 ms
E/System(22590): java.util.concurrent.TimeoutException
E/System(22590):    at android.media.MediaRecorder.native_finalize(Native Method)
E/System(22590):    at android.media.MediaRecorder.finalize(MediaRecorder.java:1056)
E/System(22590):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:186)
E/System(22590):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
E/System(22590):    at java.lang.Thread.run(Thread.java:856)

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".2"
        android:background="#2C3539"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight=".2"
            android:src="@drawable/up2" />

        <ProgressBar
            android:id="@+id/progressBar2"
            style="@style/Widget.ProgressBar.Vertical"
            android:layout_width="20dp"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight=".6" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight=".2"
            android:src="@drawable/up1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".6"
        android:orientation="vertical" >

        <SurfaceView
            android:id="@+id/surface_camera1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".2"
        android:background="@drawable/bottom"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".1" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight=".16"
            android:src="@drawable/bottam3" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".16" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight=".16"
            android:onClick="StrBtnClick1"
            android:src="@drawable/bottom2" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".16" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight=".16"
            android:src="@drawable/bottom1" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".1" />

     </LinearLayout>

  </LinearLayout>

   </LinearLayout>

0 个答案:

没有答案