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

feat(android): add setBalance (#58) #1444

Merged
merged 1 commit into from
Mar 20, 2023
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
2 changes: 1 addition & 1 deletion feature_parity_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Note: LLM means Low Latency Mode.
<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>
<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>
<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>
<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>
<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>
<tr><td colspan="7"><strong>Streams</strong></td></tr>
<tr><td>duration event</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
<tr><td>position event</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class PlatformFeatures {

static const androidPlatformFeatures = PlatformFeatures(
hasRecordingActive: false,
hasBalance: false,
);

static const iosPlatformFeatures = PlatformFeatures(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ class AudioplayersPlugin : FlutterPlugin, IUpdateCallback {
}

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

"setPlaybackRate" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class MediaPlayerPlayer(
return mediaPlayer.isPlaying
}

override fun setVolume(volume: Float) {
mediaPlayer.setVolume(volume, volume)
override fun setVolume(leftVolume: Float, rightVolume: Float) {
mediaPlayer.setVolume(leftVolume, rightVolume)
}

override fun setRate(rate: Float) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Player {
fun seekTo(position: Int)
fun release()

fun setVolume(volume: Float)
fun setVolume(leftVolume: Float, rightVolume: Float)
fun setRate(rate: Float)
fun setLooping(looping: Boolean)
fun updateContext(context: AudioContextAndroid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class SoundPoolPlayer(
}
}

override fun setVolume(volume: Float) {
streamId?.let { soundPool.setVolume(it, volume, volume) }
override fun setVolume(leftVolume: Float, rightVolume: Float) {
streamId?.let { soundPool.setVolume(it, leftVolume, rightVolume) }
}

override fun setRate(rate: Float) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xyz.luan.audioplayers.player
import android.content.Context
import android.media.AudioManager
import android.media.MediaPlayer
import kotlin.math.min
import xyz.luan.audioplayers.AudioContextAndroid
import xyz.luan.audioplayers.AudioplayersPlugin
import xyz.luan.audioplayers.PlayerMode
Expand Down Expand Up @@ -44,7 +45,17 @@ class WrappedPlayer internal constructor(
if (field != value) {
field = value
if (!released) {
player?.setVolume(value)
player?.setVolumeAndBalance(value, balance)
}
}
}

var balance = 0.0f
set(value) {
if (field != value) {
field = value
if (!released) {
player?.setVolumeAndBalance(volume, value)
}
}
}
Expand Down Expand Up @@ -324,8 +335,14 @@ class WrappedPlayer internal constructor(

private fun Player.configAndPrepare() {
setRate(rate)
setVolume(volume)
setVolumeAndBalance(volume, balance)
setLooping(isLooping)
prepare()
}

private fun Player.setVolumeAndBalance(volume: Float, balance: Float) {
val leftVolume = min(1f, 1f - balance) * volume
val rightVolume = min(1f, 1f + balance) * volume
setVolume(leftVolume, rightVolume)
}
}