我试图制作一个简单的音频播放器,具有播放,停止,暂停和恢复歌曲的能力。
我想做的是
什么不起作用
当
什么起作用?
不使用threading/multiprocessing.的
我的代码
from ctypes import windll
import threading
import time
class WinPlayAudio():
def __init__(self):
self.alias = "A{}".format(id(self))
def _MCI_command(self,command):
windll.winmm.mciSendStringW(command,0,0,0) # If printed, this returns 263 == unrecognized audio device
# This does not play anything even tho wait is turned to: True
def _play(self, start=0, wait = False):
th = threading.Thread(target=self._MCI_command, args=(f'play {self.alias} from {start} {"wait" if wait else ""}',))
th.start()
def _open_song(self, audio_path):
self._MCI_command(f'open {audio_path} alias {self.alias}')
def _set_volume(self):
self._MCI_command(f'setaudio {self.alias} volume to 500')
def _pause(self):
self._MCI_command(f'pause {self.alias}')
def _resume(self):
self._MCI_command(f'resume {self.alias}')
def _stop(self):
self._MCI_command(f'stop {self.alias}')
if __name__ == '__main__':
p = WinPlayAudio()
p._open_song(r"D:\songs\bee.mp3")
p._play(0, True)发布于 2021-08-17 23:38:05
固定代码,如果您想要多线程,请远离别名
import threading
from ctypes import windll
class WinPlayAudio():
def __init__(self):
self.alias = "A{}".format(id(self))
self.audio_path = ""
def _MCI_command(self, command):
print(command)
err = windll.winmm.mciSendStringW(command, 0, 0, 0) # If printed, this returns 263 == unrecognized audio device
print(err)
def _play(self, start=0, wait=True):
th = threading.Thread(target=self.__play__, args=(start, wait))
th.start()
def __play__(self, start, wait):
self._MCI_command(f'play {self.audio_path} from {start} {"wait" if wait else ""}', )
def _open_song(self, audio_path):
self.audio_path = audio_path
self._MCI_command(f'open {audio_path} alias {self.alias}')
def _set_volume(self):
self._MCI_command(f'setaudio {self.alias} volume to 500')
def _pause(self):
self._MCI_command(f'pause {self.alias}')
def _resume(self):
self._MCI_command(f'resume {self.alias}')
def _stop(self):
self._MCI_command(f'stop {self.alias}')
if __name__ == '__main__':
p = WinPlayAudio()
p._open_song("start.mp3")
p._play()https://stackoverflow.com/questions/68245425
复制相似问题