0

I've been making a music player program to practice using Tkinter for my NEA (Alevel computer science project) I have a shuffle function in the program but can't seem to find a way of pausing/skipping songs.
Example of code: (Obviously I have imported tkinter, random and winsound, also I have more songs)

def song1():
    print('song name - song artist')
    winsound.PlaySound(r"song address",winsound.SND_FILENAME)
def shuffle():
    ShuffleButton.grid_remove()
    playlist=list(range(1,NumberOfSongs+1))
    random.shuffle(playlist)
    print(playlist)
    i=int(0)
    while i<=NumberOfSongs:
        if playlist[i]==1:
            song1()
            i=i+1

Btw I know there is probably a much simpler way to make a music player but I can actually understand this way.

jpyams
  • 4,030
  • 9
  • 41
  • 66
  • What have you tried so far? My first thought is that you'd want to use the `SND_ASYNC` flag or else your program will halt until the sound is done playing. Then, as long as you're not using the `SND_NOSTOP` flag, playing another sound will stop the one that's currently playing. Or you can use the `SND_PURGE` flag to stop the sound before playing a new one, or to pause, just don't play a new one after. These are just guesses after reading the documentation - without knowing what you've tried so far, though, I have no idea if you've been down that path already or not. – Random Davis Dec 04 '18 at 19:26
  • Used ASYNC and it now lets me change song have an idea for how I'm going to do the skip pause and play thx x – Aidan Walton Dec 04 '18 at 20:08
  • Glad you were able to make some headway. If you get it totally working, it's probably worth self-answering this question with what worked so that future users can see how you fixed it. – Random Davis Dec 04 '18 at 20:33
  • When using ASYNC the shuffle function becomes obsolete as it will only play the last song in the list. One solution to this would be to have a time.sleep(LengthOfSong) in every if/elif but that seems way too inefficient – Aidan Walton Dec 04 '18 at 21:04

1 Answers1

1

Replace SND_FILENAME with SND_ASYNC this will allow you to change the song whilst one is playing.

When using SND_ASYNC the shuffle function needs to be amended so the song will play - this can be done by using time.sleep(LengthOfSongInSeconds) underneath winsound.PlaySound(r"SongAddress",SND_ASYNC) this will allow the song to play before the next (el)if playlist[i]==x: will play the next song in the list. I am aware that there will be a more efficient way of allowing the song to be played in full and I am working on the solution.

jpyams
  • 4,030
  • 9
  • 41
  • 66