5

I am trying to use media projection api that became available on lollipop devices (android 5 and above), now I realized 2 ways of using this api with android:

First way:

Using the mediacodec and media muxer.

Second way

Using the media recorder class.

What I did:

I used first media codec and it worked fine on different devices , but the problem came when I needed to add audio (it seemed a bit complicated).

Then I tried to use media recorder as some others used it and seemed simple to add audio, so I did something like this:

//set a media recorder
try{
MediaRecorder mediarecorder = new MediaRecorder();
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediarecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediarecorder.setOutputFile(path_file);
mediarecorder.setVideoSize(480, 270);
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediarecorder.setVideoEncodingBitRate(70000);
mediarecorder.setVideoFrameRate(30);

mediarecorder.prepare();

}catch(IOException e){


}

//density
Metrics metrics = getReources().getDisplayMetrics();
int density = metrics.densityDpi;

//set virtual display

media_projection.createVirtualDisplay("name",480,270,denisty,DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mediarecorder.getSurface(),null,null);

//start
mediarecorder.start();

I am hard coding: width, height, frame rate and bitrate because I need the output video to be controlled, beacause videos tend to get large in size.

My problem:

Using the media recorder method, I realized a problem while testing the api, I used 2 different devices (android 7 and android 6) both are different interms of screen density but approximately they are of same size:

Android 7 device(resolution: 720x1280):

I got perfect results no problem (this device is the higher resolution device).

Android 6 device(resolution: 540x960):

I got a weird corrupted output video, such that if I recorder a white screen i get corrupted video with purple blurry stuff, and if I recorded a blue screen I get a corrupted video with green blurry stuff.

Question:

While these don't occur using media codec, my question is:

why this occurs using media recorder?

Is media recorder stable to use with media projection?

Thanks.

0 Answers0