从Python Tkinter中的特定位置播放声音 [英] Playing sound from a specific location in Python Tkinter

查看:274
本文介绍了从Python Tkinter中的特定位置播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

当用户启动应用程序时,要从位置D:1.wav播放wav文件

To play a wav file from the location D:1.wav, when the application is started up by the user

研究

看到以下问题:

如何播放警报声音在python中?

使用Python播放音频

我尝试了什么

我尝试了以下几行:

示例1

import winsound
winsound.PlaySound('D:\1.wav',winsound.SND_FILENAME)  ##Did not work

示例2

import winsound
winsound.PlaySound('1.wav',winsound.SND_FILENAME)  ##DID not work

两次都获得默认声音,但没有获得应按照音频文件播放的声音

Both the times I got the default sound but not the sound that should have played as per the audio file

  1. 当我编写winsound.PlaySound('1.wav',winsound.SND_FILENAME)时,python在哪里检查文件1.wav?在哪个目录中?
  2. 为什么使用winsound.SND_FILENAME标志?
  1. When I write winsound.PlaySound('1.wav',winsound.SND_FILENAME) where does python check for the file 1.wav? In which directory?
  2. Why is the flag winsound.SND_FILENAME used for?

规格:Python 2.7 TKinter 8.5 Windows XP SP

specs Python 2.7 TKinter 8.5 Windows XP SP

帮助我解决此问题.

推荐答案

更改

winsound.PlaySound('D:\1.wav',winsound.SND_FILENAME)

winsound.PlaySound('D:\\1.wav',winsound.SND_FILENAME)

防止python转义路径:

to prevent python from escaping your path:

>>> a = '\1.wav'
>>> a
'\x01.wav'

winsound.SND_FILENAME

winsound.SND_FILENAME

sound参数是WAV文件的名称.请勿与SND_ALIAS一起使用.

The sound parameter is the name of a WAV file. Do not use with SND_ALIAS.

(来自 winsound文档)

如果您还不使用标志winsound.SND_NODEFAULT,则winsound会在找不到指定文件的情况下播放默认声音.

If you don't use also the flag winsound.SND_NODEFAULT, winsound plays the default sound if it cannot find your specified file.

创建一个包含wav文件的文件夹(例如D:\test_sounds\),

Create a folder (say D:\test_sounds\) with your wav file inside, add that folder to your PYTHONPATH variable and try running your code again. Or (better, if you plan to distribute your code), following the same post I just linked, add this to your code:

import sys
if "D:\\my_sound_folder" not in sys.path:
    sys.path.append("D:\\my_sound_folder")

,然后您就可以调用winsound.PlaySOund('1.wav', winsound.SND_FILENAME)了,因为它将位于您的可用路径中

and then you can just call winsound.PlaySOund('1.wav', winsound.SND_FILENAME) since it will be in your available paths

这篇关于从Python Tkinter中的特定位置播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆