码迷,mamicode.com
首页 > 其他好文 > 详细

Andriod- 使用MediaRecord进行录屏

时间:2020-11-23 11:46:44      阅读:12      评论:0      收藏:0      [点我收藏+]

标签:pack   EOS   storage   nts   tor   andriod   read   end   xmlns   

试了一下可以:

package com.cts.camerademo.camerademo;

import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.Timer;

public class MainActivity extends AppCompatActivity {

    private SurfaceView surfaceView;
    private Button recordButton;
    private Button stopButton;
    private MediaRecorder mediaRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        setContentView(R.layout.activity_main);

        surfaceView = this.findViewById(R.id.surfaceView);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceView.getHolder().setFixedSize(176,144);
        surfaceView.getHolder().setKeepScreenOn(true);

        recordButton = findViewById(R.id.recordButton);
        stopButton = findViewById(R.id.stopButton);

        //mediaRecorder = new MediaRecorder();

    }


    public void record(View v){
        Toast.makeText(MainActivity.this,"record",Toast.LENGTH_LONG).show();


        try {

            File videoFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp");

            if(mediaRecorder==null){
                mediaRecorder = new MediaRecorder();
            }

            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setVideoSize(320,240);
            //mediaRecorder.setVideoFrameRate(5);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
            mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

            //mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

            mediaRecorder.prepare();
            //Thread.sleep(5000);
            mediaRecorder.start();

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



    }

    public void stop(View v){
        //Toast.makeText(MainActivity.this,"stop",Toast.LENGTH_LONG).show();
        if(mediaRecorder!=null){
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
        }

    }
}

前面的代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
            android:id="@+id/recordButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="刻录"
            android:onClick="record"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            />
        <Button
            android:id="@+id/stopButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"
            android:layout_toLeftOf="@id/recordButton"
            android:layout_alignTop="@+id/recordButton"
            android:onClick="stop"
            />

    </RelativeLayout>

</FrameLayout>

 

 

 

package com.cts.camerademo.camerademo;

import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.Timer;

public class MainActivity extends AppCompatActivity {

private SurfaceView surfaceView;
private Button recordButton;
private Button stopButton;
private MediaRecorder mediaRecorder;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.activity_main);

surfaceView = this.findViewById(R.id.surfaceView);
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176,144);
surfaceView.getHolder().setKeepScreenOn(true);

recordButton = findViewById(R.id.recordButton);
stopButton = findViewById(R.id.stopButton);

//mediaRecorder = new MediaRecorder();

}


public void record(View v){
Toast.makeText(MainActivity.this,"record",Toast.LENGTH_LONG).show();


try {

File videoFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp");

if(mediaRecorder==null){
mediaRecorder = new MediaRecorder();
}

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setVideoSize(320,240);
//mediaRecorder.setVideoFrameRate(5);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

//mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

mediaRecorder.prepare();
//Thread.sleep(5000);
mediaRecorder.start();

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



}

public void stop(View v){
//Toast.makeText(MainActivity.this,"stop",Toast.LENGTH_LONG).show();
if(mediaRecorder!=null){
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}

}
}

Andriod- 使用MediaRecord进行录屏

标签:pack   EOS   storage   nts   tor   andriod   read   end   xmlns   

原文地址:https://www.cnblogs.com/cxeye/p/13998479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!