简体   繁体   English

当 GUI 使用 tkinter 运行时,在 python 的背景中播放声音

[英]Playing sound in background in python while GUI is running with tkinter

I have been trying to run sound in background while App is running.我一直在尝试在应用程序运行时在后台运行声音。 I am Arch Linux and Python 3.8 I have tried playsound as like this我是 Arch Linux 和 Python 3.8 我试过 playsound 像这样

playsound('music.mp3', False)

but I get error saying system not supported.但我收到错误说系统不支持。 I have also tried pygame following way:我也尝试过 pygame 以下方式:

from pygame import mixer
mixer.init()
mixer.music.load("music.mp3")
mixer.music.play()

But i get error pygame.error: Unrecognized audio format .但我收到错误pygame.error: Unrecognized audio format Is there anyother way I can run music in background some task is executing on GUI with tkinter.有没有其他方法可以在后台运行音乐一些任务正在使用 tkinter 在 GUI 上执行。 The program will run on arch and Ubuntu.该程序将在 arch 和 Ubuntu 上运行。

Try this code it will run the background music continuously till the app is running试试这个代码,它将持续运行背景音乐,直到应用程序运行

mixer.music.play(-1)

Try this:尝试这个:

mixer.music.play(-1)

And if you want your background music to be stopped in 10s then add this too:如果你想让你的背景音乐在 10 秒内停止,那么也添加这个:

gui_name.after(10000,  mixer.music.stop)

Hope it will help you.希望它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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