python-3.x Tkinter视频播放列表

esyap4oy  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(135)

尝试使用Tkinter构建视频播放列表。目前我有一个mp4数组,名称将传递到tkintervideo加载。
我有问题,如何等待第一个视频结束,以进入第二个。

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()

字符串
实际上,在执行root.update()之后,它将进入播放列表的下一次迭代。
我还能尝试什么?

uyhoqukh

uyhoqukh1#

问题是你在所有视频结束之前立即加载了所有视频。你应该在<<Ended>>处理程序中加载下一个视频。
下面是一个简单的例子:

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()

字符串

相关问题