View Full Version : wa-hoo!!!
:)
in leiu of a celebratory dance, I'll post some code showing how to play an mp3 file in python, which I learned this morning from the Dr. Dobbs Python URL (http://www.ddj.com/topics/pythonurl/):
import pygame.mixer as mixer
mixer.init(44100)
mixer.music.load("my mp3 file.mp3")
mixer.music.play()
This of course assumes you have pygame (http://pygame.org/) installed, which is python's wrapper around the Simple DirectMedia Layer (SDL) (http://www.libsdl.org/).
Oh, and if anyone has python questions, ask ahead. I'm no guru (yet), but I'll at least help you find the answer.
imported_bryan
03-06-2002, 01:12 AM
kmj is our resident python guy. horray for kmj!
GnuVince
03-07-2002, 12:19 AM
Totally sweet!!!! I love it!!!
GnuVince
03-07-2002, 07:55 PM
Here's a small, basic and dumb mp3 player in Python. There's a bug, some songs don't play. I don't understand why. Anyone has an idea?
#!/usr/bin/env python
# pygame is not a standard library, so we must make sure that
# the user has it. *If he does not, we tell him so and exit
try:
*import pygame.mixer
except ImportError:
*print "Cannot load pygame.mixer <http://www.pygame.org>"
*sys.exit(1)
import sys
import string
import random
import os
# Mp3Player class has the methods to play a song, stop a song and skip a song
#
class Mp3Player:
*# Initializations
*#
*def __init__(self, songs):
* *self.songs * = songs
* *self.songNum = random.randrange(len(self.songs))
* *self.song * *= sys.argv[1] + '/' + self.songs[self.songNum]
*# Load the song and play it. *Skip to the next song in case an exception
*# is raised
*#
*def play(self):
* *try:
* * *pygame.mixer.music.load(self.song)
* * *pygame.mixer.music.play()
* *except pygame.error:
* * *self.next()
*# Stop the current song
*#
*def stop(self):
* *try:
* * *pygame.mixer.music.stop()
* *except pygame.error:
* * *return
*
*# Go to the next song
*#
*def next(self):
* *self.songNum = random.randrange(len(self.songs))
* *self.song * *= sys.argv[1] + '/' + self.songs[self.songNum]
if __name__ == '__main__':
*# Exit with an error message if user hasn't specified a directory
*#
*if len(sys.argv) < 2:
* *print "Usage: %s <Directory>" % sys.argv[0]
* *sys.exit(1)
*
*# Put the directory content into a list.
*# Exit with an error message if argv[1] is not a directory
*#
*try:
* *mysongs = os.listdir(sys.argv[1])
*except OSError:
* *print sys.argv[1], ": no such directory"
* *sys.exit(1)
* *
*pygame.mixer.init(44100) *# Sets mixer to 44Khz
*# Initialize the player with the list of songs from the directory
*#
*player = Mp3Player(os.listdir(sys.argv[1]))
*
*# Play the first song
*#
*player.play()
*option = None
*while option != 4:
* *print '\x1b[H\x1b[2J' *# This clears the screen
* *print string.center("Welcome to the very basic Python MP3 player!", 80)
* *print "\n"
* *# Menu
* *#
* *print "1. Play"
* *print "2. Stop"
* *print "3. Next"
* *print "4. Exit\n"
* *# Get option and display an error message if it's not a number
* *#
* *try:
* * *option = int(raw_input("> "))
* *except ValueError:
* * *print "Bad option"
* * *raw_input()
* * *continue
* *
* *# Behave accordingly to the chosen option
* *if option == 1:
* * *player.play()
* *elif option == 2:
* * *player.stop()
* *elif option == 3:
* * *player.next()
* * *player.play()
* *
* *elif option == 4:
* * *player.stop()
* *else:
* * *print "Bad option"
* * *raw_input()
* * *continue
GnuVince
03-07-2002, 08:46 PM
Any comments?
I ran into that problem with not being able to read certain files, too. I'll bet it has something to do with the file format somehow. I haven't looked at the docs at all.
GnuVince
03-08-2002, 04:11 PM
It seems the ID3v2 tags phoquent le chien.
I'm assuming phoquent is an explitive, since neither lycos nor babelfish would translate it. Perhaps the lib is old.
Ludootje
03-10-2002, 10:25 AM
"phoquent" doesn't exist, at least not in non-Canadian AFAIK
But I suppose he means 'It seems the ID3v2 tags fuck the dog.'
'phoque' is an animal too, but I don't remember the English name of that animal.
GnuVince
03-10-2002, 10:42 AM
Seal. But I didn't meant to write 'fucK'
Here are a few links I just snagged off comp.lang.python in relation to id3 tags. They let you read the tags, but I guess you'd still have to remove the tags from the files to use them with the mixer. Perhaps you can remove the tags and give the mixer the raw data. I'm not sure.
http://www.id3.org/implement.html
http://csl.cse.ucsc.edu/~ben/python/
https://sourceforge.net/projects/pyid3/
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.