1
00:00:01,840 --> 00:00:07,570
OK, so now we want to start messing around with time in our songs, so we want to get the total play

2
00:00:07,570 --> 00:00:09,040
length time of a song.

3
00:00:09,040 --> 00:00:10,360
We want to get the current time.

4
00:00:10,660 --> 00:00:14,770
We want to know at any given time how long the song has played and stuff like that.

5
00:00:14,770 --> 00:00:17,650
So we're going to start to do that in this video that's going to take us a few videos.

6
00:00:18,070 --> 00:00:21,540
It's not all that complicated, but we're going to have to do a little bit of coding for this.

7
00:00:21,550 --> 00:00:25,780
So the first thing we want to do is let's create a function.

8
00:00:27,530 --> 00:00:36,620
To deal with time and I'm going to call this define play, underscore time, and right now we're just

9
00:00:36,620 --> 00:00:37,310
going to pass.

10
00:00:37,520 --> 00:00:44,120
Now we want to call this function any time we start to play a song so we can call this function from

11
00:00:44,120 --> 00:00:45,080
our play function.

12
00:00:46,010 --> 00:00:52,370
So let's come down to our play function and here at the bottom, let's just say get song time and we

13
00:00:52,370 --> 00:00:56,000
could just call it like this, we call it the name of the function and then we put the parentheses.

14
00:00:56,210 --> 00:01:02,630
So any time we play a song, it will grab the song, do its stuff, add it to the mixer, start playing

15
00:01:02,630 --> 00:01:05,450
it, and then boom, call this play time function.

16
00:01:05,720 --> 00:01:08,150
So what do we want to do in this play time function?

17
00:01:08,330 --> 00:01:13,400
Well, first off, let's just grab whatever the current time is for our song.

18
00:01:13,410 --> 00:01:21,680
So let's call this current underscore time and we can do this by calling pygame mixer dot music.

19
00:01:21,680 --> 00:01:27,940
As always, dot get underscore PEOC that stands for position right now.

20
00:01:27,950 --> 00:01:30,220
This is a function now is kind of interesting.

21
00:01:30,230 --> 00:01:32,630
What is this returning.

22
00:01:32,960 --> 00:01:35,720
Is it returning seconds, milliseconds, minutes.

23
00:01:36,020 --> 00:01:38,460
What kind of time you type things.

24
00:01:38,660 --> 00:01:39,470
Is this returning.

25
00:01:39,660 --> 00:01:42,200
This is actually returning milliseconds.

26
00:01:42,380 --> 00:01:47,540
So actually let's come down to the very bottom here and let's grab our my label thing and let's use

27
00:01:47,540 --> 00:01:54,260
this again to output the current whatever onto the screen and and see what it see what it outputs.

28
00:01:54,290 --> 00:02:04,310
So up to our play time function and let's go my label dot config and set the text equal to current underscore

29
00:02:04,310 --> 00:02:04,860
time.

30
00:02:05,270 --> 00:02:12,500
Now when this when we play a song, the song will start and then this will get called and it'll get

31
00:02:12,500 --> 00:02:15,740
the current position which is basically zero or close to zero.

32
00:02:15,930 --> 00:02:18,550
So this will put something close to zero.

33
00:02:18,650 --> 00:02:20,990
We want this to refire.

34
00:02:21,020 --> 00:02:22,640
We want this function to.

35
00:02:23,670 --> 00:02:31,290
Sort of run itself every second so we can do that by, for instance, going my underscore label dot

36
00:02:31,290 --> 00:02:38,430
after and saying after a thousand milliseconds, which is one second after one second, run this play

37
00:02:38,430 --> 00:02:40,150
time function again.

38
00:02:40,530 --> 00:02:48,180
So basically what this will do is every second this function will run again and a second will have gone

39
00:02:48,180 --> 00:02:48,640
by.

40
00:02:48,660 --> 00:02:54,270
So another second will be the current position of the song will be a second later and then this will

41
00:02:54,270 --> 00:02:56,760
get output to that current time a second later.

42
00:02:56,880 --> 00:02:58,950
So every second this label will change.

43
00:02:58,980 --> 00:03:01,380
So let's go and save this and head back over here.

44
00:03:02,940 --> 00:03:05,880
And run the sky and run this guy.

45
00:03:06,860 --> 00:03:09,050
And let's add some songs you.

46
00:03:10,210 --> 00:03:17,860
And play one, so a thousand, two thousand, three thousand, four thousand, so you remember, these

47
00:03:17,860 --> 00:03:21,480
are milliseconds, so that's not all that useful.

48
00:03:22,030 --> 00:03:23,860
So we need to convert this.

49
00:03:23,860 --> 00:03:31,210
So let's head back over our code and see in our playtime thing, we can just take this number and divide

50
00:03:31,210 --> 00:03:31,990
it by a thousand.

51
00:03:32,860 --> 00:03:41,230
Now, this is still going to return a decimal, so it'll be one point zero four seconds, one point

52
00:03:41,290 --> 00:03:44,750
oh eight seconds, two point zero seven seconds, whatever.

53
00:03:44,950 --> 00:03:46,930
So we don't really want the decimals.

54
00:03:47,110 --> 00:03:49,750
So instead, let's convert this to an integer.

55
00:03:50,050 --> 00:03:52,030
And integers are just whole numbers.

56
00:03:52,030 --> 00:03:52,310
Right.

57
00:03:52,600 --> 00:03:56,790
So we could do this by wrapping this variable in an integer function.

58
00:03:56,850 --> 00:03:57,140
Right.

59
00:03:57,520 --> 00:04:01,680
So this was just rounded up or down or whatever and make it a whole number.

60
00:04:01,690 --> 00:04:04,390
So one second, two seconds, three seconds, etc..

61
00:04:04,660 --> 00:04:07,630
So, OK, let's go ahead and run this and see what this looks like.

62
00:04:08,020 --> 00:04:08,920
Pull this over.

63
00:04:09,130 --> 00:04:10,450
Add a bunch of songs.

64
00:04:12,240 --> 00:04:20,120
Now, let's play one one second, two seconds, three seconds, four seconds, five seconds, pretty

65
00:04:20,360 --> 00:04:20,750
so.

66
00:04:23,210 --> 00:04:29,990
All right, we're starting to get somewhere here and close this, and that's useful, but it's still

67
00:04:29,990 --> 00:04:31,100
not super useful.

68
00:04:31,130 --> 00:04:38,090
We like time formats, like when you look at a watch, it's like or one 12 or two and thirty seconds

69
00:04:38,090 --> 00:04:40,190
and thirty five seconds, etc., stuff like that.

70
00:04:40,190 --> 00:04:44,480
So we can actually convert that number, that integer that we just saw on the label.

71
00:04:44,480 --> 00:04:49,400
One second, two seconds, three seconds to a time format and we'll look at that in the next video.
