Skip to content

Commit 8f334fb

Browse files
committed
fix set volume error
1 parent 313d428 commit 8f334fb

File tree

9 files changed

+116
-56
lines changed

9 files changed

+116
-56
lines changed

demo/Android/app/src/main/java/com/slark/demo/ui/component/CustomSlider.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import androidx.compose.ui.Modifier
99
import androidx.compose.ui.geometry.CornerRadius
1010
import androidx.compose.ui.geometry.Offset
1111
import androidx.compose.foundation.Canvas
12+
import androidx.compose.runtime.getValue
13+
import androidx.compose.runtime.mutableStateOf
14+
import androidx.compose.runtime.remember
15+
import androidx.compose.runtime.setValue
1216
import androidx.compose.ui.geometry.Size
1317
import androidx.compose.ui.graphics.Color
1418
import androidx.compose.ui.input.pointer.pointerInput
@@ -33,6 +37,7 @@ fun CustomSlider(
3337
) {
3438
val thumbRadiusPx = with(LocalDensity.current) { thumbRadius.toPx() }
3539
val trackHeightPx = with(LocalDensity.current) { trackHeight.toPx() }
40+
var newValue by remember { mutableStateOf(value) }
3641

3742
BoxWithConstraints(
3843
modifier = modifier
@@ -41,7 +46,7 @@ fun CustomSlider(
4146
detectTapGestures { offset ->
4247
val width = size.width - thumbRadiusPx * 2
4348
val fraction = ((offset.x - thumbRadiusPx) / width).coerceIn(0f, 1f)
44-
val newValue = lerp(valueRange.start, valueRange.endInclusive, fraction)
49+
newValue = lerp(valueRange.start, valueRange.endInclusive, fraction)
4550
onValueChange(newValue, true)
4651
}
4752
}
@@ -54,11 +59,11 @@ fun CustomSlider(
5459
change.consume()
5560
val width = size.width - thumbRadiusPx * 2
5661
val fraction = ((change.position.x - thumbRadiusPx) / width).coerceIn(0f, 1f)
57-
val newValue = lerp(valueRange.start, valueRange.endInclusive, fraction)
62+
newValue = lerp(valueRange.start, valueRange.endInclusive, fraction)
5863
onValueChange(newValue, false)
5964
},
6065
onDragEnd = {
61-
onValueChange(value, true)
66+
onValueChange(newValue, true)
6267
}
6368
)
6469
}

demo/Android/app/src/main/java/com/slark/demo/ui/component/PlayerControls.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ fun PlayerControls(
3333
viewModel: PlayerViewModel,
3434
modifier: Modifier = Modifier
3535
) {
36-
val isPlaying = viewModel.isPlaying
37-
val progress by remember {
38-
derivedStateOf {
39-
val current = viewModel.currentTime
40-
val total = viewModel.totalTime
41-
if (total > 0) current.toFloat() / total.toFloat() else 0f
42-
}
43-
}
44-
4536
Column(
4637
modifier = modifier.fillMaxWidth().padding(16.dp),
4738
horizontalAlignment = Alignment.CenterHorizontally,
@@ -94,10 +85,12 @@ fun PlayerControlBar(viewModel: PlayerViewModel) {
9485

9586
CustomSlider(
9687
value = viewModel.volume,
97-
onValueChange = { t, _ -> viewModel.volume = t },
98-
valueRange = 0f..1f,
88+
onValueChange = { t, _ ->
89+
viewModel.volume = t
90+
},
91+
valueRange = 0f..100f,
9992
trackColor = Color.LightGray,
100-
activeColor = Color(0xFFA9A9A9),
93+
activeColor = Color(0xFFE6E6E6),
10194
modifier = Modifier
10295
.constrainAs(volumeSlider) {
10396
start.linkTo(volumeIcon.end)

demo/Android/app/src/main/java/com/slark/demo/ui/component/PlayerScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fun PlayerScreen(
8787
if (controlsVisible) {
8888
hideJob?.cancel()
8989
hideJob = CoroutineScope(Dispatchers.Main).launch {
90-
delay(3000)
90+
delay(5000)
9191
controlsVisible = false
9292
}
9393
}

demo/Android/app/src/main/java/com/slark/demo/ui/model/PlayerViewModel.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ import com.slark.sdk.SlarkLog
1919

2020
class PlayerViewModel(private var player: SlarkPlayer?) : ViewModel() {
2121
var isPlaying by mutableStateOf(false)
22-
//private set
22+
2323

2424
var currentTime by mutableStateOf(0.0)
25-
// private set
2625

2726
var totalTime by mutableStateOf(0.0)
28-
//private set
2927

3028
var cacheTime by mutableStateOf(0.0)
31-
//private set
3229

3330
var isLoading by mutableStateOf(false)
3431

35-
var volume by mutableStateOf(100.0f)
32+
private var _volume = mutableStateOf(100.0f)
33+
var volume: Float
34+
get() = _volume.value
35+
set(value) {
36+
player?.volume = value
37+
_volume.value = value
38+
}
3639

3740
var isLoop by mutableStateOf(false)
3841

@@ -125,10 +128,6 @@ class PlayerViewModel(private var player: SlarkPlayer?) : ViewModel() {
125128
player?.setRenderSize(width, height)
126129
}
127130

128-
fun setRotation(rotation: Int) {
129-
player?.setRotation(rotation)
130-
}
131-
132131
fun loop(isLoop: Boolean) {
133132
player?.isLoop = isLoop
134133
this.isLoop = isLoop
@@ -163,12 +162,7 @@ class PlayerViewModel(private var player: SlarkPlayer?) : ViewModel() {
163162
player?.setRenderTarget(SlarkRenderTarget.FromSurface(surface, size.width.toInt(), size.height.toInt()))
164163
}
165164

166-
fun stop() {
167-
player?.stop()
168-
}
169-
170165
fun release() {
171-
player?.stop()
172166
player?.release()
173167
}
174168

src/core/PlayerImpl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ void Player::Impl::setLoop(
10971097
params_.withWriteLock([isLoop](auto& p){
10981098
p->setting.isLoop = isLoop;
10991099
});
1100+
ownerThread_->start();
11001101
}
11011102

11021103
void Player::Impl::setVolume(
@@ -1105,6 +1106,7 @@ void Player::Impl::setVolume(
11051106
auto ptr = buildEvent(EventType::UpdateSettingVolume);
11061107
ptr->data = std::make_any<float>(volume);
11071108
sender_->send(std::move(ptr));
1109+
ownerThread_->start();
11081110
}
11091111

11101112
void Player::Impl::setMute(
@@ -1113,6 +1115,7 @@ void Player::Impl::setMute(
11131115
auto ptr = buildEvent(EventType::UpdateSettingMute);
11141116
ptr->data = std::make_any<bool>(isMute);
11151117
sender_->send(std::move(ptr));
1118+
ownerThread_->start();
11161119
}
11171120

11181121
PlayerState Player::Impl::state() noexcept {

src/platform/Android/sdk/src/main/cpp/PlayerManager.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ enum class Action {
199199
Prepare = 0,
200200
Play,
201201
Pause,
202-
Stop,
203202
Release
204203
};
205204

@@ -292,10 +291,6 @@ Java_com_slark_sdk_SlarkPlayerManager_00024Companion_doAction(
292291
player->pause();
293292
}
294293
break;
295-
case Action::Stop: {
296-
player->stop();
297-
}
298-
break;
299294
case Action::Release: {
300295
player->stop();
301296
PlayerObserverManager::shareInstance().remove(playerId);

src/platform/Android/sdk/src/main/java/com/slark/api/SlarkPlayer.kt

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,125 @@
11
package com.slark.api
22

3-
import android.util.Size
43
import com.slark.sdk.SlarkPlayerImpl
54

65
/**
76
* SlarkPlayer interface that defines the methods for a media player.
87
*/
98

109
interface SlarkPlayer {
10+
/**
11+
* @param isLoop: value from true to false
12+
*/
1113
var isLoop: Boolean
1214

15+
/**
16+
* @param isMute: value from true to false
17+
*/
1318
var isMute: Boolean
1419

20+
/**
21+
* @param volume: value from 0.0 to 100.0
22+
*/
1523
var volume: Float
1624

25+
/**
26+
* Prepare the player with the given configuration.
27+
* This method should be called before any playback actions.
28+
*/
1729
fun prepare()
1830

19-
fun getPlayerId(): String
31+
/**
32+
* Get the player ID.
33+
* This ID is used to identify the player instance in the system.
34+
*/
35+
fun playerId(): String
2036

37+
/**
38+
* Start playback of the media.
39+
* This method should be called after prepare() to start playing the media.
40+
*/
2141
fun play()
2242

43+
/**
44+
* Pause the playback of the media.
45+
* This method can be called to pause the media playback.
46+
*/
2347
fun pause()
2448

25-
fun stop()
49+
/**
50+
* Stop the playback of the media.
51+
* This method stops the media playback and dispose media resources.
52+
*/
53+
fun release()
2654

2755
/**
2856
* @param time: seconds
2957
* @param isAccurate: Accurate seek is only performed when the user releases the progress slider.
3058
*/
3159
fun seekTo(time: Double, isAccurate: Boolean)
3260

33-
fun release()
34-
61+
/**
62+
* Set the observer for player events.
63+
* The observer will receive notifications about player state changes and events.
64+
*/
3565
fun setObserver(observer: SlarkPlayerObserver?)
3666

67+
/**
68+
* Set the render target for the player.
69+
* The render target is where the player will render the video frames.
70+
*/
3771
fun setRenderTarget(renderTarget: SlarkRenderTarget)
3872

73+
/**
74+
* Get the current state of the player.
75+
* This method returns the current state of the player, such as playing, paused, buffering, etc.
76+
*/
77+
fun state(): SlarkPlayerState
78+
79+
/**
80+
* Get the total duration of the media in seconds.
81+
* This method returns the total duration of the media being played by the player.
82+
*/
3983
fun totalDuration(): Double
4084

85+
/**
86+
* Get the current played time of the media in seconds.
87+
* This method returns the current played time of the media.
88+
*/
4189
fun currentTime(): Double
4290

91+
/**
92+
* Request a render update with the given texture ID and size.
93+
* This method is used to request a render update for the player.
94+
*/
4395
fun setRenderSize(width: Int, height: Int)
4496

45-
fun setRotation(rotation: Int) // 0, 90, 180, 270 degrees
97+
enum class Rotation {
98+
ROTATION_0, // 0 degrees
99+
ROTATION_90, // 90 degrees
100+
ROTATION_180, // 180 degrees
101+
ROTATION_270 // 270 degrees
102+
}
103+
/**
104+
* Set the rotation of the video.
105+
* The rotation can be 0, 90, 180, or 270 degrees.
106+
*/
107+
fun setRotation(rotation: Rotation)
46108

109+
/**
110+
* Handle background state changes.
111+
* This method is called when the player goes into the background or comes back to the foreground.
112+
* @param isBackground: true if the player is in the background, false if it is in the foreground.
113+
*/
47114
fun onBackground(isBackground: Boolean)
48115
}
49116

50117
object SlarkPlayerFactory {
118+
/**
119+
* Create a SlarkPlayer instance with the given configuration.
120+
* @param config: The configuration for the player.
121+
* @return A new instance of SlarkPlayer, or null if creation fails.
122+
*/
51123
fun createPlayer(config: SlarkPlayerConfig): SlarkPlayer? {
52124
val player = SlarkPlayerImpl.create(config)
53125
return player

src/platform/Android/sdk/src/main/java/com/slark/sdk/SlarkPlayerImpl.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.view.TextureView
99
import android.view.View
1010
import com.slark.api.KtTimeRange
1111
import com.slark.api.SlarkPlayer
12+
import com.slark.api.SlarkPlayer.Rotation
1213
import com.slark.api.SlarkPlayerConfig
1314
import com.slark.api.SlarkPlayerObserver
1415
import com.slark.api.SlarkPlayerState
@@ -18,6 +19,7 @@ import kotlinx.coroutines.Dispatchers
1819
import kotlinx.coroutines.delay
1920
import kotlinx.coroutines.launch
2021
import kotlin.math.ceil
22+
import kotlin.math.min
2123

2224

2325
class SlarkPlayerImpl(val config: SlarkPlayerConfig, private val playerId: String): SlarkPlayer {
@@ -41,11 +43,11 @@ class SlarkPlayerImpl(val config: SlarkPlayerConfig, private val playerId: Strin
4143

4244
override var volume: Float = 100.0f
4345
set(value) {
44-
field = value
45-
SlarkPlayerManager.setVolume(playerId, value)
46+
field = min(100.0f, maxOf(0.0f, value))
47+
SlarkPlayerManager.setVolume(playerId, field)
4648
}
4749

48-
override fun getPlayerId(): String {
50+
override fun playerId(): String {
4951
return playerId
5052
}
5153

@@ -61,10 +63,6 @@ class SlarkPlayerImpl(val config: SlarkPlayerConfig, private val playerId: Strin
6163
SlarkPlayerManager.doAction(playerId, SlarkPlayerManager.Action.PAUSE.ordinal)
6264
}
6365

64-
override fun stop() {
65-
SlarkPlayerManager.doAction(playerId, SlarkPlayerManager.Action.STOP.ordinal)
66-
}
67-
6866
override fun release() {
6967
SlarkPlayerManager.doAction(playerId, SlarkPlayerManager.Action.RELEASE.ordinal)
7068
SlarkPlayerManager.removePlayer(playerId)
@@ -98,8 +96,13 @@ class SlarkPlayerImpl(val config: SlarkPlayerConfig, private val playerId: Strin
9896
renderThread?.setRenderSize(width, height)
9997
}
10098

101-
override fun setRotation(rotation: Int) {
102-
renderThread?.setRotation(rotation)
99+
override fun setRotation(rotation: Rotation) {
100+
when (rotation) {
101+
Rotation.ROTATION_0 -> renderThread?.setRotation(0)
102+
Rotation.ROTATION_90 -> renderThread?.setRotation(90)
103+
Rotation.ROTATION_180 -> renderThread?.setRotation(180)
104+
Rotation.ROTATION_270 -> renderThread?.setRotation(270)
105+
}
103106
}
104107

105108
override fun onBackground(isBackground: Boolean) {
@@ -125,11 +128,7 @@ class SlarkPlayerImpl(val config: SlarkPlayerConfig, private val playerId: Strin
125128
return SlarkPlayerManager.currentPlayedTime(playerId)
126129
}
127130

128-
fun playerId(): String {
129-
return playerId
130-
}
131-
132-
fun state(): SlarkPlayerState {
131+
override fun state(): SlarkPlayerState {
133132
return SlarkPlayerManager.state(playerId)
134133
}
135134

src/platform/Android/sdk/src/main/java/com/slark/sdk/SlarkPlayerManager.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class SlarkPlayerManager {
99
PREPARE,
1010
PLAY,
1111
PAUSE,
12-
STOP,
1312
RELEASE
1413
}
1514

0 commit comments

Comments
 (0)