In the previous section, we seen how to play a Quad video in 4V using the ExoPlayer. Playing a SBS video is very similar to playing a Quad video using the SDK.
For this section, we will stream a 2x1 video using ExoPlayer.
Resources
For this section, we will be working with a SBS video. We will be streaming the video using this url
As we are going to be streaming the 2x1 video from the internet, you need to add the Internet permission in your app's manifest file.
The QuadView is a reusable Android view which allows you to easily render a quad with just one line of code.
Link the QuadView to your Activity or Fragment
Obtain the reference to the QuadView in your Activity or Fragment using findViewById()
val quadView: QuadView=findViewById(R.id.quad_view)
QuadView quadView = findViewById(R.id.quad_view);
Implement SurfaceTextureReady Callbacks
In order to play videos on the QuadView, you need to obtain the SurfaceTexture when it becomes available. You can do so by implementing the SurfaceTextureReadyCallback which will notify you when the Surface Texture becomes available via callbacks.
publicclassVideoActivityextendsAppCompatActivityimplementsSurfaceTextureReadyCallback {// rest of activity code @OverridepublicvoidonSurfaceTextureReady(SurfaceTexture surfaceTexture) {// TODO }}
Attach Callbacks to QuadView
We now have to attach the callback to the QuadView. This registers the callback so the QuadView can notify us when the surface texture becomes available. We can do this by calling getInputSurfaceTexture() as shown below
val quadView: QuadView=findViewById(R.id.quad_view)quadView.getInputSurfaceTexture(this)
This step is where it gets a bit different from playing a regular Quad video. For SBS videos, we need to first pass the surface to the SBS Surface Renderer. The SBS Surface renderer performs real time conversion from 2x1 to 2x2.
You can obtain the converted surface texture via callbacks. Pass this surface to the ExoPlayer to play the video in 4V.
classVideoActivity : AppCompatActivity(), SurfaceTextureReadyCallback {// rest of activity codeprivatelateinitvar sbsVideoSurfaceRenderer: SbsVideoSurfaceRendereroverridefunonSurfaceTextureReady(surfaceTexture: SurfaceTexture?) {// Initialize and pass in surface texture to the SBSVideoSurfaceRendererif (sbsVideoSurfaceRenderer ==null) { sbsVideoSurfaceRenderer =SbsVideoSurfaceRenderer(this,Surface(surfaceTexture), TextureShape.LANDSCAPE, ) {configureExoplayer(it) } } }}privatefunconfigureExoplayer(surfaceTexture: SurfaceTexture) {// Note: This is appropriate for 2x2 textures on Hydrogen One. exoPlayer.setVideoSurface(Surface(surfaceTexture))val userAgent = Util.getUserAgent(this, "exoplayer2example")val uri = Uri.parse( "https://dev.streaming.leialoft.com/out/v1/08cd49f09fbc4a1e9c063424fa0bfc00/7845cda1bdd5494db13a24f5d13374ea/adacb4edf0434177ae441f124d989fe7/index.mpd"
)val dataSourceFactory: DataSource.Factory =DefaultHttpDataSourceFactory(userAgent)val videoSource: MediaSource= DashMediaSource.Factory(dataSourceFactory).createMediaSource(uri)val loopingSource =LoopingMediaSource(videoSource) exoPlayer.prepare(loopingSource) }