user2852955
user2852955

Reputation: 43

MP3 with Pyaudio

import pyaudio
import wave

chunk = 1024
wf = wave.open('yes.mp3', 'rb')
p = pyaudio.PyAudio()

stream = p.open(
    format = p.get_format_from_width(wf.getsampwidth()),
    channels = wf.getnchannels(),
    rate = wf.getframerate(),
    output = True)
data = wf.readframes(chunk)

while data != '':
    stream.write(data)
    data = wf.readframes(chunk)

stream.close()
p.terminate()

No matter how I put this, while trying multiple methods I seem to keep getting the following error in terminal:

raise Error, 'file does not start with RIFF id'

I would use pyglet but media and all other modules aren't detected even though I'm able to import pyglet.

Any help?

Upvotes: 3

Views: 9332

Answers (1)

thenormalsquid
thenormalsquid

Reputation: 96

You're using wave to attempt to open a file that is not wav. Instead, you're attempting to open an mp3 file. The wave module can only open wav files, so you need to convert the mp3 to wav. Here's how you can use pyglet to play an mp3 file:

        import pyglet

        music = pyglet.resource.media('music.mp3')
        music.play()

        pyglet.app.run()

It would be much simpler than the method you're trying. What errors are you getting with pyglet?

Upvotes: 1

Related Questions