0

我的相机预览工作正常,它还没有完全存储视频:(但是主要问题是当我按下“开始”按钮时它开始录制但是当我按下“停止”时它停止预览并且当我按下“再次开始”时“它使程序崩溃。有没有办法让我可以停止和开始录制而不会崩溃?感谢您的帮助:)

package com.example.camera;

import java.io.IOException;

import android.app.Activity;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.exercise.AndroidVideoCapture.R;

public class MainActivity extends Activity implements SurfaceHolder.Callback{

Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    recording = false;

    mediaRecorder = new MediaRecorder();
    initMediaRecorder();

    setContentView(R.layout.activity_main);

    SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
    surfaceHolder = myVideoView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    myButton = (Button)findViewById(R.id.mybutton);
    myButton.setOnClickListener(myButtonOnClickListener);
}

private Button.OnClickListener myButtonOnClickListener
        = new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(recording){
            mediaRecorder.stop();
            myButton.setText("START");
            recording = false;
            //mediaRecorder.reset();
            //mediaRecorder.release();

        }else{
            mediaRecorder.start();
            recording = true;
            myButton.setText("STOP");
        }
    }};

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
    prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

private void initMediaRecorder(){
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mediaRecorder.setProfile(camcorderProfile_HQ);
    mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
    mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
}

private void prepareMediaRecorder(){
    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <SurfaceView
            android:id="@+id/videoview"
            android:layout_width="720px"
            android:layout_height="480px"/>
    <Button
            android:id="@+id/mybutton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="REC"
            android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
4

1 回答 1

0

将您的 onClick 方法更改为:

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(recording){
        mediaRecorder.stop();
        myButton.setText("START");
        recording = false;
        initMediaRecorder();
        prepareMediaRecorder();

    }else{
        mediaRecorder.start();
        recording = true;
        myButton.setText("STOP");
    }
}};

也使用此方法不覆盖您的视频文件:

private File getOutputMediaFile() {
    // To be safe, you can check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath().toString() + "/YourDirectory/");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "VID_" + timeStamp + ".mp4");
    return mediaFile;
}

像这样使用它来设置输出文件:

mediaRecorder.setOutputFile(getOutputMediaFile().getPath());
于 2013-08-26T12:14:50.060 回答