Search code examples
androidmedia-player

How do you access "elapsed time" of currently playing Android song?


I'm using the code from this SO question in order to get track title, artist, and album for the currently playing song in the Android music player (or Play Music).

That is, I'd like to be able to access how far into the current song you are.

I'm looking at this bit from the aforementioned SO question:

String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");

It seems to me that I should be able to access other metrics in a similar way, but I can't find a way to look at what String values are valid keys for the intent.getStringExtra() method.

I'm a noob to the android development scene, so forgive me my beginnerness! Am I on the right track?

[EDIT]: I was able to find the Intent when debugging; are these values being stored in that mExtras Hashmap? If so, I can see that such values as duration are available, though elapsed time is not.


Solution

  • This should be the solution:

    Bundle extras = intent.getExtras();
    
    Long elapsedtime = extras.getLong("position");
    
    Long duration = extras.getLong("duration");