WinSound从变量播放声音?

时间:2015-03-13 17:27:26

标签: python

我如何使用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

1 个答案:

答案 0 :(得分:2)

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

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