使用surfaceView捕获视频

发布于 2025-01-04 13:16:46 字数 122 浏览 1 评论 0原文

我有一个应用程序,我想使用 Surfaceview 捕获视频并将其存储在我自己创建的文件夹中,这可能吗?

它仅存储视频的默认文件夹。

感谢在andvac......

I have an Application in that I want to capture video using Surfaceview and stored it in my own created folder it is possible?

It stored video only default folder.

Thanks in andvace.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2025-01-11 13:16:46

使用此代码

public class record extends Activity implements OnClickListener, SurfaceHolder.Callback{

MediaRecorder recorder;
SurfaceHolder holder;
boolean recording=false;
public static final String TAG = "VIDEOCAPTURE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    recorder = new MediaRecorder();// Instantiate our media recording object
    initRecorder();
    setContentView(R.layout.view);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_view);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    cameraView.setClickable(true);// make the surface view clickable
    cameraView.setOnClickListener((OnClickListener) this);// onClicklistener to be called when the surface view is clicked
}

private void initRecorder() {// this takes care of all the mediarecorder settings
    File OutputFile = new File(Environment.getExternalStorageDirectory().getPath());
    String video= "/DCIM/100MEDIA/Video";
    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
     recorder.setProfile(cpHigh);        

    //recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    // default microphone to be used for audio
   // recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// default camera to be used for video capture.
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// generally used also includes h264 and best for flash
   // recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //well known video codec used by many including for flash
    //recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// typically amr_nb is the only codec for mobile phones so...

    //recorder.setVideoFrameRate(15);// typically 12-15 best for normal use. For 1080p usually 30fms is used.
   // recorder.setVideoSize(720,480);// best size for resolution.
    //recorder.setMaxFileSize(10000000);
    recorder.setOutputFile(OutputFile.getAbsolutePath()+video+".3gp");
    //recorder.setVideoEncodingBitRate(256000);//
    //recorder.setAudioEncodingBitRate(8000);
   recorder.setMaxDuration(600000);


}

/*if(record.setMaxDuration>60000){

    recorder.stop();
    MediaRecorder.OnInfoListener;
    Toast display = Toast.makeText(this, "You have exceeded the record time", Toast.LENGTH_SHORT);// toast shows a display of little sorts
    display.show();
    return true;
}*/

private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}

public void onClick(View v) {
    if (recording) {
        recorder.stop();
        recording = false;

        // Let's initRecorder so we can record again
        initRecorder();
        prepareRecorder();
        Toast display = Toast.makeText(this, "Stopped Recording", Toast.LENGTH_SHORT);// toast shows a display of little sorts
        display.show();


    } else {

        recorder.start();
        Log.v(TAG,"Recording Started"); 
        recording = true;

    }
}

public void surfaceCreated(SurfaceHolder holder) {
    initRecorder();
    Log.v(TAG,"surfaceCreated");
    prepareRecorder();
}

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

public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    finish();

}

,不要忘记授予相机权限

Use this code

public class record extends Activity implements OnClickListener, SurfaceHolder.Callback{

MediaRecorder recorder;
SurfaceHolder holder;
boolean recording=false;
public static final String TAG = "VIDEOCAPTURE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    recorder = new MediaRecorder();// Instantiate our media recording object
    initRecorder();
    setContentView(R.layout.view);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_view);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    cameraView.setClickable(true);// make the surface view clickable
    cameraView.setOnClickListener((OnClickListener) this);// onClicklistener to be called when the surface view is clicked
}

private void initRecorder() {// this takes care of all the mediarecorder settings
    File OutputFile = new File(Environment.getExternalStorageDirectory().getPath());
    String video= "/DCIM/100MEDIA/Video";
    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
     recorder.setProfile(cpHigh);        

    //recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    // default microphone to be used for audio
   // recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// default camera to be used for video capture.
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// generally used also includes h264 and best for flash
   // recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //well known video codec used by many including for flash
    //recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// typically amr_nb is the only codec for mobile phones so...

    //recorder.setVideoFrameRate(15);// typically 12-15 best for normal use. For 1080p usually 30fms is used.
   // recorder.setVideoSize(720,480);// best size for resolution.
    //recorder.setMaxFileSize(10000000);
    recorder.setOutputFile(OutputFile.getAbsolutePath()+video+".3gp");
    //recorder.setVideoEncodingBitRate(256000);//
    //recorder.setAudioEncodingBitRate(8000);
   recorder.setMaxDuration(600000);


}

/*if(record.setMaxDuration>60000){

    recorder.stop();
    MediaRecorder.OnInfoListener;
    Toast display = Toast.makeText(this, "You have exceeded the record time", Toast.LENGTH_SHORT);// toast shows a display of little sorts
    display.show();
    return true;
}*/

private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}

public void onClick(View v) {
    if (recording) {
        recorder.stop();
        recording = false;

        // Let's initRecorder so we can record again
        initRecorder();
        prepareRecorder();
        Toast display = Toast.makeText(this, "Stopped Recording", Toast.LENGTH_SHORT);// toast shows a display of little sorts
        display.show();


    } else {

        recorder.start();
        Log.v(TAG,"Recording Started"); 
        recording = true;

    }
}

public void surfaceCreated(SurfaceHolder holder) {
    initRecorder();
    Log.v(TAG,"surfaceCreated");
    prepareRecorder();
}

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

public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    finish();

}

and do not forget to give camera permission

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文