Python Forum
Lost Control over VLC - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Lost Control over VLC (/thread-40369.html)



Lost Control over VLC - jrockow - Jul-18-2023

First post; not sure if this is the proper forum?
I'm working on a Python Windows VLC media player script, and ran into a problem I can't figure out.
I've done a lot of reading and testing, but am getting nowhere.

After I start playing my video I need a popup window to select the proper subtitle.
As soon as I select the popup, I lose control of VLC.
I can close the popup and the video keeps playing but I have no control over it.

I have included the 2 functions that are relevant. They have been stripped to the minimum required to produce the problem.
I would truly appreciate any help.
Thanx!

from pynput import keyboard
from pynput.keyboard import Key
import time
import keyboard
global media_player

def vlc_player(filename):
       # creating vlc media player object
       media_player = vlc.MediaPlayer()
       # making keyboard input enable
       media_player.video_set_key_input(True)
       # media object
       media = vlc.Media(filename)
       media_player.set_media(media)
       # toggling full screen
       media_player.toggle_fullscreen()
       # set volumes
       media_player.audio_set_volume(100)
       #time=media_player.get_time()
       media_player.play()
       # STOP playing when end-of-file reached
       Ended = 6#set arbitrary value
       current_state = media_player.get_state()
       while current_state != Ended:
               # define keys pressed during playback
               if keyboard.is_pressed("s"):
                       media_player.stop()
               if keyboard.is_pressed("ctrl+x"):
                    sub_popup()
               current_state = media_player.get_state()
       media_player.stop()


def sub_popup():
   
       top = Toplevel(my_canvas, width = 250, height = 400)
       top.geometry('%dx%d+%d+%d' % (300, 350, 800, 200))
       top.configure(bg="black",highlightthickness=7)
       top.wm_overrideredirect(True) 
       top.attributes("-topmost", True)
       play = Button(top, text="Okay", command = top.destroy)
       play.pack()
       top.mainloop()



RE: Lost Control over VLC - deanhystad - Jul-18-2023

mainloop() blocks your program from running until the last window is destroyed. You should not put mainloop() in your sub_popup() function if there is a mainloop() elsewhere in your program (There can only be one).


RE: Lost Control over VLC - jrockow - Jul-18-2023

(Jul-18-2023, 03:20 PM)deanhystad Wrote: mainloop() blocks your program from running until the last window is destroyed. You should not put mainloop() in your sub_popup() function if there is a mainloop() elsewhere in your program (There can only be one).

Thanx for your response.
When I remove the mainloop the popup doesn't display at all.

The mainloop statement is not in my full script, but I still have the same problem.
I will try to upload the script.


RE: Lost Control over VLC - deanhystad - Jul-18-2023

Is that the only time you call mainloop() in your program? Is that the only tkinter window? What is my_canvas?


RE: Lost Control over VLC - jrockow - Jul-18-2023

(Jul-18-2023, 05:07 PM)deanhystad Wrote: Is that the only time you call mainloop() in your program? Is that the only tkinter window? What is my_canvas?

I only call mainloop at the end of the script.
There are several tkinter windows.
I'm trying to send you the full script, but having a problem.

OK, maybe it's there now?


RE: Lost Control over VLC - jrockow - Jul-18-2023

It just dawns on me that you may need my SQL.DB to test the code.
Currently it's around 14MB, but I could probably delete a lot of the records if you need it.
What is the maximum size I can attach?

I just deleted about half of the records and the size is still 14MB.


RE: Lost Control over VLC - deanhystad - Jul-18-2023

Have you written tkinter programs before? They have a pattern that you need to follow, a pattern very different than a script.


RE: Lost Control over VLC - deanhystad - Jul-18-2023

You have a problem with the design of your program. tkinter.mainloop() blocks other code from running. You also have a loop that looks for key presses that blocks other code from running. Somehow you need to get these to work together.

I think I would move this loop:
       Ended = 6#set arbitrary value
       current_state = media_player.get_state()
       while current_state != Ended:
            ...
To a function that I would call periodically using root.after(milliseconds, func_to_call). Another idea is use event binding to direct key presses to some function. You are already doing this with some key presses, but not all. And there is always multi-threading. Using multi-threading you could set up a keyboard listener that runs in one thread and have tkinter run in another thread. Getting the two threads to work together can be tricky.


RE: Lost Control over VLC - jrockow - Jul-18-2023

(Jul-18-2023, 05:53 PM)deanhystad Wrote: I'm pretty sure you can make a test case that does not require a database. If you could just add the code that sets up the keyboard listener and creates the root window (if there is one), that would go a long way toward making something others can run.

Hopefully you got the full script I attached?
I am a complete novice with Python and Tkinter, and have been teaching myself thru reading and trial and error.
My program has come a long way, but I'm stuck now with this one problem.
I just can't resume my VLC session after calling the popup.