# Video playback in LazyColumn in Jetpack Compose

DevFeed: [Video playback in LazyColumn in Jetpack Compose](<https://devfeed.tech/articles/video-playback-in-lazycolumn-in-jetpack-compose-25900.md>)

Original publisher: [Read original article](<https://proandroiddev.com/video-playback-in-lazycolumn-in-jetpack-compose-df355097f26e?source=rss-56174fa84bcc------2>)

Author: Denys Rudenko

Published: 2021-10-05T23:51:23Z

Content type: tutorial

Language: en

Sources: [Stories by Denis Rudenko on Medium](<https://devfeed.tech/sources/stories-by-denis-rudenko-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [ExoPlayer](<https://devfeed.tech/topics/exoplayer.md>), [Playback](<https://devfeed.tech/topics/playback.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [article](<https://devfeed.tech/tags/article.md>), [compose](<https://devfeed.tech/tags/compose.md>), [exoplayer](<https://devfeed.tech/tags/exoplayer.md>), [image](<https://devfeed.tech/tags/image.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [playback](<https://devfeed.tech/tags/playback.md>), [state](<https://devfeed.tech/tags/state.md>), [video](<https://devfeed.tech/tags/video.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

A tutorial on implementing manual and automatic video playback in a Jetpack Compose LazyColumn using ExoPlayer. It covers preserving playback positions, pausing videos when cards are not visible, and responding to the application lifecycle.

## Source excerpt

Article will cover both manual & auto-playback of videos in an efficient way, storing/restoring last played video position, pausing playback if video card is not visible to the user and handling application lifecycle. ExoPlayer will be used for video playback, and these videos as a test data set. Coil will be used for displaying the video thumbnails. Video shows manual play/pause & auto-playback versions side to side. https://medium.com/media/98ab3ea482f78b96bf44de2ce54c7168/hrefStep 1: VideosScreen Let's start implementing all of those features step by step. First we create a composable which will contain the exoPlayer instance, list of videos and playingItemIndex: https://medium.com/media/2440139ad1f185b8d1f1446f844530b9/href Very important part here is using keys for the lazyColumn. In short: we provide a key which enables item state to be consistent across data-set changes isPlaying -- we can always know if video is playing or not. If it's playing -- playingItemIndex will represent the position of the item in the list, if nothing is playing -- null. We need this field to know if we should show play/pause icon, and thumbnail image or playerView. VideoCard -- composable which exposes click event on play/pause icon. Click will be handled inside the viewModel. Notice that we're passing current playback position from exoPlayer as well as the index of clicked item. We'll use it to save & restore the playback position for each video. https://medium.com/media/518600644bbec8f225a0c4b933adccd1/href Whenever the play/pause icon is clicked, we're checking for three scenarios: currentlyPlayingIndex is null -- video is not playing at the moment, so we are assigning the videoIndex to currentlyPlayingIndex. currentlyPlayingIndex is the same as videoIndex clicked -- this means that the same video is already playing, and we want to pause the playback. That's why we assign null to currentlyPlayingIndex. To store the last played video position we're mutating list, and saving the position