You are here: Home Blog listen.py: Little script to stream internet radio for an hour and then stop

listen.py: Little script to stream internet radio for an hour and then stop

Posted by Nicholas Spagnoletti at Jun 25, 2011 06:38 PM |
To avoid accidentally wasting lots of bandwidth I made this little python script that will play an internet stream using gstreamer for an hour and then stop.

In the mornings I like listening to the BBC World Service while having breakfast and getting ready. I used to use my Worldspace satellite receiver but a while ago they ceased operating and a few months ago they stopped broadcasting the BBC. I realised I would have to listen to it over the net. The Worldspace radio had a very useful builtin FM modulator so you could listen around your home on normal FM radios. I purchased a cheap FM modulator to attach to my computer so that we can listen all over the flat on normal FM radios.

To solve the problem of accidentally leaving the stream running all day and chowing all our expensive international bandwidth, I wrote this little app which uses wx to make a very simple GUI that has a button to start playing the stream (using gstreamer of course) and a timer that counts down. After an hour the stream stops.

Dependencies are wxpython and the python-gstreamer bindings. On Ubuntu you can install these packages:

apt-get install python-gst0.10 python-wxgtk2.8

Here's the script, where you can choose between the World Service and BBC Radio 4 - add any other streams to the streams list of tuples.

#!/usr/bin/python
import gst
import wx
import time

player = gst.element_factory_make("playbin", "player")

streams = [
 ('BBC World Service', 'http://mp32.bbc.streamuk.com:80/'),
 ('BBC Radio 4', 'mms://wm-live.bbc.net.uk/wms/bbc_ami/radio4/radio4_bb_live_int_ep1_sl0?BBC-UID=b4adbcdf5ba7eea337440a77610973cee5bd1676f0b0524162a83074f35cc068&SSO2-UID=')
]


class mainwin(wx.Frame):
    playfor=60*60
    s=playfor
    
    def __init__(self, parent=None):
        super(mainwin,self).__init__(parent, wx.ID_ANY, title='Listen to the BBC')
        
        panel = wx.Panel(self, wx.ID_ANY)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.button = wx.Button(panel, wx.ID_ANY, 'Start')
        self.cb = wx.ComboBox(panel, choices=[s[0] for s in streams],style=wx.CB_DROPDOWN|wx.CB_READONLY,value=streams[0][0])
        
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.update, self.timer)
        
        self.label = wx.StaticText(panel)
            
        panel.SetSizer(sizer)
        sizer.Add(self.cb)
        sizer.Add(self.button)
        sizer.Add(self.label)
        
        
        self.Bind(wx.EVT_BUTTON, self.toggle, self.button)
        
    
    def toggle(self, evt=None):        
        label = self.button.GetLabel()
        station = self.cb.GetValue()
        if label=='Start':
            for s, uri in streams:
                if station==s:
                    player.set_property('uri', uri)
            player.set_state(gst.STATE_PLAYING)
            self.button.SetLabel('Stop')
            self.timer.Start(1000)
            
        elif label=='Stop':
            self.s=1
            self.update()
            player.set_state(gst.STATE_NULL)
            self.button.SetLabel('Start')
    
    def update(self, evt=None):
        self.s -= 1
        if self.s <= 0:
            player.set_state(gst.STATE_NULL)
            
            self.button.SetLabel('Start')
            self.timer.Stop()
            self.s=self.playfor
            self.label.SetLabel('')
        else:
            mins = self.s / 60
            secs = self.s % 60
            self.label.SetLabel("%02d:%02d" % (mins,secs))
        
        

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = mainwin().Show()
    app.MainLoop()

Comments (1)

Craig Watson Sep 03, 2011 07:49 AM
SmartHome here we come!