Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing remote audio files in Python? [closed]

I'm looking for a solution to easily play remote .mp3 files. I have looked at "pyglet" module which works on local files, but it seems it can't handle remote files. I could temporary download the .mp3 file but that's not reccomended due to how large the .mp3 files could appear to be.

I rather want it to be for cross-platform instead of Windows-only etc.

Example, playing a audio file from:

http://example.com/sound.mp3

Just stream the file as it's downloads, my idea is a MP3 player in Python which opens Soundcloud songs.

like image 920
Jack Avatar asked Dec 08 '13 22:12

Jack


People also ask

Can you play sound files in Python?

Play sound on Python is easy. There are several modules that can play a sound file (. wav). These solutions are cross platform (Windows, Mac, Linux).

Can Pyaudio play MP3?

Pyaudio allows us to play and record sounds with Python. To play MP3, however, we first need to convert the MP3 file to WAV format with ffmpeg. To use ffmpeg in Python, we use an interface tool called Pydub, which directly calls our ffmpeg executable and integrates with Pyaudio.

How do I open a WAV file in Python?

python-sounddevice In order to play WAV files, numpy and soundfile need to be installed, to open WAV files as NumPy arrays. The line containing sf. read() extracts the raw audio data, as well as the sampling rate of the file as stored in its RIFF header, and sounddevice.


2 Answers

You can use GStreamer with python bindings (requires PyGTK).

Then you can use this code:

import pygst
import gst

def on_tag(bus, msg):
    taglist = msg.parse_tag()
    print 'on_tag:'
    for key in taglist.keys():
        print '\t%s = %s' % (key, taglist[key])

#our stream to play
music_stream_uri = 'http://mp3channels.webradio.antenne.de/chillout'

#creates a playbin (plays media form an uri) 
player = gst.element_factory_make("playbin", "player")

#set the uri
player.set_property('uri', music_stream_uri)

#start playing
player.set_state(gst.STATE_PLAYING)

#listen for tags on the message bus; tag event might be called more than once
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
bus.connect('message::tag', on_tag)

#wait and let the music play
raw_input('Press enter to stop playing...')

GStreamer playbin Docs

UPDATE

Controlling the player:

def play():
    player.set_state(gst.STATE_PLAYING)

def pause():
    player.set_state(gst.STATE_PAUSED)

def stop():
    player.set_state(gst.STATE_NULL)

def play_new_uri( new_uri ):
    player.set_state(gst.STATE_NULL)
    player.set_property('uri', new_uri )
    play()
like image 141
quleuber Avatar answered Oct 05 '22 23:10

quleuber


PyAudio seems to be what you are looking for. It is a python module that allows you to reproduce and record streamed audio files. It also allows you implement the server.

According PyAudio's site: Runs in GNU/Linux, Microsoft Windows, and Apple Mac OS X.

This is an example I copy from http://people.csail.mit.edu/hubert/pyaudio/#examples :

"""PyAudio Example: Play a WAVE file."""

import pyaudio
import wave
import sys

CHUNK = 1024

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

data = wf.readframes(CHUNK)

while data != '':
    stream.write(data)
    data = wf.readframes(CHUNK)

stream.stop_stream()
stream.close()

p.terminate()

I think you will find this interesting too. And surely brings you some ideas.

like image 32
Raydel Miranda Avatar answered Oct 05 '22 23:10

Raydel Miranda