1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| private void prepare() throws IOException, RemoteException, RuntimeException { IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE); IMediaProjectionManager mediaService = IMediaProjectionManager.Stub.asInterface(b); IMediaProjection proj = null; proj = mediaService.createProjection(mUser, mContext.getPackageName(), MediaProjectionManager.TYPE_SCREEN_CAPTURE, false); IBinder projection = proj.asBinder(); mMediaProjection = new MediaProjection(mContext, IMediaProjection.Stub.asInterface(projection)); File cacheDir = mContext.getCacheDir(); cacheDir.mkdirs(); mTempVideoFile = File.createTempFile("temp", ".mp4", cacheDir); mMediaRecorder = new MediaRecorder(); if (mAudioSource == MIC) { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); } mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); DisplayMetrics metrics = new DisplayMetrics(); WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getRealMetrics(metrics); int refreshRate = (int) wm.getDefaultDisplay().getRefreshRate(); int[] dimens = getSupportedSize(metrics.widthPixels, metrics.heightPixels, refreshRate); int width = dimens[0]; int height = dimens[1]; refreshRate = dimens[2]; int vidBitRate = width * height * refreshRate / VIDEO_FRAME_RATE * VIDEO_FRAME_RATE_TO_RESOLUTION_RATIO; mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setVideoEncodingProfileLevel( MediaCodecInfo.CodecProfileLevel.AVCProfileHigh, MediaCodecInfo.CodecProfileLevel.AVCLevel3); mMediaRecorder.setVideoSize(width, height); mMediaRecorder.setVideoFrameRate(refreshRate); mMediaRecorder.setVideoEncodingBitRate(vidBitRate); mMediaRecorder.setMaxDuration(MAX_DURATION_MS); mMediaRecorder.setMaxFileSize(MAX_FILESIZE_BYTES); if (mAudioSource == MIC) { mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC); mMediaRecorder.setAudioChannels(TOTAL_NUM_TRACKS); mMediaRecorder.setAudioEncodingBitRate(AUDIO_BIT_RATE); mMediaRecorder.setAudioSamplingRate(AUDIO_SAMPLE_RATE); } mMediaRecorder.setOutputFile(mTempVideoFile); mMediaRecorder.prepare(); mInputSurface = mMediaRecorder.getSurface(); mVirtualDisplay = mMediaProjection.createVirtualDisplay( "Recording Display", width, height, metrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mInputSurface, null, null); mMediaRecorder.setOnInfoListener(mListener); if (mAudioSource == INTERNAL || mAudioSource == MIC_AND_INTERNAL) { mTempAudioFile = File.createTempFile("temp", ".aac", mContext.getCacheDir()); mAudio = new ScreenInternalAudioRecorder(mTempAudioFile.getAbsolutePath(), mMediaProjection, mAudioSource == MIC_AND_INTERNAL); } }
|