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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os, random
from subprocess import check_output, Popen
from shlex import split # needed for Popen
 
 
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
  Listening on receiving Interface (using scapy) CodyTheCodeNoob 1 1,910 Dec-22-2024, 10:51 PM
Last Post: PolandoFaker
  Waiting and listening test 2 3,076 Nov-13-2020, 04:43 PM
Last Post: michael1789
  How can I make this server stop listening for connections to sockets Emekadavid 0 4,677 Jun-03-2020, 02:28 PM
Last Post: Emekadavid
  listening to USB DPaul 3 6,329 Dec-27-2017, 07:31 AM
Last Post: DPaul
  Array of Listening Sockets abarnes 1 3,243 Sep-28-2017, 07:26 PM
Last Post: abarnes
  listening to user input even after opening an application in mac sharma16aug 4 6,070 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