简体   繁体   English

停止 Winsound/停止 Python 上的线程

[英]Stop Winsound / Stop a thread on Python

Im writing a litle game on python using Tkinter (And by the way, i am not allowed to use any other non built in modules) and I want to play a background song when on the main window, wich is the one that holds the title, and the buttons to go to other windows and stuff...我正在使用 Tkinter 在 python 上编写一个小游戏(顺便说一句,我不允许使用任何其他非内置模块),我想在主窗口上播放背景歌曲,这是拥有标题的歌曲,以及转到其他窗口和其他东西的按钮...

So the thing is that I need that sound to stop when I enter to another window, but when I hit the button to go, to another window, the song keeps playing...所以问题是当我进入另一个窗口时我需要声音停止,但是当我点击按钮去另一个窗口时,这首歌继续播放......

I'm using the winsound module, and had define a couple of functions, (That by the way, are wicked as hell) to play the song when I first run the program using threads...我正在使用 winsound 模块,并定义了几个函数,(顺便说一句,这是邪恶的)在我第一次使用线程运行程序时播放这首歌......

So here is the deal, I want something to put in the function "killsound" so that I can add it to every button, and then when I press any button to open any other window, the sound will be killed.所以这就是交易,我想在“killsound”功能中加入一些东西,以便我可以将它添加到每个按钮,然后当我按下任何按钮打开任何其他窗口时,声音将被杀死。

I was hoping something like 'a.kill()' or 'a.stop()' but it didnt worked.我希望像 'a.kill()' 或 'a.stop()' 之类的东西,但它没有奏效。

And I really don´t know how to use the SND_PURGE thing on winsound... Although I understand that SND_PURGE is no longer working on new windows OS (Got Win8.1)而且我真的不知道如何在 winsound 上使用 SND_PURGE 东西......虽然我知道 SND_PURGE 不再适用于新的 Windows 操作系统(获得 Win8.1)

Could you please help me?请你帮助我好吗?

Thank You!谢谢你! (And Sorry for the creepy english...) (对不起,令人毛骨悚然的英语......)

def Play(nombre): #This function Is the core of the winsound function
   ruta = os.path.join('Audio',nombre)
   Reproducir= winsound.PlaySound(ruta,winsound.SND_FILENAME)
   return Reproducir

def PlayThis(): 
   while flag_play:
       try:
           return Play('prettiest weed.wav')
       except:
           return "Error"

def PlayThisThread():
   global flag_play
   flag_play= True
   a=Thread(target=PlayThis, args=())
   a.daemon = True
   a.start()

   PlayThisThread()


def killsound():  #This is the function I want, for killing sound.
   global flag_play
   flag_play = False

There are 2 major problems in your code:您的代码中有两个主要问题:

  1. global variable flag_play has to be placed inside the sound playback loop, in your case within the PlayThis() function全局变量flag_play必须放在声音播放循环内,在你的情况下在PlayThis()函数内
  2. the Winsound module is aiming simple non-threaded usage. Winsound模块的目标是简单的非线程使用。 while the sound is playbacked, there is no chance to "softly" interrupt.在播放声音时,没有机会“轻声”打断。 It does not support any playback status reporting eg .isPlaying() nor any kind of .stop() that you need in order to kill it.它不支持任何播放状态报告,例如.isPlaying()也不支持任何类型的.stop()来杀死它。

Solution:解决方案:

  • try PyMedia package.试试PyMedia包。 Pymedia allows lower-level audio manipulation therefore more details have to be provided at the initialisation: Pymedia 允许较低级别的音频操作,因此必须在初始化时提供更多细节:

     import time, wave, pymedia.audio.sound as sound # little to do on the proper audio setup f= wave.open( 'prettiest weed.wav', 'rb' ) sampleRate= f.getframerate() # reads framerate from the file channels= f.getnchannels() format= sound.AFMT_S16_LE # this sets the audio format to most common WAV with 16-bit codec PCM Linear Little Endian, use either pymedia or any external utils such as FFMPEG to check / corvert audio into a proper format. audioBuffer = 300000 # you are able to control how much audio data is read

with the following assigment the "snd" becomes an instance of the class sound.Output and gives you bunch of useful audio methods:通过以下分配,“snd”成为sound.Output类的一个实例,并为您提供一堆有用的音频方法:

    snd= sound.Output( sampleRate, channels, format )
    s= f.readframes( audioBuffer )
    snd.play( s )

and finally your threaded playback loop might look as follows:最后,您的线程播放循环可能如下所示:

    while snd.isPlaying():
      global flag_play
      if not flag_play: snd.stop()  #here is where the playback gets interupted.
      time.sleep( 0.05 )

    f.close()

Please, let me know if you need more support on this.如果您需要更多支持,请告诉我。

我还在 Adob​​e Audition 中创建了一个包含静音的 0.5 秒 wavfile 并将其附加到停止按钮,这基本上“停止”了先前播放的音频剪辑的播放。

我找到了一种方法,通过向按钮添加 0.5 秒的声音,这样当我按下按钮时,它会停止背景播放按钮一,然后停止程序中的所有声音。

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

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