Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: No video when network video is played and crash when open local video is played #1197

Merged
merged 3 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.database.Cursor
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
Expand Down Expand Up @@ -93,6 +92,9 @@ fun Context.getPath(uri: Uri): String? {
}
}
} else if (ContentResolver.SCHEME_CONTENT.equals(uri.scheme, ignoreCase = true)) {
if (uri.isLocalPhotoPickerUri) return null
if (uri.isCloudPhotoPickerUri) return null

return if (uri.isGooglePhotosUri) {
uri.lastPathSegment
} else {
Expand All @@ -116,19 +118,17 @@ private fun Context.getDataColumn(
selection: String? = null,
selectionArgs: Array<String>? = null,
): String? {
var cursor: Cursor? = null
val column = MediaStore.Images.Media.DATA
val projection = arrayOf(column)
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor != null && cursor.moveToFirst()) {
val index = cursor.getColumnIndexOrThrow(column)
return cursor.getString(index)
contentResolver.query(uri, projection, selection, selectionArgs, null)?.use { cursor ->
if (cursor.moveToFirst()) {
val index = cursor.getColumnIndexOrThrow(column)
return cursor.getString(index)
}
}
} catch (e: Exception) {
return null
} finally {
cursor?.close()
}
return null
}
Expand Down Expand Up @@ -172,26 +172,24 @@ fun Context.getFilenameFromContentUri(uri: Uri): String? {
fun Context.getMediaContentUri(uri: Uri): Uri? {
val path = getPath(uri) ?: return null

var cursor: Cursor? = null
val column = MediaStore.Video.Media._ID
val projection = arrayOf(column)
try {
cursor = contentResolver.query(
contentResolver.query(
VIDEO_COLLECTION_URI,
projection,
"${MediaStore.Images.Media.DATA} = ?",
arrayOf(path),
null,
)
if (cursor != null && cursor.moveToFirst()) {
val index = cursor.getColumnIndexOrThrow(column)
val id = cursor.getLong(index)
return ContentUris.withAppendedId(VIDEO_COLLECTION_URI, id)
)?.use { cursor ->
if (cursor.moveToFirst()) {
val index = cursor.getColumnIndexOrThrow(column)
val id = cursor.getLong(index)
return ContentUris.withAppendedId(VIDEO_COLLECTION_URI, id)
}
}
} catch (e: Exception) {
return null
} finally {
cursor?.close()
}
return null
}
Expand Down Expand Up @@ -275,7 +273,14 @@ private fun detectCharset(url: URL): Charset {

private fun detectCharsetFromStream(inputStream: InputStream): Charset {
return BufferedInputStream(inputStream).use { bufferedStream ->
val data = bufferedStream.readBytes()
val maxBytes = 1024 * 100 // 100 KB
val data = ByteArray(maxBytes)
val bytesRead = bufferedStream.read(data, 0, maxBytes)

if (bytesRead <= 0) {
return@use Charset.forName(StandardCharsets.UTF_8.name())
}

UniversalDetector(null).run {
handleData(data, 0, data.size)
dataEnd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ suspend fun File.getSubtitles(): List<File> = withContext(Dispatchers.IO) {

subtitleExtensions.mapNotNull { extension ->
val file = File(parentDir, "$mediaName.$extension")
file.takeIf { it.exists() }
file.takeIf { it.exists() && it.isFile }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ val Uri.isMediaDocument: Boolean
*/
val Uri.isGooglePhotosUri: Boolean
get() = "com.google.android.apps.photos.content" == authority

/**
* Whether the Uri authority is PhotoPicker.
*/
val Uri.isLocalPhotoPickerUri: Boolean
get() = toString().contains("com.android.providers.media.photopicker")

/**
* Whether the Uri authority is PhotoPicker.
*/
val Uri.isCloudPhotoPickerUri: Boolean
get() = toString().contains("com.google.android.apps.photos.cloudpicker")
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class PlayerActivity : AppCompatActivity() {
setOrientation()
applyVideoZoom(videoZoom = playerPreferences.playerVideoZoom, showInfo = false)
mediaController?.currentMediaItem?.mediaId?.let {
applyVideoScale(videoScale = viewModel.getVideoState(it)?.videoScale ?: 0f)
applyVideoScale(videoScale = viewModel.getVideoState(it)?.videoScale ?: 1f)
}

mediaController?.run {
Expand Down
Loading