我有这段代码没有问题:
Media_list = instance.media_list_new(song_list)
list_player = instance.media_list_player_new()
list_player.set_media_list(Media_list)
list_player.play()
我怎么想通过列表进行迭代,并使用普通的vlc播放器来播放它。
playing = set([1,2,3,4])
for i in song_list:
player.set_mrl(i)
player.play()
play=True
while play == True:
time.sleep(1)
play_state = player.get_state()
if play_state in playing:
continue
else:
play = False
这几乎是一样的,它更适合我的需要,但它冻结了我的GUi,(qml / pyside2)。所以现在我被合并了,我应该为此创建一个新线程,或者在vlc中还有其他方法可以做到这一点。
我确实尝试创建新线程并在其中运行上面的函数,但同样的问题,当玩家进入循环并开始播放方法时,gui冻结。(vlc正常工作,播放播放列表,但gui对持续时间没有反应)
所以只是为了扩展一点,这是我的部分,它工作正常,但我不能在他们的播放时间从我的歌曲中获取数据,因为我拥有的只是网址,而不是元数据。
song_list=[]
r = requests.get('https://www.youtube.com/playlist?list=PLD6s0l-FZhjkc-TYwXO5GbwyxFqTd5Y9J')
page = r.text
soup=bs(page,'html.parser')
res=soup.find_all('a',{'class':'pl-video-title-link'})
for l in res:
#print (l.get("href"))
#print("https://www.youtube.com"+l.get("href"))
yt ='https://www.youtube.com'
temp =l.get("href")
url =yt+temp
video = pafy.new(url)
bestaudio = video.getbestaudio()
song = bestaudio.url
#print(video.getbestaudio().url)
song_list.append(song)
Media_list = instance.media_list_new(song_list)
list_player = instance.media_list_player_new()
list_player.set_media_list(Media_list)
list_player.play()
我想要的是:
@Slot()
def print_yt_playlist(self):
song_list=[]
r = requests.get('https://www.youtube.com/playlist?list=PLD6s0l-FZhjkc-TYwXO5GbwyxFqTd5Y9J')
page = r.text
soup=bs(page,'html.parser')
res=soup.find_all('a',{'class':'pl-video-title-link'})
for l in res:
#print (l.get("href"))
#print("https://www.youtube.com"+l.get("href"))
yt ='https://www.youtube.com'
temp =l.get("href")
url =yt+temp
video = pafy.new(url)
bestaudio = video.getbestaudio()
song = bestaudio.url
#print(video.getbestaudio().url)
song_list.append(video)
playing = set([1,2,3,4])
for i in song_list:
media = instance.media_new(i.getbestaudio().url)
print(i.Artist) #THIS is what i want, i want to be able to acces that data for the song that is playing
print(i.Duration) #and this and so on, that is why i want to loop through list, since i dont think i can do it with media_list
player.set_media(media)
player.play()
play=True
while play == True:
time.sleep(1)
play_state = player.get_state()
if play_state in playing:
continue
else:
play = False
或者更简单,有没有办法粘贴"视频"进入media_list,然后从那里我可以访问有关当前歌曲的数据,以及播放歌曲吗?
我不知道从qml方面可以帮助你的是什么,我唯一能做的就是点击按钮来触发这个功能。
答案 0 :(得分:0)
嗯,我花了一点时间,我有一个解决方案,它仍处于“粗糙”的状态,但它的工作原理,当我使用它时它不会阻止gui。 我把这个逻辑放在一个新的线程中,我从那里调用它,需要做很多调整。 我不知道这是否是最“优雅”的方法,所以如果其他人有更好的想法,请不要犹豫。
class Threaddy(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
song_list=[]
r = requests.get('https://www.youtube.com/playlist?list=PLD6s0l-FZhjkc-TYwXO5GbwyxFqTd5Y9J')
page = r.text
soup=bs(page,'html.parser')
res=soup.find_all('a',{'class':'pl-video-title-link'})
for l in res:
#print (l.get("href"))
#print("https://www.youtube.com"+l.get("href"))
yt ='https://www.youtube.com'
temp =l.get("href")
url =yt+temp
video = pafy.new(url)
bestaudio = video.getbestaudio()
song = bestaudio.url
#print(video.getbestaudio().url)
song_list.append(video)
for song in song_list:
media=instance.media_new(song.getbestaudio().url) #THIS WORKS NOW
media.get_mrl()
player.set_media(media)
player.play()
print(song.title) #SO DOES THIS
playing = set([1,2,3,4])
time.sleep(1)
duration = player.get_length() / 1000
mm, ss = divmod(duration, 60)
while True:
state = player.get_state()
if state not in playing:
break
continue