Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit a1905bd0 authored by Matthijs Tijink's avatar Matthijs Tijink Committed by Nicolas Fella

Control the volume of Android media players

parent abcb6cbf
......@@ -30,7 +30,7 @@ import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
class MprisReceiverCallback extends MediaController.Callback {
private static final String TAG = "MprisReceiver";
......@@ -43,7 +43,6 @@ class MprisReceiverCallback extends MediaController.Callback {
this.plugin = plugin;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onPlaybackStateChanged(PlaybackState state) {
plugin.sendMetadata(player);
......@@ -54,4 +53,9 @@ class MprisReceiverCallback extends MediaController.Callback {
plugin.sendMetadata(player);
}
@Override
public void onAudioInfoChanged(MediaController.PlaybackInfo info) {
//Note: not called by all media players
plugin.sendMetadata(player);
}
}
......@@ -147,9 +147,18 @@ class MprisReceiverPlayer {
}
int getVolume() {
if (controller.getPlaybackInfo() == null)
return 0;
return 100 * controller.getPlaybackInfo().getCurrentVolume() / controller.getPlaybackInfo().getMaxVolume();
MediaController.PlaybackInfo info = controller.getPlaybackInfo();
if (info == null) return 0;
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() {
......
......@@ -134,6 +134,13 @@ public class MprisReceiverPlugin extends Plugin {
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")) {
String action = np.getString("action");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment