0

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

我已经尝试了下面的代码,但出现了错误:

Traceback(最近一次调用最后一次):文件“”,第 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()

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

4

2 回答 2

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()
于 2020-06-22T07:04:58.447 回答
1

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

>>> import vlc
>>> i = vlc.Instance()
>>> media_player = i.media_player_new()
>>> media_player.set_mrl('./vp1.mp3')
>>> media_player.play()
于 2019-08-23T16:53:22.793 回答