【问题标题】:Python VLC binding- playing a playlistPython VLC 绑定——播放列表
【发布时间】:2015-04-11 00:26:59
【问题描述】:

我想知道是否可以打开(播放)音乐播放列表 ( .m3u 文件)与 vlc.py 的使用?我搜索了答案,但找不到。我设法播放了一个简单的 mp3 文件,甚至是一个 mp3 流,但我对播放列表没有任何运气。你能帮我,给我一些示例代码吗?我希望能够在我的 python 程序中浏览曲目(下一个和上一个)。比提前

【问题讨论】:

  • 您将不得不编写自己的播放列表系统或找到一个已经可用的系统。
  • 我自己的系统是什么意思? VLC 播放器(带 GUI)可以播放 .m3u 文件并转到下一个曲目,上一个曲目...我想知道如何使用 vlc.py 模块来完成...
  • @NatkoKraševac 你有没有让这个工作?

标签: python mp3 vlc playlist audio-player


【解决方案1】:

这是我为其他内容编写的一些代码的“非常”粗略模型,适用于您的问题。
它应该允许您使用 vlc.py 播放流式音频、m3u 音频播放列表和 mp3 文件。
正如我所说,这是非常粗略的代码,但它应该为您指明正确的方向。
希望能帮助到你。

import requests
import vlc
from time import sleep
urls = [
    'http://network.absoluteradio.co.uk/core/audio/aacplus/live.pls?service=acbb',
    'file:///home/rolf/test.m3u',
    'file:///home/rolf/happy.mp3',
    'http://statslive.infomaniak.ch/playlist/energy90s/energy90s-high.mp3/playlist.pls',
    'http://streaming.radio.rtl2.fr/rtl2-1-44-128',
    ]

playlists = set(['pls','m3u'])

Instance = vlc.Instance()

for url in urls:
    ext = (url.rpartition(".")[2])[:3]
    test_pass = False    
    try:
        if url[:4] == 'file':
            test_pass = True
        else:
            r = requests.get(url, stream=True)
            test_pass = r.ok
    except Exception as e:
        print('failed to get stream: {e}'.format(e=e))
        test_pass = False
    else:
        if test_pass:
            print('Sampling for 15 seconds')
            player = Instance.media_player_new()
            Media = Instance.media_new(url)
            Media_list = Instance.media_list_new([url])
            Media.get_mrl()
            player.set_media(Media)
            if ext in playlists:
                list_player = Instance.media_list_player_new()
                list_player.set_media_list(Media_list)
                if list_player.play() == -1:
                    print ("Error playing playlist")
            else:
                if player.play() == -1:
                    print ("Error playing Stream")
            sleep(15)
            if ext in playlists:
                list_player.stop()
            else:
                player.stop()

        else:
            print('error getting the audio')

【讨论】:

    【解决方案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()
    

    【讨论】:

    • 完美运行,谢谢!
    猜你喜欢
    • 2013-05-13
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多