我在VLC的捕获异常方面有问题。而不是打印文本,我会得到以下信息:
from vlc import MediaPlayer
x = MediaPlayer('recording.mp3')
def play_sound():
try:
x.play()
except:
print('Cannot play sound.')
play_sound()
如何修复?
在
Https://www.reddit.com/r/learlenprogramming/comments/fgymq4/import_vlc_error_python/
上打开代码登录,但没有一个可用于me.
instead,启动VLC和管道标准错误,使用
your_python_script.py &> yourlog_file.txt
然后使用
watchDog观察该文件并采取行动。不好。比我能找到的其他任何东西都要好。
我正在使用以下方式使用VLCS Event_Manager回调功能来本机本地播放错误。这是我的工作代码:
def callback_from_player(self, event: vlc.Event, *args):
logging.debug('callback called: %s, from %s', event.type, args[0])
# continue with your code here, I am starting a new thread at this point.
# one cannot call vlc library functions within the event callback function.
def play(self):
instance: vlc.Instance = vlc.Instance()
list_player: vlc.MediaListPlayer = instance.media_list_player_new()
list_player.set_playback_mode(vlc.PlaybackMode.loop)
media_player: vlc.MediaPlayer = list_player.get_media_player()
# the crutial line:
media_player.event_manager().event_attach(
vlc.EventType.MediaPlayerEncounteredError,
self.callback_from_player, "media_player")
media: vlc.Media = instance.media_new(self.url)
media_list: vlc.MediaList = instance.media_list_new([])
list_player.set_media_list(media_list)
media_list.add_media(media)
list_player.play()