gpt4 book ai didi

python - 使用gstreamer,在不停止接收器的情况下播放播放列表

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:04 24 4
gpt4 key购买 nike

我想向我的音乐播放器添加播放列表功能。播放列表中的第一首轨道。在控制台中输入“下一个”并按回车键应该开始播放下一首轨道,但歌曲停止播放并且没有任何反应。

将状态设置为GST_STATE_READY而不是之前的GST_STATE_NULL更改“位置”也不起作用。

有人可以纠正我的代码并告诉我哪里错了吗?

import pygst, gobject, time, sys
pygst.require("0.10")
import gst

class AudioPlayer:
def __init__(self):
self.songs = ['Holy Diver.mp3','Paranoid.mp3','Fast as a Shark.mp3']

# create a new gstreamer pipeline
self.pipeline = gst.Pipeline("mypipeline")

# add a file source to the pipeline
self.filesrc = gst.element_factory_make("filesrc","source")
self.pipeline.add(self.filesrc)

# add a generic decoder to the pipeline and link it to thesource
self.decode = gst.element_factory_make("decodebin","decode")
self.decode.connect("new-decoded-pad", self.decode_link)
self.pipeline.add(self.decode)
self.filesrc.link(self.decode)

# add a convertor to the pipeline
self.convert = gst.element_factory_make("audioconvert","convert")
self.pipeline.add(self.convert)

# add an alsa sink to the pipeline and link it to theconvertor
self.sink = gst.element_factory_make("alsasink", "sink")
self.pipeline.add(self.sink)
self.convert.link(self.sink)

# start playing
self.filesrc.set_property("location", self.songs.pop(0))
self.pipeline.set_state(gst.STATE_PLAYING)

def decode_link(self, dbin, pad, islast):
pad.link(self.convert.get_pad("sink"))

def next(self):
self.convert.unlink(self.sink)
self.filesrc.set_state(gst.STATE_NULL)
self.filesrc.set_property("location", self.songs.pop(0))
self.convert.link(self.sink)
self.pipeline.set_state(gst.STATE_PLAYING)
return True

player = AudioPlayer()
loop = gobject.MainLoop()
gobject.threads_init()
context = loop.get_context()

while 1:
value = sys.stdin.readline()
if value == "next\n":
player.next()
context.iteration(True)

最佳答案

接下来您将错误的内容设置为 NULL:
self.filesrc.set_state(gst.STATE_NULL)

应该是
self.pipeline.set_state(gst.STATE_NULL)

这是问题的症结所在,您没有停止管道,但也不需要取消链接和重新链接 self.convert,但这是一个附带问题。

关于python - 使用gstreamer,在不停止接收器的情况下播放播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com