Skip to content

Commit 3b5de50

Browse files
authored
feat(android): add setBalance (#58) (#1444)
1 parent a6f89be commit 3b5de50

7 files changed

Lines changed: 27 additions & 12 deletions

File tree

feature_parity_table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Note: LLM means Low Latency Mode.
5959
<tr><td>stay awake</td><td>yes (except LLM)</td><td>yes</td><td>no</td><td>no</td><td>no</td><td>no</td></tr>
6060
<tr><td>recording active</td><td>not yet</td><td>yes</td><td>no</td><td>no</td><td>no</td><td>no</td></tr>
6161
<tr><td>playing route</td><td>yes (except LLM)</td><td>yes</td><td>no</td><td>no</td><td>no</td><td>no</td></tr>
62-
<tr><td>balance</td><td>not yet</td><td>not yet</td><td>not yet</td><td>yes</td><td>yes</td><td>yes</td></tr>
62+
<tr><td>balance</td><td>yes</td><td>not yet</td><td>not yet</td><td>yes</td><td>yes</td><td>yes</td></tr>
6363
<tr><td colspan="7"><strong>Streams</strong></td></tr>
6464
<tr><td>duration event</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
6565
<tr><td>position event</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>

packages/audioplayers/example/integration_test/platform_features.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class PlatformFeatures {
2020

2121
static const androidPlatformFeatures = PlatformFeatures(
2222
hasRecordingActive: false,
23-
hasBalance: false,
2423
);
2524

2625
static const iosPlatformFeatures = PlatformFeatures(

packages/audioplayers_android/android/src/main/kotlin/xyz/luan/audioplayers/AudioplayersPlugin.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ class AudioplayersPlugin : FlutterPlugin, IUpdateCallback {
124124
}
125125

126126
"setBalance" -> {
127-
Logger.error("setBalance is not currently implemented on Android")
128-
response.notImplemented()
129-
return
127+
val balance = call.argument<Double>("balance") ?: error("balance is required")
128+
player.balance = balance.toFloat()
130129
}
131130

132131
"setPlaybackRate" -> {

packages/audioplayers_android/android/src/main/kotlin/xyz/luan/audioplayers/player/MediaPlayerPlayer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class MediaPlayerPlayer(
3636
return mediaPlayer.isPlaying
3737
}
3838

39-
override fun setVolume(volume: Float) {
40-
mediaPlayer.setVolume(volume, volume)
39+
override fun setVolume(leftVolume: Float, rightVolume: Float) {
40+
mediaPlayer.setVolume(leftVolume, rightVolume)
4141
}
4242

4343
override fun setRate(rate: Float) {

packages/audioplayers_android/android/src/main/kotlin/xyz/luan/audioplayers/player/Player.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface Player {
1515
fun seekTo(position: Int)
1616
fun release()
1717

18-
fun setVolume(volume: Float)
18+
fun setVolume(leftVolume: Float, rightVolume: Float)
1919
fun setRate(rate: Float)
2020
fun setLooping(looping: Boolean)
2121
fun updateContext(context: AudioContextAndroid)

packages/audioplayers_android/android/src/main/kotlin/xyz/luan/audioplayers/player/SoundPoolPlayer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ class SoundPoolPlayer(
124124
}
125125
}
126126

127-
override fun setVolume(volume: Float) {
128-
streamId?.let { soundPool.setVolume(it, volume, volume) }
127+
override fun setVolume(leftVolume: Float, rightVolume: Float) {
128+
streamId?.let { soundPool.setVolume(it, leftVolume, rightVolume) }
129129
}
130130

131131
override fun setRate(rate: Float) {

packages/audioplayers_android/android/src/main/kotlin/xyz/luan/audioplayers/player/WrappedPlayer.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package xyz.luan.audioplayers.player
33
import android.content.Context
44
import android.media.AudioManager
55
import android.media.MediaPlayer
6+
import kotlin.math.min
67
import xyz.luan.audioplayers.AudioContextAndroid
78
import xyz.luan.audioplayers.AudioplayersPlugin
89
import xyz.luan.audioplayers.PlayerMode
@@ -44,7 +45,17 @@ class WrappedPlayer internal constructor(
4445
if (field != value) {
4546
field = value
4647
if (!released) {
47-
player?.setVolume(value)
48+
player?.setVolumeAndBalance(value, balance)
49+
}
50+
}
51+
}
52+
53+
var balance = 0.0f
54+
set(value) {
55+
if (field != value) {
56+
field = value
57+
if (!released) {
58+
player?.setVolumeAndBalance(volume, value)
4859
}
4960
}
5061
}
@@ -324,8 +335,14 @@ class WrappedPlayer internal constructor(
324335

325336
private fun Player.configAndPrepare() {
326337
setRate(rate)
327-
setVolume(volume)
338+
setVolumeAndBalance(volume, balance)
328339
setLooping(isLooping)
329340
prepare()
330341
}
342+
343+
private fun Player.setVolumeAndBalance(volume: Float, balance: Float) {
344+
val leftVolume = min(1f, 1f - balance) * volume
345+
val rightVolume = min(1f, 1f + balance) * volume
346+
setVolume(leftVolume, rightVolume)
347+
}
331348
}

0 commit comments

Comments
 (0)