3

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

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

I tried a script from here 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'

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

I am using Python 3.4 64 bit version!

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
whatname
  • 51
  • 1
  • 1
  • 5

3 Answers3

4

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

pip install pydub

pydub does need FFMPEG installed. Details of installation of pydub and ffmpeg given @ 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.

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)
Anil_M
  • 10,893
  • 6
  • 47
  • 74
0

Add python to windows 7 PATH

  1. Hold Win and 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:\python34 to the Path variable.
  6. Restart Command Prompt.

enter image description here

Installing pygame

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

  1. Download and extract pygame-1.9.1release.zip
  2. Open a terminal
  3. cd into the pygame-1.9.1release folder you just extracted
  4. Run python setup.py

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

Running pygame

You need to run

pygame.init()

Before any calls to pygame modules !

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)

import winsound

fname = "C:\\Users\\Asdf\\Documents\\Audio.wav"
winsound.PlaySound(fname, winsound.SND_FILENAME)
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
Rikard Söderström
  • 1,000
  • 6
  • 14
  • This code seems the same as mine except mine does not work. Im trying on a Raspberry pi though and not windows. I keep getting an error message my_sound = pygame.mixer.my_sound('/home/pi/gpio-music-box/samples/drum_tom_mid_hard.wav') AttributeError: module 'pygame.mixer' has no attribute 'my_sound' Is there something wrong in the code or my setup? Thanks import pygame pygame.init my_sound= pygame.mixer.sound('/home/pi/gpio-music-box/samples/drum_tom_mid_hard.wav') my_sound.play() – Dave Aug 19 '19 at 12:45
-1

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

First install playsound with 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.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193