日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

Android開發實現錄屏小功能

瀏覽:25日期: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
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
97精品中文字幕| 色88888久久久久久影院| 欧美一区激情| 国产精品www.| 精品美女在线视频| 丝袜av一区| 国产探花在线精品一区二区| 日本国产一区| 国产精品久久| 激情综合自拍| 日韩av首页| 久久精品123| 欧美精品激情| 日韩中文字幕视频网| 欧美一级二级三级视频| 欧美视频二区| 美女国产精品久久久| 日韩高清中文字幕一区二区| 亚洲美女久久精品| 99国产精品| 国产精品成人3p一区二区三区| 中文字幕中文字幕精品| 国产欧美一区二区精品久久久 | 一区二区三区四区精品视频| 在线精品一区二区| 国产精品magnet| 日韩精品影视| 国产欧美日韩精品高清二区综合区| 午夜性色一区二区三区免费视频| 麻豆成人综合网| 性欧美69xoxoxoxo| 欧美日韩 国产精品| 波多视频一区| 亚洲乱码久久| 国产精品久久久免费| а√在线中文在线新版| 一本综合精品| 国产精品网址| 国模 一区 二区 三区| 99综合视频| 精品国产欧美日韩| 在线视频精品| 国产在线日韩精品| 日韩av午夜在线观看| 美女少妇全过程你懂的久久| 国产精品美女在线观看直播| 精品国产乱码久久久| 午夜电影亚洲| 国产成人精品一区二区三区视频 | 精品色999| 丝袜亚洲另类欧美| av资源亚洲| 国产亚洲一卡2卡3卡4卡新区| 91精品亚洲| 国产一区欧美| 欧美经典一区| 国产精区一区二区| 亚洲精品影视| 欧美专区18| 香蕉成人久久| 亚洲日本国产| 最新国产精品久久久| 美女精品一区二区| 国产精品久av福利在线观看| 日韩av电影一区| 亚洲一区网站| 今天的高清视频免费播放成人| 精品视频99| 日韩在线欧美| 四虎4545www国产精品 | 亚洲精选久久| 久久国产福利| 视频一区日韩| 男人的天堂亚洲一区| 欧美1区2区3区| 黑丝一区二区| 久久精品凹凸全集| 久久精品99国产精品日本| 国产精品免费99久久久| 国产伦久视频在线观看| 91亚洲人成网污www| 久久精品主播| 在线亚洲一区| 一区二区三区午夜视频| 日本高清久久| 国产麻豆精品久久| 精品视频自拍| 1024精品久久久久久久久| 久久影院午夜精品| 久久99国产精品视频| 国产麻豆久久| 亚洲另类av| 精品久久福利| 亚洲成人一区在线观看| 美女少妇全过程你懂的久久| 日韩专区欧美专区| se01亚洲视频| 国产欧美日本| 宅男在线一区| 日韩欧美美女在线观看| 久久免费视频66| 国产一级久久| 四季av一区二区凹凸精品| 亚洲天堂1区| 日韩精品久久理论片| 日韩成人精品一区| 99国产精品99久久久久久粉嫩| 日本特黄久久久高潮| 午夜国产一区二区| 亚洲乱码久久| 麻豆91精品| 在线精品小视频| 黄色精品视频| 国产精品蜜月aⅴ在线| 99精品在线| 国产日韩高清一区二区三区在线 | 久久高清精品| 亚洲精选91| 午夜精品影院| 国产资源在线观看入口av| 久久福利一区| 久久中文字幕av一区二区不卡| 麻豆免费精品视频| 日韩有吗在线观看| 99久久九九| 日韩欧美在线中字| 国内自拍视频一区二区三区| 久久婷婷国产| 国产精品亚洲欧美日韩一区在线| 午夜久久tv| 午夜欧美视频| 国产福利91精品一区二区| 日本欧美大码aⅴ在线播放| 国产色综合网| 久久亚洲欧洲| 丝袜a∨在线一区二区三区不卡| 激情综合自拍| 婷婷色综合网| 一区在线观看| 免费精品国产| 天堂av一区| 成人在线免费观看91| 卡一卡二国产精品| 国产欧洲在线| 久久久天天操| 视频一区中文字幕| 高清久久一区| 免费在线亚洲| 超碰在线99| 国产66精品| 国产综合婷婷| 久久av在线| 久久精品xxxxx| 日本а中文在线天堂| 午夜久久福利| 国产极品一区| 午夜精品网站| 日韩av资源网| 91亚洲国产成人久久精品| 天堂а√在线最新版中文在线| 国产aa精品| 新版的欧美在线视频| 欧洲一区二区三区精品| 黄色日韩在线| 婷婷精品久久久久久久久久不卡| 精品久久网站| 视频在线观看91| 国产精品色在线网站| 成人精品亚洲| 亚洲人成网站在线在线观看| 国产一区二区三区四区二区| 99久久婷婷这里只有精品| 日韩高清欧美激情| 欧美日韩国产一区二区三区不卡| 亚洲国产不卡| 国产精品成人国产| 亚洲精品一二三**| 免费在线小视频| 中文一区一区三区免费在线观 | 色老板在线视频一区二区| 日韩福利视频导航| 91tv亚洲精品香蕉国产一区| 日本成人一区二区| 日韩有吗在线观看| 婷婷综合网站| 香蕉成人av| 日韩av影院| 亚洲一区日韩| 日本一区二区高清不卡| 日韩精品中文字幕一区二区| 啪啪国产精品| 久久av综合| 精品久久美女| 久久精品女人| 日本不卡的三区四区五区| 久久国产中文字幕| 中文字幕系列一区| 日本一二区不卡| 久久一区国产| 精品资源在线|