Showing the progression of the song

It would be nice to see the cursor moving when the song plays. Let's tackle this challenge right now.

We'll start by adding a method to our Player to get the duration of a song:

use std::time::Duration;

    pub fn compute_duration<P: AsRef<Path>>(path: P) ->      Option<Duration> {
        let file = File::open(path).unwrap();
        Mp3Decoder::compute_duration(BufReader::new(file))
    }

We simply call the compute_duration() method we created earlier. Next, we'll modify the Playlist to call this function. But before we do so, we'll modify the State type from the main module to include additional information:

use std::collections::HashMap;

struct State {
    current_time: u64,
    durations: HashMap<String, u64>,
    stopped: bool,
}

We added ...

Get Rust Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.