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

<pre><code>import os
os.startfile('hello.mp3')
</code></pre>

<p>But I Just Got The <strong>Error</strong>:</p>

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

<p>I Have Also Tried This:</p>

<pre><code>import vlc
p = vlc.MediaPlayer("file:hello.mp3")
p.play()
</code></pre>

<p>But I Get The <strong>Error</strong>:</p>

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

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

<p>You will need to install the <code>vlc.py</code> module from <a href="https://wiki.videolan.org/Python_bindings" rel="nofollow">https://wiki.videolan.org/Python_bindings</a></p>

<p>The absolute bare bones for this would be something like:</p>

<pre><code>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()
</code></pre>

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

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

<pre><code>import os
os.system('play /home/rolf/vp1.mp3')
</code></pre>

<p>You could also look at Gstreamer, although the documentation could do with some improvement.</p>

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

<p>play is stopped with <code>player.set_state(gst.STATE_NULL)</code>,<br>
pause with <code>player.set_state(gst.STATE_PAUSED)</code></p>

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

python mp3 playback