Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listening music from url
#1
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()
Reply
#2
Solved by using the urs as a file.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting and listening test 2 2,099 Nov-13-2020, 04:43 PM
Last Post: michael1789
  How can I make this server stop listening for connections to sockets Emekadavid 0 3,386 Jun-03-2020, 02:28 PM
Last Post: Emekadavid
  listening to USB DPaul 3 5,289 Dec-27-2017, 07:31 AM
Last Post: DPaul
  Array of Listening Sockets abarnes 1 2,583 Sep-28-2017, 07:26 PM
Last Post: abarnes
  listening to user input even after opening an application in mac sharma16aug 4 4,849 Jan-24-2017, 10:43 AM
Last Post: sharma16aug

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020