I use libVLC with the python bindings. Thanks to the help of someone in this forum I managed to get it to play a webradio. It requires playing the playlist first, waiting for it to finish, and then playing the first subitem.
This works fine right now.
The problem I have is when I want to change the media dynamically. I've made a function which creates a new media with the new source and plays it. When I call this function in a very simple Python script with a new url it works fine. But when I try in a bigger Python application (involving a CherryPy webserver and a couple of small threads), it crashes Python without any error message.
So I suppose that i'm not handling the change of media the proper way. Do I need to wate for something to load ? Should I terminate the first media somehow before creating a new one ?
I have no clue what is wrong because I get no errors at all. Is there at least a way I could increase libVLC's verbosity, or make an error log ?
I include the simple script here, but I can't include my bigger python application. I use the same functions (VLC_play() and the callback end_reached()) in the big application.
Code: Select all
import vlc
import time
url="http://tvradio.ert.gr/radio/liveradio/asx/net.asx"
global flag
global m
global i
global p
flag = 0
def end_reached(self):
global flag
flag = 1
print("End reached!")
def VLC_play(file):
global m
global p
global i
global flag
m=i.media_new(file) # Create new media
p.set_media(m) # Set URL as the player's media
m.release()
p.play() # play it
while flag == 0: # Wait until the end of the first media has been reached...
time.sleep(0.5)
print('Loading playlist...')
flag = 0
sub_list = m.subitems() # .. and get the sub itmes in the playlist
sub_list.lock()
sub = sub_list.item_at_index(0) # Get the first sub item
sub_list.unlock()
sub_list.release()
p.set_media(sub) # Set it as the new media in the player
p.play() # and play it
sub.release()
a = 0
while p.is_playing()==0: # This is just a counter that runs until the stream is actually being played
time.sleep(0.5)
a += 1
print(a)
i=vlc.Instance() #Create VLC instance
p=i.media_player_new() # Create new media player
event_manager = p.event_manager() # Attach event to player (next 3 lines)
event=vlc.EventType()
event_manager.event_attach(event.MediaPlayerEndReached, end_reached)
VLC_play(url)