繁体   English   中英

WinSound播放声音是否来自变量?

[英]WinSound Play sound from variable?

如何使用winsound.PlaySound()函数播放存储的变量而不是特定声音? 我正在制作一个临时的音乐播放器,并且希望能够简单地播放用户选择的歌曲。 这是我当前的代码。

import Tkinter, Tkconstants, tkFileDialog
import winsound
from Tkconstants import *
from tkFileDialog import *
from PIL import Image, ImageTk

class MusicPlayer(Tkinter.Frame):

  def __init__(self, root):

    Tkinter.Frame.__init__(self, root)


    # options for buttons
    button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}

    #options for images

    # define image
    img = Image.open('musicPlayer.PNG')
    bg = ImageTk.PhotoImage(img)
    label = Tkinter.Label(image=bg)
    label.image = bg
    label.pack()

    # define buttons
    but1 = Tkinter.Button(self, text='Play', command=self.play)
    but2 = Tkinter.Button(self, text='Stop', command=self.stop)
    but1.grid(sticky="S, W, E", column=1)
    but1.grid(sticky="S, W, E", column=1)   

    # define options for opening or saving a file
    self.file_opt = options = {}
    options['defaultextension'] = '*.wav'
    options['filetypes'] = [('WAV Sound Files', '*.wav')]
    options['initialdir'] = 'C:\\'
    options['initialfile'] = '.wav'
    options['parent'] = root
    options['title'] = 'Pick a File'

    # This is only available on the Macintosh, and only when Navigation Services are installed.
    #options['message'] = 'message'

    # if you use the multiple file version of the module functions this option is set automatically.
    #options['multiple'] = 1

    # defining options for opening a directory
    self.dir_opt = options = {}
    options['initialdir'] = 'C:\\'
    options['mustexist'] = False
    options['parent'] = root
    options['title'] = 'Pick a Dir'

  def askopenfile(self):

    return tkFileDialog.askopenfile(mode='r', **self.file_opt)

  def askopenfilename(self):

    # get filename
    filename = tkFileDialog.askopenfilename(**self.file_opt)

    # open file on your own
    if filename:
      return open(filename, 'r')
      print filename

  def asksaveasfile(self):

    return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)

  def asksaveasfilename(self):

    # get filename
    filename = tkFileDialog.asksaveasfilename(**self.file_opt)

    # open file on your own
    if filename:
      return open(filename, 'w')

  def askdirectory(self):

    return tkFileDialog.askdirectory(**self.dir_opt)

  def play(self):
    soundfile = self.askopenfilename()

    winsound.PlaySound(soundfile, winsound.SND_FILENAME)

  def stop(self):
    winsound.PlaySound(None, winsound.SND_PURGE)

if __name__=='__main__':
  root = Tkinter.Tk()
  root.iconbitmap(r'C:\Python27\DLLs\musicPlayer.ico')
  MusicPlayer(root).pack()
  root.wm_title('Music Player')
  root.mainloop()

注意事项

  • 我对winsound插件以及Python本身都不陌生,并且不确定应该使用哪个标志。

我得到错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
  File "C:\Users\Brenneman.Josh.19\Downloads\code (2)\MusicPlayer.py", line 88, in play
    winsound.PlaySound(soundfile, winsound.SND_FILENAME)
TypeError: must be string or read-only buffer, not file

winsoundPlaySound()需要传递一个带有文件名和一个或多个标志的字符串。 我建议使用SND_ASYNC标志,该标志播放声音并立即返回。 使用以下内容:

winsound.PlaySound('C:/Windows/Media/chimes.wav', winsound.SND_ASYNC)

暂无
暂无

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

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