无法在Android中使用SurfaceView相机保存视频录制文件

时间:2016-02-11 09:00:29

标签: java android

我能够录制视频文件但无法将视频录制的文件保存在手机内存中。所以请任何人帮我将录制的视频文件保存在手机内存中。我是android新手请帮助我。

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
    private final String VIDEO_PATH_NAME = "/sdcard/default.mp4";
    public static final int MEDIA_TYPE_VIDEO = 2;
    private MediaRecorder mMediaRecorder;
    private Camera mCamera;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mHolder;
    private View mToggleButton;
    private boolean mInitSuccesful;
    private File fileUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

        mToggleButton = (ToggleButton) findViewById(R.id.toggleRecordingButton);
        mToggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            // toggle video recording
            public void onClick(View v) {
                if (((ToggleButton) v).isChecked()) {
                    mMediaRecorder.start();
                    try {
                        Thread.sleep(10 * 1000); 
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    finish();
                } else {
                    mMediaRecorder.stop();
                    mMediaRecorder.reset();
                    try {
                        initRecorder(mHolder.getSurface());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void initRecorder(Surface surface) throws IOException {
        if (mCamera == null) {
            mCamera = Camera.open();
            mCamera.unlock();
        }
        if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setPreviewDisplay(surface);
        mMediaRecorder.setCamera(mCamera);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setOutputFile("/sdcard/videocapture_example.mp4");
        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        mInitSuccesful = true;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (!mInitSuccesful)
                initRecorder(mHolder.getSurface());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        shutdown();
    }

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

    private void shutdown() {
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mCamera.release();
        mMediaRecorder = null;
        mCamera = null;
    }

    private File getOutputMediaFileUri(int type) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        Log.d("Media Store Directory", "" + mediaStorageDir);
        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_" + timeStamp + ".mp4");
            Log.d("Video file", "" + mediaFile);
        } else {
            return null;
        }
        return mediaFile;
    }
}

main.xml中

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

    <ToggleButton
        android:id="@+id/toggleRecordingButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textOff="Start Recording"
        android:textOn="Stop Recording" />

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

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></SurfaceView>

    </FrameLayout>
</LinearLayout>

许可

<uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

1 个答案:

答案 0 :(得分:0)

我认为您用于保存文件的路径不正确

mMediaRecorder.setOutputFile("/sdcard/videocapture_example.mp4");

尝试:

mMediaRecorder.setOutputFile(new File(Environment.getExternalStorageDirectory(),"videocapture_example.mp4");

此方法Environment.getExternalStorageDirectory()不一定必须返回SD卡的路径。 Here你可以找到更多相关细节。