How to Play a .WAV File in Python

Written by allen bethea Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Play a .WAV File in Python
Python provides tools to play audio files. (Burke/Triolo Productions/Brand X Pictures/Getty Images)

Python's extensive set of standard libraries enable programmers to handle a wide range of programming tasks. Python is a "batteries included" language: nothing else to add. True to form, Python has built-in functions for reading and writing some audio file formats. Unfortunately, there are no standard ways to play audio files across the wide range of hardware and operating systems the Python programmer might encounter. Standard Python does, however, include a simple way to get the job done. The "os" or operating system module allows the programmer to use the underlying operating system to do the actual work of playing the file.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Windows or Linux computer with Python version 2.5 or later installed
  • Wave format audio file

Show MoreHide

Instructions

    Playing .Wav Files with Python on Linux

  1. 1

    Press the "ALT" and F2 key simultaneously. Enter "python" in the input box. Select the option to allow Python to run in the Linux terminal. Click on the "Run" button.

  2. 2

    At the Python prompt, import the os module by typing "import os".

  3. 3

    Enter the following system call on the Python command line.

    os.system("aplay sounds/test.wav")

    Replace the parameter "sounds/test.wav" with the location and name of the .wav file to be played.

  4. 4

    Press the return or enter key to execute the code.

    Playing .Wav Files with Python on Windows XP

  1. 1

    Click on the Start menu button and then select "Run" to load the Windows command interpreter.

  2. 2

    At the Windows command prompt, type "python" and then press the enter key. Some installations require the path to the directory containing the Python binaries in order to locate and load the interpreter. For example if Python 2.7 is the installed version, to load the Python interpreter enter "c:\python27\python."

  3. 3

    At the Python command line prompt, enter "import os" and then press the enter key.

  4. 4

    Enter the following command in the Python interpreter prompt to play the wave file.

    os.system("start /min mplay32 /play /close tada.wav")

    Replace the parameter "sounds/test.wav" with the location and name of the .wav file to be played.

  5. 5

    Press the return or enter key to play the wave file.

Tips and warnings

  • Python programmers have several open source audio libraries available that can provide the classes and methods needed to play popular audio file formats. These libraries often have dependencies: other libraries or files are needed in order for them to install and function.
  • Python's "os.system()" function allows any standard operating system command to be executed that the Python interpreter has permission to execute. It is possible to insert commands to delete or access important data and applications.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.