【发布时间】:2021-07-16 05:37:56
【问题描述】:
import pyglet, pafy
from pyglet.window import key
import urllib.request
from urllib.parse import *
from googleapiclient.discovery import build
api_key = ''
yt = build('youtube', 'v3', developerKey=api_key)
def download_file(search):
try:
os.remove("song.m4a")
except:
pass
request = yt.search().list(
part="snippet",
maxResults=1,
q=search
)
response = request.execute()
items = response['items']
item = items[0]
snippet = item['snippet']
title = snippet['title']
id_ = item["id"]
videoid = id_["videoId"]
url = "https://www.youtube.com/watch?v=" + videoid
info = pafy.new(url)
audio = info.getbestaudio(preftype="m4a")
audio.download('song.m4a', quiet=True)
file = 'song.m4a'
#if len(sys.argv)>1:
# file = sys.argv[1]
#
window = pyglet.window.Window()
#music = pyglet.resource.media(file)
player = pyglet.media.Player()
#player.queue(music)
#player.play()
paused = False
def help():
print("""\nCommands:
\tEsc or x \t Exit program
\tp \t Pause/unpause music
\th \t See this list again""")
print("""Welcome to this music player!
You can give a file as an argument or use the commands below.""")
help()
@window.event
def on_key_press(symbol, modifiers):
global paused
global player
global window
file = 'song.m4a'
if symbol == key.P:
if paused:
print("Resume")
player.play()
paused = False
else:
print("Pause")
player.pause()
paused = True
elif symbol == key.R:
pass
#Doesn't work :P
#player.seek(0)
elif symbol == key.H:
help()
elif symbol == key.ESCAPE:
try:
os.remove("song.m4a")
except:
pass
window.close()
elif symbol == key.Q:
srch = input("Queue Song: ")
download_file(srch)
music = pyglet.media.load(file)
player.queue(music)
#music.play()
player.next_source()
print(f'Added to queue.')
elif symbol == key.S:
srch = input("Play Song: ")
download_file(srch)
music = pyglet.media.load(file)
#music.play()
player.queue(music)
player.next_source()
pyglet.app.run()
pyglet.app.exit()
所以我在 Pyglet 库中遇到了一些问题。我似乎无法让这部分工作。我打电话给player.next_source() 正确吗?因为我刚刚运行symbol == key.S 为srch 输入了一个值,但没有播放任何内容。我的目标是做到这一点,这样当我按下q 时输入的输入将使歌曲排队并在当前歌曲之后自动播放,而按下s 并在那里输入一个值将覆盖队列并对其进行优先排序。一些帮助将不胜感激。
【问题讨论】:
标签: python audio-player pyglet