Python VLC绑定-播放播放列表 [英] Python VLC binding- playing a playlist

查看:536
本文介绍了Python VLC绑定-播放播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering if it is possible to open(play) a music playlist ( .m3u file) with the use of vlc.py ? I searched for an answer but could not find it. I managed to play a simple mp3 file, even a mp3 stream, but I dont have any luck with the playlists. Can you help me, give me some example code? I want to be able to go through tracks( Next and Previous) within my python program. Thans in advance

推荐答案

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

Here is a "very" rough mock up of some code that I wrote for something else, adapted to your question.
It should allow you, using vlc.py, to play streamed audio, an m3u audio playlist and an mp3 file.
As I said it is very rough code but it should point you in the right direction.
Hope it helps.

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')

这篇关于Python VLC绑定-播放播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆