电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android開發實現錄屏小功能

瀏覽:3日期:2022-09-23 09:16:12

最近開發中,要實現錄屏功能,查閱相關資料,發現調用 MediaProjectionManager的api 實現錄屏功能即可:

import android.Manifest;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.media.projection.MediaProjectionManager;import android.os.Build;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;public class RecordScreenActivity extends Activity { private boolean isRecord = false; private int mScreenWidth; private int mScreenHeight; private int mScreenDensity; private int REQUEST_CODE_PERMISSION_STORAGE = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestPermission(); getScreenBaseInfo(); startScreenRecord(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1000) { if (resultCode == RESULT_OK) {//獲得錄屏權限,啟動Service進行錄制Intent intent = new Intent(this, ScreenRecordService.class);intent.putExtra('resultCode', resultCode);intent.putExtra('resultData', data);intent.putExtra('mScreenWidth', mScreenWidth);intent.putExtra('mScreenHeight', mScreenHeight);intent.putExtra('mScreenDensity', mScreenDensity);startService(intent);finish(); } } } //start screen record private void startScreenRecord() { //Manages the retrieval of certain types of MediaProjection tokens. MediaProjectionManager mediaProjectionManager =(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); //Returns an Intent that must passed to startActivityForResult() in order to start screen capture. Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent(); startActivityForResult(permissionIntent, 1000); } /** * 獲取屏幕基本信息 */ private void getScreenBaseInfo() { //A structure describing general information about a display, such as its size, density, and font scaling. DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); mScreenWidth = metrics.widthPixels; mScreenHeight = metrics.heightPixels; mScreenDensity = metrics.densityDpi; } @Override protected void onDestroy() { super.onDestroy(); } private void requestPermission() { if (Build.VERSION.SDK_INT >= 23) { String[] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA }; for (String str : permissions) {if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) { this.requestPermissions(permissions, REQUEST_CODE_PERMISSION_STORAGE); return;} } } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode==REQUEST_CODE_PERMISSION_STORAGE){ startScreenRecord(); } }}

service 里面進行相關錄制工作

import android.app.Service;import android.content.Context;import android.content.Intent;import android.hardware.display.DisplayManager;import android.hardware.display.VirtualDisplay;import android.media.MediaRecorder;import android.media.projection.MediaProjection;import android.media.projection.MediaProjectionManager;import android.os.Environment;import android.os.IBinder;import android.support.annotation.Nullable;import android.util.Log;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by dzjin on 2018/1/9. */ public class ScreenRecordService extends Service { private int resultCode; private Intent resultData=null; private MediaProjection mediaProjection=null; private MediaRecorder mediaRecorder=null; private VirtualDisplay virtualDisplay=null; private int mScreenWidth; private int mScreenHeight; private int mScreenDensity; private Context context=null; @Override public void onCreate() { super.onCreate(); } /** * Called by the system every time a client explicitly starts the service by calling startService(Intent), * providing the arguments it supplied and a unique integer token representing the start request. * Do not call this method directly. * @param intent * @param flags * @param startId * @return */ @Override public int onStartCommand(Intent intent, int flags, int startId) { try{ resultCode=intent.getIntExtra('resultCode',-1); resultData=intent.getParcelableExtra('resultData'); mScreenWidth=intent.getIntExtra('mScreenWidth',0); mScreenHeight=intent.getIntExtra('mScreenHeight',0); mScreenDensity=intent.getIntExtra('mScreenDensity',0); mediaProjection=createMediaProjection(); mediaRecorder=createMediaRecorder(); virtualDisplay=createVirtualDisplay(); mediaRecorder.start(); }catch (Exception e) { e.printStackTrace(); } /** * START_NOT_STICKY: * Constant to return from onStartCommand(Intent, int, int): if this service’s process is * killed while it is started (after returning from onStartCommand(Intent, int, int)), * and there are no new start intents to deliver to it, then take the service out of the * started state and don’t recreate until a future explicit call to Context.startService(Intent). * The service will not receive a onStartCommand(Intent, int, int) call with a null Intent * because it will not be re-started if there are no pending Intents to deliver. */ return Service.START_NOT_STICKY; } //createMediaProjection public MediaProjection createMediaProjection(){ /** * Use with getSystemService(Class) to retrieve a MediaProjectionManager instance for * managing media projection sessions. */ return ((MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE)).getMediaProjection(resultCode,resultData); /** * Retrieve the MediaProjection obtained from a succesful screen capture request. * Will be null if the result from the startActivityForResult() is anything other than RESULT_OK. */ } private MediaRecorder createMediaRecorder(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat('yyyy-MM-dd-HH-mm-ss'); String filePathName= Environment.getExternalStorageDirectory()+'/'+simpleDateFormat.format(new Date())+'.mp4'; //Used to record audio and video. The recording control is based on a simple state machine. MediaRecorder mediaRecorder=new MediaRecorder(); //Set the video source to be used for recording. mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); //Set the format of the output produced during recording. //3GPP media file format mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //Sets the video encoding bit rate for recording. //param:the video encoding bit rate in bits per second. mediaRecorder.setVideoEncodingBitRate(5*mScreenWidth*mScreenHeight); //Sets the video encoder to be used for recording. mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //Sets the width and height of the video to be captured. mediaRecorder.setVideoSize(mScreenWidth,mScreenHeight); //Sets the frame rate of the video to be captured. mediaRecorder.setVideoFrameRate(60); try{ //Pass in the file object to be written. mediaRecorder.setOutputFile(filePathName); //Prepares the recorder to begin capturing and encoding data. mediaRecorder.prepare(); }catch (Exception e){ e.printStackTrace(); } return mediaRecorder; } private VirtualDisplay createVirtualDisplay(){ /** * name String: The name of the virtual display, must be non-empty.This value must never be null. width int: The width of the virtual display in pixels. Must be greater than 0. height int: The height of the virtual display in pixels. Must be greater than 0. dpi int: The density of the virtual display in dpi. Must be greater than 0. flags int: A combination of virtual display flags. See DisplayManager for the full list of flags. surface Surface: The surface to which the content of the virtual display should be rendered, or null if there is none initially. callback VirtualDisplay.Callback: Callback to call when the virtual display’s state changes, or null if none. handler Handler: The Handler on which the callback should be invoked, or null if the callback should be invoked on the calling thread’s main Looper. */ /** * DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR * Virtual display flag: Allows content to be mirrored on private displays when no content is being shown. */ return mediaProjection.createVirtualDisplay('mediaProjection',mScreenWidth,mScreenHeight,mScreenDensity,DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,mediaRecorder.getSurface(),null,null); } @Override public void onDestroy() { super.onDestroy(); if(virtualDisplay!=null){ virtualDisplay.release(); virtualDisplay=null; } if(mediaRecorder!=null){ mediaRecorder.stop(); mediaRecorder=null; } if(mediaProjection!=null){ mediaProjection.stop(); mediaProjection=null; } } @Nullable @Override public IBinder onBind(Intent intent) { return null; }}

錄屏功能就這么實現了,有什么不妥之處,敬請留言討論。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 超声波焊接机_超音波熔接机_超声波塑焊机十大品牌_塑料超声波焊接设备厂家 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 吲哚菁绿衍生物-酶底物法大肠菌群检测试剂-北京和信同通科技发展有限公司 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 搅拌磨|搅拌球磨机|循环磨|循环球磨机-无锡市少宏粉体科技有限公司 | 精密五金冲压件_深圳五金冲压厂_钣金加工厂_五金模具加工-诚瑞丰科技股份有限公司 | 广州工业氧气-工业氩气-工业氮气-二氧化碳-广州市番禺区得力气体经营部 | 游泳池设备安装工程_恒温泳池设备_儿童游泳池设备厂家_游泳池水处理设备-东莞市君达泳池设备有限公司 | 标策网-专注公司商业知识服务、助力企业发展 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 考勤系统_考勤管理系统_网络考勤软件_政企|集团|工厂复杂考勤工时统计排班管理系统_天时考勤 | 蒸压釜_蒸养釜_蒸压釜厂家-山东鑫泰鑫智能装备有限公司 | 南京交通事故律师-专打交通事故的南京律师| 全自动包衣机-无菌分装隔离器-浙江迦南科技股份有限公司 | 刘秘书_你身边专业的工作范文写作小秘书 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 聚合甘油__盐城市飞龙油脂有限公司 | HV全空气系统_杭州暖通公司—杭州斯培尔冷暖设备有限公司 | 仿真茅草_人造茅草瓦价格_仿真茅草厂家_仿真茅草供应-深圳市科佰工贸有限公司 | 上海小程序开发-上海小程序制作公司-上海网站建设-公众号开发运营-软件外包公司-咏熠科技 | 耐酸碱泵-自吸耐酸碱泵型号「品牌厂家」立式耐酸碱泵价格-昆山国宝过滤机有限公司首页 | 体坛网_体坛+_体坛周报新闻客户端 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 耐酸碱泵-自吸耐酸碱泵型号「品牌厂家」立式耐酸碱泵价格-昆山国宝过滤机有限公司首页 | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 临海涌泉蜜桔官网|涌泉蜜桔微商批发代理|涌泉蜜桔供应链|涌泉蜜桔一件代发 | 粘弹体防腐胶带,聚丙烯防腐胶带-全民塑胶 | 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! | 深圳侦探联系方式_深圳小三调查取证公司_深圳小三分离机构 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 盘古网络技术有限公司| 热镀锌槽钢|角钢|工字钢|圆钢|H型钢|扁钢|花纹板-天津千百顺钢铁贸易有限公司 | b2b网站大全,b2b网站排名,找b2b网站就上地球网| 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 新中天检测有限公司青岛分公司-山东|菏泽|济南|潍坊|泰安防雷检测验收 | 全自动不干胶贴标机_套标机-上海今昂贴标机生产厂家 | 气力输送设备_料封泵_仓泵_散装机_气化板_压力释放阀-河南锐驰机械设备有限公司 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 |