User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

How do you use Python to play MP3 files?

Sun May 31, 2015 10:33 am

Hi,
I'm making a code in minecraft but I need to know if you can play MP3 files if you can please tel me.
Thank you in advance

H1pp0s ;)

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 10:37 am

H1pp0s wrote:Hi,
I'm making a code in minecraft but I need to know if you can play MP3 files if you can please tel me.
Yes, you can.

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 10:44 am

ame wrote:
H1pp0s wrote:Hi,
I'm making a code in minecraft but I need to know if you can play MP3 files if you can please tel me.
Yes, you can.
Thank you for your reply do you know the line of code?and thank you in advance :)

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 10:49 am

H1pp0s wrote:
ame wrote:
H1pp0s wrote:Hi,
I'm making a code in minecraft but I need to know if you can play MP3 files if you can please tel me.
Yes, you can.
Thank you for your reply do you know the line of code?and thank you in advance :)
Presumably you are using Python. You will need to spawn a new process with an mp3 player application.

The simplest way might be:

Code: Select all

import os
os.system('mpg321 foo.mp3 &')
You must install mpg321 first:

Code: Select all

sudo apt-get install mpg321
Try googling for "python play mp3"

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 10:50 am

You can use something like this:

Code: Select all

import pygame

pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.play()

while pygame.mixer.music.get_busy() == True:
	pass
Laurens

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 10:58 am

Thanks guys
So what would the line of code be if the MP3 song was mans zelmrerlow-heroes
Thank in advance
H1pp0s

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 11:00 am

I'm just asking Ame but can we be friends on this forum?
H1pp0s ;) ;) :) :D

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 11:02 am

Replace file.mp3 with the name of the track you want to play. It really is that simple.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 11:03 am

scotty101 wrote:Replace file.mp3 with the name of the track you want to play. It really is that simple.
Thanks so much Im still here but also coding on minecraft pi :)
H1pp0s

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 11:23 am

Laurens-wuyts wrote:You can use something like this:

Code: Select all

import pygame

pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.play()

while pygame.mixer.music.get_busy() == True:
	pass
Laurens
Hi thanks for the code!

I entered the code but it just said can't open Mans Zelmerlow-Heroes.mp3
(Which is the track Im trying to play)
Do you know what's happening?
H1pp0s

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 11:32 am

Is the python file in the same directory as the mp3 file?
If not, is it possible to place them in the same directory?

Laurens

gkreidl
Posts: 6307
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 11:56 am

Use the full file path, something like '/home/pi/name.mp3'
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:09 pm

gkreidl wrote:Use the full file path, something like '/home/pi/name.mp3'
Thanks but not working its saying it can't open it still :roll:

gkreidl
Posts: 6307
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:17 pm

H1pp0s wrote:
gkreidl wrote:Use the full file path, something like '/home/pi/name.mp3'
Thanks but not working its saying it can't open it still :roll:
What error message do you get?
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:23 pm

It was Traceback (most recent call last)
File "/home/pi/Downloads/minecraft-bootabully.py", Line 97, in <module>
Pygame.mixer.load("home/pi/Downloads/Mans zelmerlow-heroes.mp3")
Error: couldnt open 'home/pi/Downloads/Mans Zelmerlow-heroes.mps'

I am so :evil: at this thing please help

gkreidl
Posts: 6307
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:30 pm

The leading slash is missing: "/home/pi..."
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:31 pm

gkreidl wrote:The leading slash is missing: "/home/pi..."
Oh ok thx :lol:

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:33 pm

H1pp0s wrote:
gkreidl wrote:The leading slash is missing: "/home/pi..."
Oh ok thx :lol:
(Sigh) not working

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:33 pm

Also bear in mind that playing mp3 files using pygame may not work.

From the pygame docs:
Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:39 pm

elParaguayo wrote:Also bear in mind that playing mp3 files using pygame may not work.

From the pygame docs:
Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.
If you are sudesting a different system what system do you recomend?

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:40 pm

How do I use OGG? :?: :?::?::?:

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:47 pm

OGG is a file format. You just need to convert mp3 to ogg.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:50 pm

elParaguayo wrote:OGG is a file format. You just need to convert mp3 to ogg.
And how do I do that sorry I'm new to this but very ambitious
And I'm so :evil: at this Im gonna shut it down and :cry: soon

User avatar
H1pp0s
Posts: 42
Joined: Mon May 18, 2015 7:36 am
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:55 pm

Gtg its lunch time bye please tell me how I'll be very great full ;) :) :D

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: How do you use Python to play MP3 files?

Sun May 31, 2015 12:57 pm

You can do that here:
http://media.io/en/

Laurens

Return to “Python”