1

I have tried playing file using Pygame This is the following code:

    import time, sys
    from pygame import mixer
    # pygame.init()
    mixer.init()

   sound = mixer.Sound("C:\Users\sharathchandra\Downloads\17.wav")
   sound.play()

   time.sleep(5)

But it does not throw any error but the song is not played. I tried playing .wav files but with the same result I also tried pyglet But it shows >WAVEFormatException: AVbin is required to decode compressed media I would like to learn how to install AVbin I have run the codes using spyder on Windows and my python version is 2.7. I have also run many examples of pygame but no use. I want to Learn a correct way for Playing the mp3 files using Python and How to install the Desired Library For that Purpose.

Michael
  • 1,089
  • 1
  • 11
  • 28
v.sharath chandra
  • 81
  • 1
  • 2
  • 11
  • Make sure the WAV file is encoded properly. There's many different formats for it, so go on the pygame wiki and check which one is proper. – Spooky Jan 31 '16 at 02:11

3 Answers3

1
'''
Created on 2016. 6. 6.
This module is for playing a mp3 file by using pygame module
@author: Peter Sun
'''

import pygame

filePath = r"C:\learn\***.mp3"    #change to your MP3 file path

def playmusic(filename):
    BUFFER = 3072
    pygame.mixer.init()
    FREQ, SIZE, CHAN = pygame.mixer.get_init()
    pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
    pygame.init()
    pygame.mixer.init()
    clock = pygame.time.Clock()

    pygame.mixer.music.load(filename)


    pygame.mixer.music.play()
    while True:
        clock.tick(500)
        print clock

try:

    playmusic(filePath)
except KeyboardInterrupt:
    pygame.mixer.music.stop()
    print("User soptted music")
except Exception:
    print("Unknown error")

print "Done!"
peter
  • 21
  • 3
  • This answer seems to have a bunch of extra code that doesn't directly answer the question, such as the clock stuff. I would try to minimize this to just what you need to load and play a .wav file. – Amndeep7 Jun 06 '16 at 20:53
  • this file was only trying to open a mp3 file. According the title, the code is for a mp3 file. You can get rid of the clock thing if you don't want it. it shows fps dynamically to monitor wave of audio file. – peter Jun 07 '16 at 21:57
  • Thank you so much your solution worked perfectly and i think the clock stuff prevents the console from closing down right? – Adophilus Aug 22 '19 at 21:38
0

Try using this (solution taken from here):

mixer.music.load('C:\Users\sharathchandra\Downloads\17.wav') # you may use .mp3 but support is limited
mixer.music.play()

Many more ways can be found here and here.

Also, you might want to read the documentation on Pygame's music.

Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75
0

Try the VLC Python module, vlc.py, as explained here:

import vlc
p = vlc.MediaPlayer("file:///path/to/track.mp3")
p.play()

And you can stop it with:

p.stop()
Community
  • 1
  • 1
paeslago
  • 1
  • 3
  • Can you specify how to install the vlc module? – v.sharath chandra Jan 31 '16 at 04:44
  • Download `vlc.py` from [their git repository](https://git.videolan.org/?p=vlc/bindings/python.git;a=blob;f=generated/3.0/vlc.py;h=025331241e25bd6b39a6ede8a7a6b022d390ed6f;hb=HEAD) Put the file in the same place as your script and import it with `import vlc` – paeslago May 25 '18 at 11:06