Add HLS playback

pull/653/merge
Varun Patil 2023-05-14 13:57:32 -07:00
parent 403e4404a7
commit d7b550e85a
3 changed files with 34 additions and 7 deletions

View File

@ -43,5 +43,5 @@ dependencies {
implementation 'androidx.exifinterface:exifinterface:1.3.6' implementation 'androidx.exifinterface:exifinterface:1.3.6'
implementation "androidx.media3:media3-exoplayer:$mediaVersion" implementation "androidx.media3:media3-exoplayer:$mediaVersion"
implementation "androidx.media3:media3-ui:$mediaVersion" implementation "androidx.media3:media3-ui:$mediaVersion"
implementation "androidx.media3:media3-exoplayer-dash:$mediaVersion" implementation "androidx.media3:media3-exoplayer-hls:$mediaVersion"
} }

View File

@ -4,15 +4,15 @@ import android.annotation.SuppressLint
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.webkit.WebResourceRequest import android.webkit.*
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import androidx.media3.common.util.Util import androidx.media3.common.util.Util
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.hls.HlsMediaSource
import gallery.memories.databinding.ActivityMainBinding import gallery.memories.databinding.ActivityMainBinding
@UnstableApi class MainActivity : AppCompatActivity() { @UnstableApi class MainActivity : AppCompatActivity() {
@ -110,16 +110,34 @@ import gallery.memories.databinding.ActivityMainBinding
player = null player = null
} }
// Prevent re-creating
playerUri = uri playerUri = uri
playerUid = uid playerUid = uid
// Add cookies from webview to data source
val cookies = CookieManager.getInstance().getCookie(uri.toString())
val httpDataSourceFactory =
DefaultHttpDataSource.Factory()
.setDefaultRequestProperties(mapOf("cookie" to cookies))
.setAllowCrossProtocolRedirects(true)
val dataSourceFactory = DefaultDataSource.Factory(this, httpDataSourceFactory)
// Create media item from local or remote uri
val mediaItem = MediaItem.fromUri(uri)
// Build exoplayer
player = ExoPlayer.Builder(this) player = ExoPlayer.Builder(this)
.build() .build()
.also { exoPlayer -> .also { exoPlayer ->
binding.videoView.player = exoPlayer binding.videoView.player = exoPlayer
binding.videoView.visibility = View.VISIBLE binding.videoView.visibility = View.VISIBLE
val mediaItem = MediaItem.fromUri(uri)
exoPlayer.setMediaItems(listOf(mediaItem), mediaItemIndex, playbackPosition) val hlsMediaSource = HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(mediaItem);
exoPlayer.addMediaSource(hlsMediaSource)
// val mediaItem = MediaItem.fromUri(uri)
// exoPlayer.setMediaItems(listOf(mediaItem), mediaItemIndex, playbackPosition)
exoPlayer.playWhenReady = playWhenReady exoPlayer.playWhenReady = playWhenReady
exoPlayer.prepare() exoPlayer.prepare()
} }

View File

@ -2,6 +2,7 @@ package gallery.memories
import android.R import android.R
import android.graphics.Color import android.graphics.Color
import android.net.Uri
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import android.view.View import android.view.View
@ -117,6 +118,14 @@ import java.net.URLDecoder
}.start() }.start()
} }
@JavascriptInterface
fun playVideoHls(fileId: String?, url: String?) {
if (fileId == null || url == null) return
mActivity.runOnUiThread {
mActivity.initializePlayer(Uri.parse(url), fileId)
}
}
@JavascriptInterface @JavascriptInterface
fun destroyVideo(fileId: String?) { fun destroyVideo(fileId: String?) {
if (fileId == null) return; if (fileId == null) return;