Fixed video player aspect ratio

This commit is contained in:
Sylvain Berfini 2024-07-22 11:12:36 +02:00
parent 9465593aa6
commit 4c66012372
4 changed files with 93 additions and 2 deletions

View file

@ -86,6 +86,15 @@ class MediaViewerFragment : GenericMainFragment() {
val fullScreenMode = viewModel.toggleFullScreen()
sharedViewModel.mediaViewerFullScreenMode.value = fullScreenMode
}
viewModel.videoSizeChangedEvent.observe(viewLifecycleOwner) {
it.consume { pair ->
val width = pair.first
val height = pair.second
Log.i("$TAG Updating video texture ration to ${width}x$height")
binding.videoPlayer.setAspectRatio(width, height)
}
}
}
override fun onResume() {

View file

@ -0,0 +1,74 @@
/*
* Copyright (c) 2010-2024 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.ui.file_viewer.view
import android.content.Context
import android.util.AttributeSet
import android.view.TextureView
class RatioTextureView : TextureView {
companion object {
private const val TAG = "[Ratio TextureView]"
}
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr,
defStyleRes
)
private var ratioWidth = 0
private var rationHeight = 0
fun setAspectRatio(width: Int, height: Int) {
if (width < 0 || height < 0) return
ratioWidth = width
rationHeight = height
requestLayout()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val width = MeasureSpec.getSize(widthMeasureSpec)
val height = MeasureSpec.getSize(heightMeasureSpec)
if (ratioWidth == 0 || rationHeight == 0) {
setMeasuredDimension(width, height)
} else {
if (width < height * ratioWidth / rationHeight) {
setMeasuredDimension(width, width * rationHeight / ratioWidth)
} else {
setMeasuredDimension(height * ratioWidth / rationHeight, height)
}
}
}
}

View file

@ -31,6 +31,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.linphone.core.tools.Log
import org.linphone.ui.GenericViewModel
import org.linphone.utils.Event
import org.linphone.utils.FileUtils
import org.linphone.utils.TimestampUtils
@ -59,6 +60,10 @@ class MediaViewModel @UiThread constructor() : GenericViewModel() {
val position = MutableLiveData<Int>()
val videoSizeChangedEvent: MutableLiveData<Event<Pair<Int, Int>>> by lazy {
MutableLiveData<Event<Pair<Int, Int>>>()
}
lateinit var mediaPlayer: MediaPlayer
private lateinit var filePath: String
@ -168,6 +173,9 @@ class MediaViewModel @UiThread constructor() : GenericViewModel() {
// Leave full screen when playback is done
fullScreenMode.postValue(false)
}
setOnVideoSizeChangedListener { mediaPlayer, width, height ->
videoSizeChangedEvent.postValue(Event(Pair(width, height)))
}
prepare()
}

View file

@ -25,9 +25,9 @@
android:visibility="@{viewModel.fullScreenMode ? View.GONE : viewModel.isAudio || viewModel.isVideo ? View.VISIBLE : View.GONE}"
app:constraint_referenced_ids="play_pause_audio_playback, progress, duration" />
<TextureView
<org.linphone.ui.file_viewer.view.RatioTextureView
android:id="@+id/video_player"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{viewModel.isVideo ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintTop_toTopOf="parent"