Reputation: 283
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.
Upvotes: 1
Views: 198
Reputation: 283
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.
Upvotes: 1