0

I Am Trying To Play An MP3 File On Python , But I Can't Find The Right Module! I've Tried This:

import os
os.startfile('hello.mp3')

But I Just Got The Error:

Traceback (most recent call last):
  File "/Applications/Youtube/text 2 speech/test.py", line 2, in <module>
    os.startfile('hello.mp3')
AttributeError: 'module' object has no attribute 'startfile'

I Have Also Tried This:

import vlc
p = vlc.MediaPlayer("file:hello.mp3")
p.play()

But I Get The Error:

Traceback (most recent call last):
  File "/Applications/Youtube/text 2 speech/test.py", line 1, in <module>
    import vlc
ImportError: No module named 'vlc'

But I Still Can't Find The Right Module. Could Someone Please Help?

Random Person
  • 399
  • 1
  • 3
  • 9

2 Answers2

0

You will need to install the vlc.py module from https://wiki.videolan.org/Python_bindings

The absolute bare bones for this would be something like:

import vlc
Inst = vlc.Instance()
player = Inst.media_player_new()
Media = Inst.media_new_path('/home/rolf/vp1.mp3')
player.set_media(Media)
player.play()

Although you would need to check player.get_state() to see if it is still running, paused, stopped etc and player.stop() to stop the audio once started

As far as I am aware os.startfile() is only available for the windows operating system.
Using the play command line instruction (Linux and probably OS X)

import os
os.system('play /home/rolf/vp1.mp3')

You could also look at Gstreamer, although the documentation could do with some improvement.

import gst
player = gst.element_factory_make("playbin")
player.set_property("uri", "file:///home/james/vp1.mp3")
player.set_state(gst.STATE_PLAYING)

play is stopped with player.set_state(gst.STATE_NULL),
pause with player.set_state(gst.STATE_PAUSED)

Note: Avoid tutorials for Gstreamer version 0.10, search instead for version 1.0
Both vlc and Gstreamer allow you to play audio and video, although, in my opinion, vlc in simpler to use but Gstreamer is more flexible.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
0

import pygame from pygame import mixer

def play_music(): global paused

if (paused):
    mixer.music.unpause()
    paused = False
else:
    music = choose_music[0]
    mixer.music.load(music)
    mixer.music.play()
    show_details(music)
    musicpage.mainloop()