15

在使用 VLC Python 绑定时,我无法附加事件处理程序来判断歌曲何时播放完毕。event_attach 函数抱怨回调类型。

def __init__(self):
    self.vlc = vlc.Instance()
    self.vlc_playlist = self.vlc.media_list_new()
    self.vlc_player = self.vlc.media_list_player_new()
    self.vlc_player.set_media_list(self.vlc_playlist)
    self.vlc_events = self.vlc_player.event_manager()
    self.vlc_events.event_attach(vlc.EventType.MediaPlayerEndReached, self.SongFinished, 1)
    ....
def SongFinished(self, *args, **kwargs):
    pass

错误:

self.vlc_events.event_attach(vlc.EventType.MediaPlayerEndReached, self.SongFinished, 1)
return libvlc_event_attach(self, i_event_type, f_callback, user_data)
    ArgumentError: argument 3: <type 'exceptions.TypeError'>: wrong type
4

2 回答 2

13

这是最近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的代码中搜索

于 2016-04-23T14:49:00.877 回答
10

找到它,VLC.py 包含一个用于包装回调的小型装饰器 ctypes 函数:

callbackmethod=ctypes.CFUNCTYPE(None, Event, ctypes.c_void_p)

要使用:

@callbackmethod
def SongFinished(self, data):
    print data

.event_attach(vlc.EventType.MediaPlayerEndReached, SongFinished, 1)

于 2010-08-30T19:45:43.850 回答