Skip to content

Commit 2fb05fc

Browse files
Merge pull request InputUsername#11 from InputUsername/multiple-player-support
Improve finding active player and switching if necessary
2 parents 4168f5b + b8165dd commit 2fb05fc

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/mainloop.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,31 @@ fn get_min_play_time(track_length: Duration, config: &Config) -> Duration {
4040
})
4141
}
4242

43+
fn player_is_active(player: &Player) -> bool {
44+
if !player.is_running() {
45+
return false;
46+
}
47+
48+
match player.get_playback_status() {
49+
Ok(PlaybackStatus::Playing) => true,
50+
_ => false
51+
}
52+
}
53+
4354
fn wait_for_player(finder: &PlayerFinder) -> Player {
4455
loop {
45-
match finder.find_active() {
46-
Ok(player) => return player,
47-
Err(_) => {}
56+
let players = match finder.find_all() {
57+
Ok(players) => players,
58+
_ => {
59+
thread::sleep(INIT_WAIT_TIME);
60+
continue;
61+
}
62+
};
63+
64+
for player in players {
65+
if player_is_active(&player) {
66+
return player;
67+
}
4868
}
4969

5070
thread::sleep(INIT_WAIT_TIME);
@@ -74,7 +94,7 @@ pub fn run(config: &Config, scrobbler: &Scrobbler) {
7494
let mut scrobbled_current_song = false;
7595

7696
loop {
77-
if !player.is_running() {
97+
if !player_is_active(&player) {
7898
println!(
7999
"Player {} stopped, looking for a new MPRIS player...",
80100
player.identity()

0 commit comments

Comments
 (0)