0

Oke so for the last week or so i've been looking around for a easy way to play sound files (wav or mp3 doesn't matter).

But alot of the posts i found just did not work or where really old. Does anyone have a simple way to do this wich is not outdated?

i've tryed winsound and pyaudio. pymedia and pygame don't even want to be installed.

i'm so lost becaus this is the thing that holds me back from finishing my code. Also i think this would help alot of starting Python users.

EDIT: i tryed using the code suggested here pyaudio help play a file. But this gives me a RIFF id error.

Community
  • 1
  • 1
DjKillerMemeStar
  • 425
  • 5
  • 18

1 Answers1

0

Documentation provides the code to play a audio file(.wav).

Edited:

import pyaudio
import wave
import sys

CHUNK = 1024

wf = wave.open("Path to audio file", 'rb')

# instantiate PyAudio (1)
p = pyaudio.PyAudio()

# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

# read data
data = wf.readframes(CHUNK)

# play stream (3)
while len(data) > 0:
    stream.write(data)
    data = wf.readframes(CHUNK)

# stop stream (4)
stream.stop_stream()
stream.close()

# close PyAudio (5)
p.terminate()
shiva
  • 2,535
  • 2
  • 18
  • 32