开发者

Recording video?

开发者 https://www.devze.com 2023-01-22 11:58 出处:网络
I\'m trying to make an app that records video and stores the files In a table view. I want to be able to see a preview on each cell and when I press a cel开发者_运维知识库l, a video that was previousl

I'm trying to make an app that records video and stores the files In a table view. I want to be able to see a preview on each cell and when I press a cel开发者_运维知识库l, a video that was previously recorded and stored here starts to play. What's the best way to do this and if there's any tutorials or video tutorials, can someone redirect me there?


Here is the code for recording video:

package milos.stackVideo;



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.CamcorderProfile;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class EnregistrementVideoStackActivity extends Activity implements Callback {
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Button startRecording = null;
//private Button stopRecording = null;
File video;
private Camera mCamera;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_surface);
    Log.i(null , "Video starting");
    startRecording = (Button)findViewById(R.id.buttonstart);
    mCamera = Camera.open();
    surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}




@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, "StartRecording");
menu.add(0, 1, 0, "StopRecording");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case 0:
try {

startRecording();

} catch (Exception e) {
String message = e.getMessage();
Log.i(null, "Problem Start"+message);
mrec.release();
}
break;

case 1: //GoToAllNotes
mrec.stop();
mrec.release();
mrec = null;
break;

default:
break;
}
return super.onOptionsItemSelected(item);
}



protected void startRecording() throws IOException 
{


    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.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    mrec.setPreviewDisplay(surfaceHolder.getSurface());
    mrec.setOutputFile("/sdcard/zzzz.3gp"); 

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

}

protected void stopRecording() {
    mrec.stop();
    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) {

    // TODO Auto-generated method stub

}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

    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();
    // TODO Auto-generated method stub

}


}

It can be usful for somebody.

0

精彩评论

暂无评论...
验证码 换一张
取 消