MediaRecorder and CameraX

886 views
Skip to first unread message

Jonathan KPEYI

unread,
Mar 15, 2022, 4:56:43 AM3/15/22
to Android CameraX Discussion Group
Hello devs,
Is there a way of recording video with cameraX and MediaRecorder combination? 
I really need some features offered by MediaRecorder , and CameraX too.

Thanks. ❤

Leo Huang

unread,
Mar 15, 2022, 6:37:19 AM3/15/22
to Android CameraX Discussion Group, jkp...@gmail.com
Have you considered using CameraX Recorder? This is recommended when using CameraX to record video.
You can learn it from here and here.

If you really need MediaRecorder, there is a workaround but not guarantee to work normally on every device. Sometimes on some devices it may need some extra work to get things going.
i.e. create a Preview use case as the surface receiver of MediaRecorder.
ex: 
    Size videoSize = new Size(640, 480); // you have to find out a device supported resolution, see sample

    mRecorder = new MediaRecorder();
    // setup recorder
    mRecorder.setVideoSize(videoSize.getWidth(), videoSize.getHeight());
    mRecorder.prepare();

    Preview preview = new Preview.Builder()
            .setTargetResolution(videoSize)
            .build();

    preview.setSurfaceProvider(new Preview.SurfaceProvider() {
            @Override
            public void onSurfaceRequested(@NonNull SurfaceRequest request) {
                request.provideSurface(mRecorder.getSurface(), mExecutor,
                new Consumer<SurfaceRequest.Result>() {
                        @Override
                        public void accept(SurfaceRequest.Result result) {
                            // A safe place to release mRecorder since camera no longer use the surface.
                        }
                });
           }
   });


jkp...@gmail.com 在 2022年3月15日 星期二下午4:56:43 [UTC+8] 的信中寫道:

Jonathan KPEYI

unread,
Mar 16, 2022, 4:50:23 AM3/16/22
to Android CameraX Discussion Group
Here is the https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:camera/camera-video/src/main/java/androidx/camera/video/AudioSpec.java code source

And I see clearly that there is a use of MediaRecorder.AudioSource.

If I could just have the possibility to change that line or to create a function that provides the source, it Will be fine. 

Saying so, how can I do that, create an extension? Edit the CameraX source code and recompile?

Thanks. A little bit newbie 😃
Screenshot_20220316-094459.jpg

Leo Huang

unread,
Mar 16, 2022, 11:31:07 AM3/16/22
to Android CameraX Discussion Group, jkp...@gmail.com
One way is to download the CameraX source from https://github.com/androidx/androidx#checking-out-the-code
and there is a chapter that show how to "Testing modified AndroidX Libraries to in your App"
Then you can change the code, build the aar file and put into your app.
And yes, in AuidoSpec.java, you can simply assign any audio source value you want to SOURCE_CAMCORDER.
or in AudioConfigUtil.java, assign the audio source value to AUDIO_SOURCE_DEFAULT which is more appropriate.

Btw, just noticed that actually Recorder.Builder has a non public method setAudioSouce(int). You may use java reflection to invoke it.
But please noted that since it is a not public so it might be changed in the future.
ex:
Recorder.Builder builder = new Recorder.Builder();
try {
    Method method = Recorder.Builder.class.getDeclaredMethod("setAudioSource", int.class);
    method.setAccessible(true);
    method.invoke(builder, new Object[]{MediaRecorder.AudioSource.VOICE_PERFORMANCE});
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
    // Error handle
}
jkp...@gmail.com 在 2022年3月16日 星期三下午4:50:23 [UTC+8] 的信中寫道:

Leo Huang

unread,
Mar 16, 2022, 11:38:26 AM3/16/22
to Android CameraX Discussion Group, Leo Huang, jkp...@gmail.com
Forgot to mention, since it's not public, the functionality isn't guaranteed either.

Leo Huang 在 2022年3月16日 星期三下午11:31:07 [UTC+8] 的信中寫道:

Jonathan KPEYI

unread,
Mar 16, 2022, 12:05:29 PM3/16/22
to Leo Huang, Android CameraX Discussion Group
Hi  Leo, ☺️ , this looks great. Let me try the reflection method first. 
Grateful 🙏

Jonathan KPEYI

unread,
Mar 16, 2022, 12:54:25 PM3/16/22
to Leo Huang, Android CameraX Discussion Group
I'm back.
It's worked (the java reflection method).

But I have two questions.

1- I've noticed that even the native camera app have changed the sound quality (eventually due to the reflection method?) If so, that is a good new.

2- You said, as the method isn't public, (setAudioSource) , it can change later. 

If the library has been updated, how it will affect my app, which is eventually built and deployed.

Eric Ng

unread,
Mar 16, 2022, 1:48:52 PM3/16/22
to Jonathan KPEYI, Leo Huang, Android CameraX Discussion Group
Hi Jonathan,

If the library has been updated, how it will affect my app, which is eventually built and deployed.

If your app is already built and deployed with a version of CameraX it won't change until you update it and re-deploy it. We encourage you to check out the release notes for updated version [Link] and test your app for core use cases. But you always control the versions based on the gradle dependencies.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/CAMCTNspS_MTq9r_HzKQ-pap5uhULRvAr10a2Zn-Ua0tfK9KHEQ%40mail.gmail.com.


--
-Eric Ng

Jonathan KPEYI

unread,
Mar 16, 2022, 3:45:35 PM3/16/22
to Eric Ng, Leo Huang, Android CameraX Discussion Group
Hello Eric,  Thanks for your reply. 
I do understand now that point.
Grateful 🙏☺️
Reply all
Reply to author
Forward
0 new messages