MediaRecorder showing Camera Preview for Audio Recording
I am preparing a small android app which capture photo's & audio recordings from user. Something like user will take a photo with his camera and then add audio comment while viewing that picture.
To capture audio I am using MediaRecorder [because android default audio recorder will not allow the app to show custom image while audio recording is done]. When app doesn't have CAMERA permission MediaRecorder is working as I expect. User can look at the custom screen and record the audio. But when CAMERA permission is given to the app MediaRecorder is opening the native camera preview, even though I am not setting the video source for the MediaRecorder. It is as if you can't record audio without camera preview when the app has CAMERA permission.
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.Manifest.permission.CAMERA;
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
I went ahead and tried to create 1 pixel * 1 pixel camera preview with custom Surface
mCamera = getCameraInstance();
mPreview = new CameraPreview(getApplicationContext(), mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
mediaRecorder.setCamera(mCamera);
mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
But my MI phone is ignoring and going ahead with the full camera preview. So is there a simple way to record audio with MediaRecorder without launching the native camera when Camera permission exists ?
From my experience, 1x1 pixel preview is not reliable, and many devices will not work with this correctly. On some devices, you can push the SurfaceView off screen with clever layout (eg huge left margin), but not on others. It is possible to hide a SurfaceView under other elements of your view hierarchy, but this doesn't always work smoothly, because the framework sometimes creates a Surface too late (I believe it's a bug on some devices, not intended).
It is safer to use TextureView, as in the MediaRecorder official sample. You can set visibility of the TextureView to hidden , or hide it behind some other view, and you can make it very small. Here you can find more details: https://stackoverflow.com/a/21447945/192373.
链接地址: http://www.djcxy.com/p/31136.html上一篇: 错误相机不是aviable