Andriod- 使用MediaRecord进行录屏

Posted cxeye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Andriod- 使用MediaRecord进行录屏相关的知识,希望对你有一定的参考价值。

试了一下可以:

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进行录屏的主要内容,如果未能解决你的问题,请参考以下文章

MediaRecord一些使用记录

Android 音频录制-MediaRecord

mediarecord两种录音方式

MediaRecord录像声音小问题

MediaRecord录像声音小问题

使用javacv录像,同时进行讯飞声纹认证