The Kotlin camera2 API video SurfaceView Android development tutorial describes how to use the SurfaceView instead of the TextureView.
This Kotlin on Android tutorial follows on from the camera2 audio episode.
The Android camera2 runtime permissions need to be changed slightly to support the SurfaceView. The CAMERA permission has to be granted prior to adding the SurfaceHolder callback.
In this Kotlin on Android tutorial we will cover:
- Adding SurfaceView to layout
- Setting up SurfaceHolder callback
- Changing visibility of layouts
- Using SurfaceView in camera capture sessions
- Displaying navigation drawer on top of SurfaceView
Episodes
- Icons
- MediaRecorder
- Rear Camera Record
- Round preview thumbnail
- Sharing Uri with ViewModel
- Video Playback with ExoPlayer
- Video PIP
- Front Camera Record (Premium)
- Video Record with Audio (Premium)
- Replace TextureView with SurfaceView (Premium)
Disclaimer, it is recommended to follow this Kotlin on Android tutorial series using a Google Pixel or Nexus device.
Please do not try it with the Android Emulator. Bugs have also been observed in camera stacks of none Google devices, which may be exposed by this tutorial series.
Conclusion
The Kotlin camera2 API video SurfaceView Android development tutorial described how to replace the TextureView with SurfaceView.
When starting the app for the first time, the runtime permission dialog will not affect the SurfaceHolder callbacks.
To get around this, the layout is made visible and SurfaceHolder callbacks added after the user has granted access to the CAMERA permission.
When the application is paused, the visibility of the parent layout gets set to GONE and the SurfaceHolder callback is removed.
The other areas of the SurfaceView remain similar to the TextureView in regards to the camera capture session.