这是最近vlc.py
使用的基本代码event_manager
:
import vlc
import time
import sys
finish = 0
def SongFinished(event):
global finish
print("\nEvent reports - finished")
finish = 1
def pos_callback(event, player):
sec = player.get_time() / 1000
m, s = divmod(sec, 60)
npos = event.u.new_position * 100
sys.stdout.write('\r%s %02d:%02d (%.2f%%)' % ('Position', m, s, npos))
sys.stdout.flush()
instance = vlc.Instance()
player = instance.media_player_new()
media = instance.media_new_path('vp1.mp3') #Your audio file here
player.set_media(media)
events = player.event_manager()
events.event_attach(vlc.EventType.MediaPlayerEndReached, SongFinished)
events.event_attach(vlc.EventType.MediaPlayerPositionChanged, pos_callback, player)
player.play()
while finish == 0:
time.sleep(0.5)
注意:媒体播放器有不少事件可以通过这种方式进行监控。
MediaPlayerMediaChanged
MediaPlayerNothingSpecial
MediaPlayerOpening
MediaPlayerBuffering
MediaPlayerPlaying
MediaPlayerPaused
MediaPlayerStopped
MediaPlayerForward
MediaPlayerBackward
MediaPlayerEndReached
MediaPlayerEncounteredError
MediaPlayerTimeChanged
MediaPlayerPositionChanged
MediaPlayerSeekableChanged
MediaPlayerPausableChanged
MediaPlayerTitleChanged
MediaPlayerSnapshotTaken
MediaPlayerLengthChanged
MediaPlayerVout
MediaPlayerScrambledChanged
MediaPlayerESAdded
MediaPlayerESDeleted
MediaPlayerESSelected
MediaPlayerCorked
MediaPlayerUncorked
MediaPlayerMuted
MediaPlayerUnmuted
MediaPlayerAudioVolume
MediaPlayerAudioDevice
class EventType
有关完整的当前列表,请在https://github.com/oaubert/python-vlc/blob/master/generated/3.0/vlc.py的代码中搜索