使用python-vlc模块控制VLC播放器

时间:2019-08-23 09:11:04

标签: python vlc python-vlc

使用python-vlc模块控制VLC媒体播放器

我尝试了以下代码,但收到错误消息:

  

回溯(最近一次通话最后一次):文件“”,第1行,在   

import vlc   File "vlc.py", line 2, in <module>
# -*- coding: utf-8 -*-
     

AttributeError:“模块”对象没有属性“ MediaPlayer”

使用代码:

import vlc
    media_player = vlc.MediaPlayer("path_to_your_song.mp3")
    media_player.play()

我希望脚本运行并播放文件

2 个答案:

答案 0 :(得分:2)

只需更改路径,您就可以开始使用了。

from vlc import Instance
import time
import os

class VLC:
    def __init__(self):
        self.Player = Instance('--loop')

    def addPlaylist(self):
        self.mediaList = self.Player.media_list_new()
        path = r"C:\Users\dell5567\Desktop\engsong"
        songs = os.listdir(path)
        for s in songs:
            self.mediaList.add_media(self.Player.media_new(os.path.join(path,s)))
        self.listPlayer = self.Player.media_list_player_new()
        self.listPlayer.set_media_list(self.mediaList)
    def play(self):
        self.listPlayer.play()
    def next(self):
        self.listPlayer.next()
    def pause(self):
        self.listPlayer.pause()
    def previous(self):
        self.listPlayer.previous()
    def stop(self):
        self.listPlayer.stop()

创建对象

player = VLC()

添加播放列表

player.addPlaylist()

播放歌曲

player.play()
time.sleep(9)

播放下一首歌曲

player.next()
time.sleep(9)

暂停歌曲

player.pause()
time.sleep(9)

恢复歌曲

player.play()
time.sleep(9)

上一曲

player.previous()
time.sleep(9)

停止播放歌曲

player.stop()

答案 1 :(得分:1)

您需要创建一个vlc Instance
所需的最低要求将是这样,但是有很多变化。

>>> import vlc
>>> i = vlc.Instance()
>>> media_player = i.media_player_new()
>>> media_player.set_mrl('./vp1.mp3')
>>> media_player.play()