make volume change work

This commit is contained in:
Christian Schabesberger 2024-08-08 15:39:34 +02:00
parent fb28aea8f8
commit 1cbbee6b80
2 changed files with 45 additions and 27 deletions

View File

@ -21,10 +21,14 @@
package net.newpipe.newplayer.model package net.newpipe.newplayer.model
import android.app.Application import android.app.Application
import android.content.Context.AUDIO_SERVICE
import android.media.AudioManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
@ -40,6 +44,7 @@ import kotlinx.coroutines.launch
import net.newpipe.newplayer.utils.VideoSize import net.newpipe.newplayer.utils.VideoSize
import net.newpipe.newplayer.NewPlayer import net.newpipe.newplayer.NewPlayer
import net.newpipe.newplayer.ui.ContentScale import net.newpipe.newplayer.ui.ContentScale
import kotlin.math.max
val VIDEOPLAYER_UI_STATE = "video_player_ui_state" val VIDEOPLAYER_UI_STATE = "video_player_ui_state"
@ -58,8 +63,20 @@ class VideoPlayerViewModelImpl @Inject constructor(
private var uiVisibilityJob: Job? = null private var uiVisibilityJob: Job? = null
private var progressUpdaterJob: Job? = null private var progressUpdaterJob: Job? = null
private val audioManager =
getSystemService(application.applicationContext, AudioManager::class.java)!!
var callbackListeners: MutableList<VideoPlayerViewModel.Listener?> = ArrayList() var callbackListeners: MutableList<VideoPlayerViewModel.Listener?> = ArrayList()
init {
val soundVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat() /
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC).toFloat()
mutableUiState.update {
it.copy(soundVolume = soundVolume)
}
}
//interface //interface
override var newPlayer: NewPlayer? = null override var newPlayer: NewPlayer? = null
set(value) { set(value) {
@ -74,8 +91,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
override var minContentRatio: Float = 4F / 3F override var minContentRatio: Float = 4F / 3F
set(value) { set(value) {
if (value <= 0 || maxContentRatio < value) if (value <= 0 || maxContentRatio < value) Log.e(
Log.e(
TAG, TAG,
"Ignoring maxContentRatio: It must not be 0 or less and it may not be bigger then mmaxContentRatio. It was Set to: $value" "Ignoring maxContentRatio: It must not be 0 or less and it may not be bigger then mmaxContentRatio. It was Set to: $value"
) )
@ -88,8 +104,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
override var maxContentRatio: Float = 16F / 9F override var maxContentRatio: Float = 16F / 9F
set(value) { set(value) {
if (value <= 0 || value < minContentRatio) if (value <= 0 || value < minContentRatio) Log.e(
Log.e(
TAG, TAG,
"Ignoring maxContentRatio: It must not be 0 or less and it may not be smaller then minContentRatio. It was Set to: $value" "Ignoring maxContentRatio: It must not be 0 or less and it may not be smaller then minContentRatio. It was Set to: $value"
) )
@ -131,8 +146,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
it.copy(isLoading = isLoading) it.copy(isLoading = isLoading)
} }
Log.i( Log.i(
TAG, TAG, if (isLoading) "Player started loading" else "Player finished loading"
if (isLoading) "Player started loading" else "Player finished loading"
) )
} }
}) })
@ -146,8 +160,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
Log.d(TAG, "Update Content ratio: $ratio") Log.d(TAG, "Update Content ratio: $ratio")
mutableUiState.update { mutableUiState.update {
it.copy( it.copy(
contentRatio = currentContentRatio, contentRatio = currentContentRatio, embeddedUiRatio = getEmbeddedUiRatio()
embeddedUiRatio = getEmbeddedUiRatio()
) )
} }
} }
@ -296,7 +309,18 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
override fun volumeChange(changeRate: Float) { override fun volumeChange(changeRate: Float) {
TODO("Not yet implemented") val currentVolume = mutableUiState.value.soundVolume
val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC).toFloat()
// we multiply changeRate by 1.5 so your finger only has to swipe a portion of the whole
// screen in order to fully enable or disable the volume
val newVolume = (currentVolume + changeRate * 1.5f).coerceIn(0f, 1f)
audioManager.setStreamVolume(
AudioManager.STREAM_MUSIC, (newVolume * maxVolume).toInt(), 0
)
println("Blub: currentVolume: $currentVolume, changeRate: $changeRate, maxVolume: $maxVolume, newvolume: $newVolume")
mutableUiState.update {
it.copy(soundVolume = newVolume)
}
} }
override fun switchToEmbeddedView() { override fun switchToEmbeddedView() {
@ -317,13 +341,10 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
} }
private fun getEmbeddedUiRatio() = private fun getEmbeddedUiRatio() = internalPlayer?.let { player ->
internalPlayer?.let { player ->
val videoRatio = VideoSize.fromMedia3VideoSize(player.videoSize).getRatio() val videoRatio = VideoSize.fromMedia3VideoSize(player.videoSize).getRatio()
return (if (videoRatio.isNaN()) return (if (videoRatio.isNaN()) currentContentRatio
currentContentRatio else videoRatio).coerceIn(minContentRatio, maxContentRatio)
else
videoRatio).coerceIn(minContentRatio, maxContentRatio)
} ?: minContentRatio } ?: minContentRatio

View File

@ -66,10 +66,7 @@ fun VolumeCircle(
Log.e(TAG, "Volume fraction must be in ragne [0;1]. It was $volumeFraction") Log.e(TAG, "Volume fraction must be in ragne [0;1]. It was $volumeFraction")
} }
Box( Box(modifier) {
modifier
.shadow(elevation = 1.dp, shape = CircleShape)
.padding(2.dp)) {
Canvas(Modifier.size(CIRCLE_SIZE.dp)) { Canvas(Modifier.size(CIRCLE_SIZE.dp)) {
val arcSize = (CIRCLE_SIZE - LINE_STROKE_WIDTH).dp.toPx(); val arcSize = (CIRCLE_SIZE - LINE_STROKE_WIDTH).dp.toPx();
drawCircle(color = Color.Black.copy(alpha = 0.3f), radius = (CIRCLE_SIZE / 2).dp.toPx()) drawCircle(color = Color.Black.copy(alpha = 0.3f), radius = (CIRCLE_SIZE / 2).dp.toPx())