Skip to content

Commit

Permalink
Merge pull request #87 from android/support_video_playback_from_internet
Browse files Browse the repository at this point in the history
Video playback support via network
  • Loading branch information
MayuriKhinvasara authored Aug 6, 2024
2 parents be8e7df + f4aedca commit 490acda
Show file tree
Hide file tree
Showing 3 changed files with 30 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 @@ -379,10 +379,18 @@ private fun MessageBubble(
@Composable
private fun VideoMessagePreview(videoUri: String, onClick: () -> Unit) {
val context = LocalContext.current.applicationContext

// Running on an IO thread for loading metadata from remote urls to reduce lag time
val bitmapState = produceState<Bitmap?>(initialValue = null) {
withContext(Dispatchers.IO) {
val mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(context, Uri.parse(videoUri))

// Remote url
if (videoUri.contains("https://")) {
mediaMetadataRetriever.setDataSource(videoUri, HashMap<String, String>())
} else { // Locally saved files
mediaMetadataRetriever.setDataSource(context, Uri.parse(videoUri))
}
// 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 {
// Running on an IO thread for loading metadata from remote urls to reduce lag time
val duration: State<Long?> = produceState<Long?>(initialValue = null) {
withContext(Dispatchers.IO) {
// Remote url
if (mediaItem.uri.contains("https://")) {
mediaMetadataRetriever.setDataSource(mediaItem.uri, HashMap<String, String>())
} else { // Locally saved files
mediaMetadataRetriever.setDataSource(context, Uri.parse(mediaItem.uri))
}
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 490acda

Please sign in to comment.