实现App带有控制悬浮窗的录屏小功能(下)

本小记继续上一篇的代码

主要实现功能为:对编码器编译压缩后输出的视频流用MediaMuxer封装到MP4容器。

//采用线程进行录屏的耗时操作
public class VideoThread extends Thread {

    private Context mContext;
    private static final String MIME_TYPE = "video/avc";
    private VirtualDisplay virtualDisplay;
    private MediaProjection projection;
    private AtomicBoolean mQuit = new AtomicBoolean(false);
    private int videoWidth;
    private int videoHeight;

    private Handler handler = null;

	//构造函数,传入上下文和屏幕大小
    public VideoThread(Context context, int width, int height){
        mContext = context;
        videoWidth = width;
        videoHeight = height;
    }

    public final void quit(){
        mQuit.set(true);
    }

    @Override
    public void run(){
        Surface surface = null;
        MediaMuxer mediaMuxer = null;  //MediaMuxer的使用要按照Constructor -> addTrack -> start -> writeSampleData -> stop 的顺序
        MediaCodec mediaCodec = null;

		//获取文件保存路径
        long timeMillis = System.currentTimeMillis();
        Date date = new Date(timeMillis);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String path = "ScreenShot" + simpleDateFormat.format(date) + ".mp4";
        File file = new File(Environment.getExternalStorageDirectory(), path);
        File parentFile = file.getParentFile();
        if (parentFile == null || !parentFile.exists()) {
            parentFile.mkdir();
        }

		//初始化编码器、创建MediaFormat设置Media格式
        MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, videoWidth, videoHeight);
        f
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值