📌  相关文章
📜  Python VLC MediaList – 获取给定项目的索引

📅  最后修改于: 2022-05-13 01:55:06.719000             🧑  作者: Mango

Python VLC MediaList – 获取给定项目的索引

在本文中,我们将看到如何在Python vlc 模块的 MediaList 对象中获取给定媒体对象的索引。 VLC媒体播放器是VideoLAN项目开发的一款免费开源的便携式跨平台媒体播放软件和流媒体服务器。 MediaList 对象包含多个媒体,换句话说,它具有可以与 MediaListPlayer 对象一起使用来播放这些多个媒体对象的媒体列表。借助add_media方法可以将多个媒体添加到媒体列表对象中,因此每个媒体都位于列表中相应的索引处。我们可以借助item_at_index方法从索引中获取媒体。

下面是实现

# importing vlc module
import vlc
  
# importing time module
import time
  
# creating a media player object
media_player = vlc.MediaListPlayer()
  
# creating Instance class object
player = vlc.Instance()
  
# creating a media list object
media_list = vlc.MediaList()
  
# creating a new media
media1 = player.media_new("death_note.mkv")
  
# adding media to media list
media_list.add_media(media1)
  
# creating a new media
media2 = player.media_new("1.mp4")
  
# adding another media
media_list.add_media(media2)
  
  
# setting media list to the media player
media_player.set_media_list(media_list)
  
  
# start playing video
media_player.play()
  
# wait so the video can be played for 5 seconds
# irrespective for length of video
time.sleep(5)
  
# getting index of media in media list
value = media_list.index_of_item(media1)
  
# printing value
print(value)

输出 :

0

另一个例子

# importing vlc module
import vlc
  
# importing time module
import time
  
# creating a media player object
media_player = vlc.MediaListPlayer()
  
# creating Instance class object
player = vlc.Instance()
  
# creating a media list object
media_list = vlc.MediaList()
  
# creating a new media
media1 = player.media_new("1.mp4")
  
# adding media to media list
media_list.add_media(media1)
  
# creating a new media
media2 = player.media_new("death_note.mkv")
  
# adding media to media list
media_list.add_media(media2)
  
  
  
# setting media list to the media player
media_player.set_media_list(media_list)
  
  
# start playing video
media_player.play()
  
# wait so the video can be played for 5 seconds
# irrespective for length of video
time.sleep(5)
  
# getting index of media in media list
value = media_list.index_of_item(media2)
  
# printing value
print(value)

输出 :

1