Play Quad Videos in 4V

Learn to play a Quad (2x2) video on a QuadView using ExoPlayer

The Android Media SDK offers easy integration with the ExoPlayer to play 2x2 videos in 4V. By leveraging the power of ExoPlayer, you can play 2x2 or 2x1 videos using different media sources.

In this guide, you will learn how to load a 2x2 video from the project's asset folder and play it using ExoPlayer on the QuadView.

We won't go over the implementation of ExoPlayer in this tutorial. You can learn more about ExoPlayer here.

Setup Resources

Add the video we will be working with to the assets directory of your project.

You can download the video for this tutorial below.

Add this video file to your project's assets directory as shown below

Setup Environment.

Setup the Android SDK in your Android project by following the steps outlined in the Getting Started Section.

We will also need to add the ExoPlayer dependency to our project. Add the following dependency to your app's build.gradle file.

implementation 'com.google.android.exoplayer:exoplayer:2.11.7'

Add the QuadView to your XML Layout file

  <com.leiainc.androidsdk.core.QuadView
    android:id="@+id/quad_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

The QuadView is a reusable Android view which allows you to easily render a quad with just one line of code.

Obtain the reference to the QuadView in your Activity or Fragment using findViewById()

val 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

class VideoActivity : AppCompatActivity(), SurfaceTextureReadyCallback {
    // rest of activity code
    
    override fun onSurfaceTextureReady(surfaceTexture: SurfaceTexture?) {
        TODO("Not yet implemented")
    }
}

Attach Callbacks to QuadView

We now have to attach the callback to the QuadView. We can do this by calling getInputSurfaceTexture() as shown below.

val quadView: QuadView = findViewById(R.id.quad_view)
quadView.getInputSurfaceTexture(this)

Initialize ExoPlayer

Initialize ExoPlayer in your Activity's onCreate() function. Add exoPlayer as a global variable so it can be accessed in other functions as well.

private lateinit var exoPlayer: SimpleExoPlayer

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_video)
    
    val quadView: QuadView = findViewById(R.id.quad_view)
    quadView.getInputSurfaceTexture(this)

    // Initialize ExoPlayer
    exoPlayer = SimpleExoPlayer.Builder(this).build()
}

Implement Surface Texture Ready

We can now use the surface texture to play the video using ExoPlayer. We can do this by passing the surface texture to the ExoPlayer.

exoPlayer.setVideoSurface(Surface(surfaceTexture))

You can now load the asset and play it using ExoPlayer. We are using a Looping Media Source to loop the video in this example.

class VideoActivity : AppCompatActivity(), SurfaceTextureReadyCallback {
    // rest of activity code
    
    override fun onSurfaceTextureReady(surfaceTexture: SurfaceTexture?) {
        // Set surface Texture in ExoPlayer 
        exoPlayer.setVideoSurface(Surface(surfaceTexture))
        
        // Load Asset
        val mp4VideoUri =
            Uri.parse("asset:///cat_2x2.mp4")

        val userAgent = Util.getUserAgent(this, "exoplayerQuadExample")
        val dataSourceFactory = DefaultDataSourceFactory(this, userAgent)

        // Create a looping Media Source
        val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(mp4VideoUri)
        val loopingSource = LoopingMediaSource(videoSource)

        exoPlayer.prepare(loopingSource)
    }
}

Playing the Video

With the ExoPlayer now setup, we can play the video by calling playWhenReady() in your Activity's onResume() function.

override fun onResume() {
    super.onResume()
    exoPlayer.playWhenReady = true
}

Pause playing the video when the activity's onPause() is called.

override fun onPause() {
    super.onPause()
    exoPlayer.playWhenReady = false
}

You can learn more about using the ExoPlayer here.

Toggle the Backlight

The last step is to switch on the backlight to view the image in 4V. We can request toggling backlight using the LeiaDisplayManager .

Place the following line of code in your Activity/Fragment's onResume() function.

override fun onResume() {
   super.onResume()
   exoPlayer.playWhenReady = true
   
   val displayManager: LeiaDisplayManager? = LeiaSDK.getDisplayManager(applicationContext)
   displayManager?.requestBacklightMode(LeiaDisplayManager.BacklightMode.MODE_3D)
}

This will turn on the backlight on a Leia Light-field device.

To turn off the backlight, you can call the same line of code with BacklightMode.MODE_2D. Its a good practice to turn off the backlight in your activity's onPause() function. This ensures the backlight is turned off when the user exits the app.

override fun onPause() {
   super.onPause()
   exoPlayer.playWhenReady = false
   
   val displayManager: LeiaDisplayManager? = LeiaSDK.getDisplayManager(applicationContext)
   displayManager?.requestBacklightMode(LeiaDisplayManager.BacklightMode.MODE_2D)
}

You can learn more about the LeiaDisplayManager here.

Build your app

If you followed the steps above correctly, you should be able to play the video in 4V. If you had trouble following the steps, you can checkout the sample app on Github

Last updated

Copyright © 2023 Leia Inc. All Rights Reserved