OK, so now we want to start messing around with time in our songs, so we want to get the total play
length time of a song.
We want to get the current time.
We want to know at any given time how long the song has played and stuff like that.
So we're going to start to do that in this video that's going to take us a few videos.
It's not all that complicated, but we're going to have to do a little bit of coding for this.
So the first thing we want to do is let's create a function.
To deal with time and I'm going to call this define play, underscore time, and right now we're just
going to pass.
Now we want to call this function any time we start to play a song so we can call this function from
our play function.
So let's come down to our play function and here at the bottom, let's just say get song time and we
could just call it like this, we call it the name of the function and then we put the parentheses.
So any time we play a song, it will grab the song, do its stuff, add it to the mixer, start playing
it, and then boom, call this play time function.
So what do we want to do in this play time function?
Well, first off, let's just grab whatever the current time is for our song.
So let's call this current underscore time and we can do this by calling pygame mixer dot music.
As always, dot get underscore PEOC that stands for position right now.
This is a function now is kind of interesting.
What is this returning.
Is it returning seconds, milliseconds, minutes.
What kind of time you type things.
Is this returning.
This is actually returning milliseconds.
So actually let's come down to the very bottom here and let's grab our my label thing and let's use
this again to output the current whatever onto the screen and and see what it see what it outputs.
So up to our play time function and let's go my label dot config and set the text equal to current underscore
time.
Now when this when we play a song, the song will start and then this will get called and it'll get
the current position which is basically zero or close to zero.
So this will put something close to zero.
We want this to refire.
We want this function to.
Sort of run itself every second so we can do that by, for instance, going my underscore label dot
after and saying after a thousand milliseconds, which is one second after one second, run this play
time function again.
So basically what this will do is every second this function will run again and a second will have gone
by.
So another second will be the current position of the song will be a second later and then this will
get output to that current time a second later.
So every second this label will change.
So let's go and save this and head back over here.
And run the sky and run this guy.
And let's add some songs you.
And play one, so a thousand, two thousand, three thousand, four thousand, so you remember, these
are milliseconds, so that's not all that useful.
So we need to convert this.
So let's head back over our code and see in our playtime thing, we can just take this number and divide
it by a thousand.
Now, this is still going to return a decimal, so it'll be one point zero four seconds, one point
oh eight seconds, two point zero seven seconds, whatever.
So we don't really want the decimals.
So instead, let's convert this to an integer.
And integers are just whole numbers.
Right.
So we could do this by wrapping this variable in an integer function.
Right.
So this was just rounded up or down or whatever and make it a whole number.
So one second, two seconds, three seconds, etc..
So, OK, let's go ahead and run this and see what this looks like.
Pull this over.
Add a bunch of songs.
Now, let's play one one second, two seconds, three seconds, four seconds, five seconds, pretty
so.
All right, we're starting to get somewhere here and close this, and that's useful, but it's still
not super useful.
We like time formats, like when you look at a watch, it's like or one 12 or two and thirty seconds
and thirty five seconds, etc., stuff like that.
So we can actually convert that number, that integer that we just saw on the label.
One second, two seconds, three seconds to a time format and we'll look at that in the next video.