我正在使用python中的pyglet包为音乐播放器编写代码。我可以成功地播放一首歌。但是在打电话给pyglet.app.run()
之后我无法暂停这首歌。
如果我在呼叫暂停后拨打pyglet.app.run()
我无法播放该歌曲。如何在代码中管理播放和暂停?
我的代码:
import pyglet
import time
player = pyglet.media.Player()
player.queue(pyglet.resource.media('1.mp3'))
player.play()
pyglet.app.run()
time.sleep(5)
player.pause()
print player.time()
答案 0 :(得分:1)
#This should play the video then pause after 12 seconds of
# play. Stay paused for 4 seconds and play again.
#Note AVbin7 needs to be installed to play mp4s
import pyglet
from time import sleep
vidPath="/home/path/to/file.mp4"
window = pyglet.window.Window()
player = pyglet.media.Player()
source = pyglet.media.StreamingSource()
MediaLoad = pyglet.media.load(vidPath)
pt = 0
player.queue(MediaLoad)
player.play()
@window.event
def on_draw():
global pt
window.clear()
if player.source and player.source.video_format:
player.get_texture().blit(0,0)
if player.time >= 12 and pt == 0:
print("pausing")
player.pause()
sleep(4)
player.play()
pt = 1
pyglet.app.run()
答案 1 :(得分:0)
pyglet.app.run()
此函数是无限循环。你可以创建一个新的线程,你可以睡觉。
class ControllerThread(threading.Thread):
def __init__(self, player):
threading.Thread.__init__(self)
self.player = player
def run(self):
time.sleep(10)
self.player.pause()
player = pyglet.media.Player()
pyglet.resource.path = [r'path/to/music']
pyglet.resource.reindex()
music = pyglet.resource.media(r'awesomeSong.mp3')
player.queue(music)
player.play()
c = ControllerThread(player)
c.start()
pyglet.app.run()
答案 2 :(得分:0)
此代码应该适合您
import pyglet
import threading
import time
class ControllerThread(threading.Thread):
def __init__(self, player):
threading.Thread.__init__(self)
self.player = player
def run(self):
self.player.pause()
player = pyglet.media.Player()
pyglet.resource.path = [r'/your/source/path/']
pyglet.resource.reindex()
def muz():
music = pyglet.resource.media(r'file.mp3')
player.queue(music)
player.play()
t=threading.Thread(target=muz)
a=''
while a!=' ':
a=raw_input("STOP/Play S/P: ")
if a=='s':
c = ControllerThread(player)
c.start()
elif a=='p':
if t.isAlive():
player.play()
else:
t=threading.Thread(target=muz)
t.start()
pyglet.app.run()
答案 3 :(得分:0)
以下示例(python 3.7,pyglet 1.5.6)使用SPACE暂停/恢复视频播放:
#create a new discount code
discount_code = ShopifyAPI::DiscountCode.new
discount_code.prefix_options = {price_rule_id: @price_rule.id}
discount_code.code = @price_rule.title
discount_code.save