Home > OS >  Tkinter Video Playlist
Tkinter Video Playlist

Time:01-10

Trying to build a Playlist of videos using Tkinter. At the moment I have an array of mp4, names that will pass into the tkintervideo to load.

I'm having issues on how to wait for the first video to end in order to go to the second.

root = Tk()    
playlist = ["New_Year_Champagne.mp4","demo.mp4","car.mp4"]
videoplayer = TkinterVideo(master=root, scaled=True, pre_load=False)
for item in playlist: 
    has_video_ended = False

    videoplayer.load(item)
    videoplayer.pack(expand=True, fill="both")
    videoplayer.play() # play the video
    videoplayer.bind("<<Ended>>", video_ended )
    
    root.update()
      
root.mainloop()

As it is, after doing the root.update() it goes to the next iteration of the playlist.

What else can I try?

CodePudding user response:

The problem is that you load all your videos immediately before any of the videos ends. You should load the next video in the <<Ended>> handler.

Here is a simple example:

import functools
import tkinter as tk
from tkVideoPlayer import TkinterVideo

def play_next_video(event, videoplayer, playlist):
    next_video = next(playlist, None)
    print('Next is:', next_video)
    if not next_video:
        return

    # Play new video only after end of current
    videoplayer.load(next_video)
    videoplayer.play()

if __name__ == "__main__":
    root = tk.Tk()
    videoplayer = TkinterVideo(master=root, scaled=True, pre_load=False)

    # Use 'iter' to work with 'next' in callback
    playlist = iter(["test.mp4", "videoplayback.mp4"])

    # After end of video, use callback to run next video from playlist
    videoplayer.bind(
        "<<Ended>>",
        functools.partial(
            play_next_video,
            videoplayer=videoplayer,
            playlist=playlist,
        ),
    )

    # Play first video from playlist
    videoplayer.load(next(playlist))
    videoplayer.pack(expand=True, fill="both")
    videoplayer.play()

    root.mainloop()
  •  Tags:  
  • Related