[go: up one dir, main page]

Skip to content

Commit

Permalink
Support playback of video through network. Modify the MediaMetadataRe…
Browse files Browse the repository at this point in the history
…triever data sources to support remote urls. Move this to an IO Thread.
  • Loading branch information
MayuriKhinvasara committed Aug 5, 2024
1 parent be8e7df commit fb1b3f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".SocialApp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,14 @@ private fun VideoMessagePreview(videoUri: String, onClick: () -> Unit) {
val bitmapState = produceState<Bitmap?>(initialValue = null) {
withContext(Dispatchers.IO) {
val mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(context, Uri.parse(videoUri))

//Locally saved files
if (videoUri.contains("content://")) {
mediaMetadataRetriever.setDataSource(context, Uri.parse(videoUri),)
} else {
//Remote url
mediaMetadataRetriever.setDataSource(videoUri, HashMap<String, String>())
}
// Return any frame that the framework considers representative of a valid frame
value = mediaMetadataRetriever.frameAtTime
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
Expand All @@ -66,6 +68,8 @@ import coil.request.ImageRequest
import com.google.android.samples.socialite.R
import com.google.android.samples.socialite.ui.rememberIconPainter
import kotlin.math.absoluteValue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Composable
fun Timeline(
Expand Down Expand Up @@ -238,15 +242,23 @@ fun MetadataOverlay(modifier: Modifier, mediaItem: TimelineMediaItem) {
) {
if (mediaItem.type == TimelineMediaType.VIDEO) {
val mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(
LocalContext.current,
Uri.parse(mediaItem.uri),
)
val context = LocalContext.current.applicationContext

val duration =
mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
?.toLong()
duration?.let {
val duration: State<Long?> = produceState<Long?>(initialValue = null) {
withContext(Dispatchers.IO) {
//Locally saved files
if (mediaItem.uri.contains("content://")) {
mediaMetadataRetriever.setDataSource(context, Uri.parse(mediaItem.uri),)
} else {
//Remote url
mediaMetadataRetriever.setDataSource(mediaItem.uri, HashMap<String, String>())
}
value =
mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
?.toLong()
}
}
duration.value?.let {
val seconds = it / 1000L
val minutes = seconds / 60L
Box(
Expand Down

0 comments on commit fb1b3f3

Please sign in to comment.