properly handle video content uri at exoplayer

This commit is contained in:
tibbi 2018-06-29 23:36:59 +02:00
parent 0ca8cca358
commit bb0b432d19

View file

@ -20,6 +20,7 @@ import com.google.android.exoplayer2.source.ExtractorMediaSource
import com.google.android.exoplayer2.source.TrackGroupArray import com.google.android.exoplayer2.source.TrackGroupArray
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.trackselection.TrackSelectionArray import com.google.android.exoplayer2.trackselection.TrackSelectionArray
import com.google.android.exoplayer2.upstream.ContentDataSource
import com.google.android.exoplayer2.upstream.DataSource import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DataSpec import com.google.android.exoplayer2.upstream.DataSpec
import com.google.android.exoplayer2.upstream.FileDataSource import com.google.android.exoplayer2.upstream.FileDataSource
@ -150,9 +151,10 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
override fun onRenderedFirstFrame() {} override fun onRenderedFirstFrame() {}
}) })
val uri = Uri.fromFile(File(medium.path)) val isContentUri = medium.path.startsWith("content://")
val uri = if (isContentUri) Uri.parse(medium.path) else Uri.fromFile(File(medium.path))
val dataSpec = DataSpec(uri) val dataSpec = DataSpec(uri)
val fileDataSource = FileDataSource() val fileDataSource = if (isContentUri) ContentDataSource(context) else FileDataSource()
try { try {
fileDataSource.open(dataSpec) fileDataSource.open(dataSpec)
} catch (e: Exception) { } catch (e: Exception) {