简体   繁体   中英

Music suddenly stops playing while using winsound in python script

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM