2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 13:17:43 +00:00

Control the volume of Android media players

This commit is contained in:
Matthijs Tijink 2019-07-19 20:31:52 +00:00 committed by Nicolas Fella
parent abcb6cbf33
commit a1905bd031
3 changed files with 25 additions and 5 deletions

View File

@ -30,7 +30,7 @@ import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
class MprisReceiverCallback extends MediaController.Callback { class MprisReceiverCallback extends MediaController.Callback {
private static final String TAG = "MprisReceiver"; private static final String TAG = "MprisReceiver";
@ -43,7 +43,6 @@ class MprisReceiverCallback extends MediaController.Callback {
this.plugin = plugin; this.plugin = plugin;
} }
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override @Override
public void onPlaybackStateChanged(PlaybackState state) { public void onPlaybackStateChanged(PlaybackState state) {
plugin.sendMetadata(player); plugin.sendMetadata(player);
@ -54,4 +53,9 @@ class MprisReceiverCallback extends MediaController.Callback {
plugin.sendMetadata(player); plugin.sendMetadata(player);
} }
@Override
public void onAudioInfoChanged(MediaController.PlaybackInfo info) {
//Note: not called by all media players
plugin.sendMetadata(player);
}
} }

View File

@ -147,9 +147,18 @@ class MprisReceiverPlayer {
} }
int getVolume() { int getVolume() {
if (controller.getPlaybackInfo() == null) MediaController.PlaybackInfo info = controller.getPlaybackInfo();
return 0; if (info == null) return 0;
return 100 * controller.getPlaybackInfo().getCurrentVolume() / controller.getPlaybackInfo().getMaxVolume(); return 100 * info.getCurrentVolume() / info.getMaxVolume();
}
void setVolume(int volume) {
MediaController.PlaybackInfo info = controller.getPlaybackInfo();
if (info == null) return;
//Use rounding for the volume, since most devices don't have a very large range
double unroundedVolume = info.getMaxVolume() * volume / 100.0 + 0.5;
controller.setVolumeTo((int) unroundedVolume, 0);
} }
long getPosition() { long getPosition() {

View File

@ -134,6 +134,13 @@ public class MprisReceiverPlugin extends Plugin {
player.setPosition(position); player.setPosition(position);
} }
if (np.has("setVolume")) {
int volume = np.getInt("setVolume", 100);
player.setVolume(volume);
//Setting volume doesn't seem to always trigger the callback
sendMetadata(player);
}
if (np.has("action")) { if (np.has("action")) {
String action = np.getString("action"); String action = np.getString("action");