Python Forum

Full Version: Listening music from url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

The code below plays music on a Raspberry pi.

Instead of selecting music files, I'd like to listen to music from an url.

Can that be possible and if so, how?
TIA

import os, random
from subprocess import check_output, Popen
from shlex import split # needed for Popen

url = "http://icy-4.radioparadise.com/aac-32"

MPL_COMMAND = 'mplayer -softvol -volume {vol} {file}'

def popen(file, vol):
    command = MPL_COMMAND.format(file=file, vol=vol)
    proc = Popen(split(command), stdout=None, stderr=None)
    return proc.wait()

def play (vol=10) :
    path = "/home/pi/music/"
    # load files into a list
    songs = os.listdir ( path )

    '''if random is chosen'''
    # choose a rdm number between 0 and tot of files -1
    number = random.randint ( 0 , len ( songs ) - 1 )
    # assign file to rdm number
    file = songs[number]
    file = path + file
    popen ( file , vol )

play()
Solved by using the urs as a file.