我如何在 python 3 中播放声音?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30860123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:05:44  来源:igfitidea点击:

How do i play a sound in python 3?

pythonaudiopygame

提问by whatname

I wanted to write a python script to play a sound (recorded using windows recorder)!

我想写一个python脚本来播放声音(使用windows录音机录制)!

I read pygame can do the job and installed pygame! But I don't know how to write a code that plays a sound from a specific path! I have to play an audio file located at C:\Users\Asdf\Documents\Audio.wav

我读过 pygame 可以完成这项工作并安装了 pygame!但是我不知道如何编写一个代码来播放来自特定路径的声音!我必须播放位于C:\Users\Asdf\Documents\Audio.wav

I tried a script from here http://pythonprogramming.net/adding-sounds-music-pygame/

我从这里尝试了一个脚本http://pythonprogramming.net/adding-sounds-music-pygame/

import pygame
crash_sound = pygame.mixer.Sound("crash.wav")

But then I get an error message:

但后来我收到一条错误消息:

Traceback (most recent call last): File "", line 1, in crash_sound = pygame.mixer.Sound("crash.wav") AttributeError: 'module' object has no attribute 'mixer'

回溯(最近一次调用):文件“”,第 1 行,在 crash_sound = pygame.mixer.Sound(“crash.wav”) AttributeError: 'module' object has no attribute 'mixer'

So how do I write the script to play that Audio.wav file using pygame?

那么如何编写脚本来使用 pygame 播放 Audio.wav 文件呢?

I am using Python 3.4 64 bit version!

我正在使用 Python 3.4 64 位版本!

采纳答案by Rikard S?derstr?m

Add python to windows 7 PATH

将 python 添加到 Windows 7 PATH

  1. Hold Winand press Pause.
  2. Click Advanced System Settings.
  3. Click Environment Variables.
  4. In "User variables" Click "New" if there is no PATH, else select PATH variable and click "Edit"
  5. Append ;C:\python34to the Pathvariable.
  6. Restart Command Prompt.
  1. 按住Win并按下Pause
  2. 单击高级系统设置。
  3. 单击环境变量。
  4. 在“用户变量”中,如果没有 PATH 则单击“新建”,否则选择 PATH 变量并单击“编辑”
  5. 附加;C:\python34Path变量。
  6. 重新启动命令提示符。

enter image description here

在此处输入图片说明

Installing pygame

安装pygame

You first want to install pygame. You can do this in a number of ways. But to install from source

您首先要安装 pygame。您可以通过多种方式执行此操作。但是要从源安装

  1. Download and extract pygame-1.9.1release.zip
  2. Open a terminal
  3. cd into the pygame-1.9.1releasefolder you just extracted
  4. Run python setup.py
  1. 下载并解压pygame-1.9.1release.zip
  2. 打开终端
  3. cd 进入刚刚解压的pygame-1.9.1release文件夹
  4. python setup.py

Alternatively if python is associated with .pyfiles in your windows installation (this is the default), open the extracted folder and double click setup.py

或者,如果 python 与.pyWindows 安装中的文件相关联(这是默认设置),请打开解压缩的文件夹并双击setup.py

Running pygame

运行 pygame

You need to run

你需要跑

pygame.init()

Before any calls to pygame modules !

在调用 pygame 模块之前!

Hope that solves the problem for you

希望能为您解决问题

Example

例子

import pygame
pygame.init()
s = pygame.mixer.Sound("C:\Users\Asdf\Documents\Audio.wav")

# Start playback
s.play()

# Stop playback
s.stop()

Play a sound on windows (without pygame)

在 Windows 上播放声音(没有 pygame)

import winsound

fname = "C:\Users\Asdf\Documents\Audio.wav"
winsound.PlaySound(fname, winsound.SND_FILENAME)

回答by ArtOfWarfare

Here's a simple solution that works on all platforms with no dependencies other than a single Python file.

这是一个简单的解决方案,适用于所有平台,除了单个 Python 文件之外没有任何依赖项。

First install playsoundwith pip:

首先playsound使用pip安装:

pip install playsound

Then import the function of the same name from the module and run it. Pretty easy, no?

然后从模块中导入同名函数并运行。很简单,不是吗?

from playsound import playsound
# wav works on all platforms. mp3 works on OS X. Other filetype/OS combos may work but have not been tested.
playsound('/path/to/file/you/want/to/play.wav')

Disclaimer: I wrote playsound. It takes an optional second argument, block = False, which will make it so the function call doesn't block. It'll block by default.

免责声明:我写了playsound。它需要一个可选的第二个参数,block = False,这将使函数调用不会阻塞。默认情况下它会阻止。

回答by Anil_M

I've used pydubeffectively for this purpose. The module can be installed as

为此,我有效地使用了pydub。该模块可以安装为

pip install pydub

pip install pydub

pydubdoes need FFMPEGinstalled. Details of installation of pydub and ffmpeg given @ https://github.com/jiaaro/pydub

pydub确实需要安装FFMPEG。pydub 和 ffmpeg 的安装细节给定@https ://github.com/jiaaro/pydub

Once the above dependancies are installed, The library provides comprehensive manipulation of various audio formats across different platforms [ windows , Linux , Mac ] in rather easy way.

安装上述依赖项后,该库以相当简单的方式提供跨不同平台 [windows、Linux、Mac] 的各种音频格式的综合操作。

Here is an example

这是一个例子

from pydub import AudioSegment
from pydub.playback import play

fname = "C:\Users\Asdf\Documents\Audio.wav"
mysong = AudioSegment.from_wav(fname)
play(mysong)