1

I have a computer project where I am using tkinter to make a GUI application. The user has the option to either switch on or switch off the music through radiobuttons in the window. I made the below code so that you guys can replicate it and try it for yourself.

import tkinter as tk
import winsound as ws
import sys

root = tk.Tk()                         # Main window
root.geometry("200x200")
myColor = '#40E0D0'                 # Its a light blue color
root.configure(bg=myColor)          # Setting color of main window to myColor


def musicplayer(music_onoff):
    if sys.platform == "win32":
        if music_onoff == True:
            ws.PlaySound('8-bit.wav', ws.SND_FILENAME |
                         ws.SND_ASYNC | ws.SND_LOOP)
        else:
            ws.PlaySound(None, ws.SND_ASYNC)
    else:
        popup.showwarning('Warning', "Only supported on Windows devices")
        

# Linking style with the button
rb1 = tk.Radiobutton(text="Off")
rb2= tk.Radiobutton(text="On")

rb1.configure(command=lambda x=False: musicplayer(x))
rb2.configure(command=lambda x=True: musicplayer(x))

rb1.pack()                          # Placing Radiobutton
rb2.pack()
root.mainloop()                    

The 8-bit.wav file corresponds to this video which is a 8-bit version of Never Gonna Give You Up. I converted the video to a .wav format. The full song plays when I play the .wav file on my windows default mp3 player(which is groove music), but not when I use winsound. I am not sure why this is happening because no error shows up on my console as well, when the music stops.

  • UPDATE: Realised that the issue was caused by winsound because it is a 8-bit sound file. Converting it into a 16-bit signed wav file using an online converter like convertio.co and then using it resolves the problem. – Veeraja Veeraesh Dec 31 '21 at 12:19
  • You can delete the question then – Delrius Euphoria Dec 31 '21 at 12:51
  • 1
    @CoolCloud or post an answer because it may help someone in the future – Matiiss Dec 31 '21 at 14:21
  • @Matiiss & Cool_Cloud, I've posted it as an answer, so that other people using different formats can understand what they need to do. Matiiss thanks for the suggestion – Veeraja Veeraesh Dec 31 '21 at 14:25
  • @Matiiss The problem was in the way OP downloaded the wav file from the internet. I downloaded the song and run it off without any _conversions_ and it worked just fine. So then the problem changes to the fact that the file downloaded was unsupported with `winsound` prolly, there are plenty of other question that ask the same stuff then – Delrius Euphoria Dec 31 '21 at 16:42
  • @CoolCloud still this could resolve issues people may face later, for example if they have an 8-bit file for other reasons or sth else, they will know that a possible solution may be to use a 16-bit file, also the questions vary in detail, someone may find the solution because of how the question is formulated, there may be a duplicate but it may be formulated differently and so this particular question may help someone find the solution – Matiiss Dec 31 '21 at 17:09
  • @Matiiss I don't agree because I do not understand what a 8-bit file is, AFAIK, the video is an 8-bit animation, I do not understand how that changes anything though. Do let me know if I missed out on something though – Delrius Euphoria Dec 31 '21 at 17:40
  • 1
    @CoolCloud I am just saying that someone may have this same issue in the future and this will be the question that helps them (perhaps because they will find it by formulating their question the same as it is formulated here), they will see the answer (your comment under that answer) and that will solve their problem, that's all – Matiiss Dec 31 '21 at 17:45

1 Answers1

1

UPDATE:
Realised that the issue was caused by winsound because it is a 8-bit sound file. Converting it into a 16-bit signed wav file using an online converter like convertio.co and then using it resolves the problem.

  • 1
    I was able to download the video as wav using [this](https://loader.to/en44/youtube-wav-converter.html) and no further conversion and the code worked fine – Delrius Euphoria Dec 31 '21 at 16:43
  • I dont know how conversions work, but I downloaded mine as mp3 first, had a few issues with it, then converted the mp3 into .wav. So maybe that's why I had issues. – Veeraja Veeraesh Jan 08 '22 at 16:36
  • 1
    Okay but don't use terminology such as 8 bit wav when you are not sure, it confuses the meaning entirely – Delrius Euphoria Jan 08 '22 at 16:41