我试图使用vlc包播放存储在Firebase Storage中的音乐。然而,在使用这些软件包时,我们需要定义睡眠的持续时间。当我使用urllib.request和诱变剂库检索时发生错误。
错误
can't sync to MPEG frame
因此,从注释中可以看出:与其尝试使用Mutagen,不如向VLC询问持续时间。
self.mediaPlayer = vlc.MediaPlayer(musicURL) self.mediaPlayer.play() duration = self.mediaPlayer.get_length() print(f"Playing {musicName}") time.sleep(duration / 1000) # duration is in milliseconds
为了澄清,我已经设法解决了这个问题,下面是我的代码:
try: self.mediaPlayer = vlc.MediaPlayer(musicURL) self.mediaPlayer.play() time.sleep(3) duration = self.mediaPlayer.get_length() self.musicLength = duration/1000 except: pass print(self.musicLength) self.mediaPlayer.play() time.sleep(self.musicLength) print("Music " + musicName + " is playing")
我有一种不同的方法/实现,您可以使用VLC,但不必调用
media_player.play()
。它不是异步的,但它几乎是即时的。def get_audio_duration(audio_path): # Get audio duration in ms using vlc # Works with vlc.__version__ 3.0.1812 # audio_path: <str> path to an audio file # return: <int> audio duration in ms media = vlc.Media(audio_path) media_player = vlc.MediaPlayer() media_player.set_media(media) media.parse_with_options(1, 0) while media.get_duration() < 0: continue return media.get_duration()