From 40aec7c1d7847713dd71453d0bdaebda6485ff81 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Wed, 30 Oct 2019 14:35:22 -0400 Subject: [PATCH] New Demo Program - A simple VLC based media player --- DemoPrograms/Demo_Media_Player_VLC_Based.py | 68 +++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 DemoPrograms/Demo_Media_Player_VLC_Based.py diff --git a/DemoPrograms/Demo_Media_Player_VLC_Based.py b/DemoPrograms/Demo_Media_Player_VLC_Based.py new file mode 100644 index 00000000..748a20e9 --- /dev/null +++ b/DemoPrograms/Demo_Media_Player_VLC_Based.py @@ -0,0 +1,68 @@ +""" + Bare Bones Media Player Demo with Playlist. Adapeted from media player located at https://github.com/israel-dryer/Media-Player + Original Author : Israel Dryer + Modified to be a PySimpleGUI Demo Program + A very simple media player ready for you to customize. Uses the VLC player to playback local files and YouTube streams. You will need to install the Python bindings for VLC as well as the VLC player itself. + You will need to pip install: + pip install python-vlc + pip install youtube-dl +""" +import PySimpleGUI as sg +import vlc + +#------- GUI definition & setup --------# + +sg.change_look_and_feel('DarkBlue') + +def btn(name): # a PySimpleGUI "User Defined Element" (see docs) + return sg. Button(name, size=(6, 1), pad=(1, 1)) + +layout = [[sg.Input(default_text='Video URL or Local Path:', size=(30, 1), key='-VIDEO_LOCATION-'), sg.Button('load')], + [sg.Image('', size=(300, 170), key='-VID_OUT-')], + [btn('previous'), btn('play'), btn('next'), btn('pause'), btn('stop')], + [sg.Text('Load media to start', key='-MESSAGE_AREA-')]] + +window = sg.Window('Mini Player', layout, element_justification='center', finalize=True) + +#------------ Media Player Setup ---------# + +inst = vlc.Instance() +list_player = inst.media_list_player_new() +media_list = inst.media_list_new([]) +list_player.set_media_list(media_list) +player = list_player.get_media_player() +player.set_hwnd(window['-VID_OUT-'].Widget.winfo_id()) + +#------------ The Event Loop ------------# +while True: + event, values = window.read(timeout=1000) # run with a timeout so that current location can be updated + if event is None: + break + + if event == 'play': + list_player.play() + if event == 'pause': + list_player.pause() + if event == 'stop': + list_player.stop() + if event == 'next': + list_player.next() + list_player.play() + if event == 'previous': + list_player.previous() # first call causes current video to start over + list_player.previous() # second call moves back 1 video from current + list_player.play() + if event == 'load': + if not 'Video URL' in values['-VIDEO_LOCATION-']: + media_list.add_media(values['-VIDEO_LOCATION-']) + list_player.set_media_list(media_list) + window['-VIDEO_LOCATION-'].update('Video URL or Local Path:') # only add a legit submit + + # update elapsed time if there is a video loaded and the player is playing + if player.is_playing(): + window['-MESSAGE_AREA-'].update("{:02d}:{:02d} / {:02d}:{:02d}".format(*divmod(player.get_time()//1000, 60), + *divmod(player.get_length()//1000, 60))) + else: + window['-MESSAGE_AREA-'].update('Load media to start' if media_list.count() == 0 else 'Ready to play media' ) + +window.close()