[英]How to define fast forward button in pygame using python?
我制作了一個音樂播放器,我想在其中制作fastforward
按鈕(如果點擊fastforward
按鈕,則當前播放的歌曲將增加 10 秒),但我無法正確定義它。 我也嘗試了 pygame 的set_pos()
方法,但它不適用於我的腳本。
請幫我解決這個問題
這是我的代碼:
fast_forward_icon= tk.PhotoImage(file=__file__+ '/../images/JPEG, PNG/icons/fast_forward.png')
def fast_forward(event=None):
def func():
global progressbar_music
current_song_Length = mixer.music.get_pos()//1000
print(current_song_Length)
# mixer.music.pause()
f= current_song_Length+10
mixer.music.set_pos(f)
progressbar_music['value']=f
# mixer.music.progressbar_music(seconds=current_song_Length+10)
progressbar_music_starttime.configure(text='{}'.format(str(datetime.timedelta(seconds=f))))
progressbar_music.after(2,func)
func()
fastForwardBtn = Button(root, bg='#310A3D', image=fast_forward_icon, command=fast_forward)
fastForwardBtn.place(x=600, y=500, relwidth=.04, relheight=.08)
set_pos()
也會給我這樣的錯誤
Exception in Tkinter callback Traceback (most recent call last):
File "C:\Users\soham\AppData\Local\Programs\Python\Python36\lib\tkinter_init_.py",
line 1702, in call return self.func(*args) File "C:\Users\soham\AppData\Local\Programs\Python\Python36\lib\tkinter_init_.py",
line 746, in callit func(*args) File "e:/python projects/MUSIC PLAYER/music_player.py",
line 346, in func mixer.music.set_pos(f) pygame.error:
set_pos unsupported for this codec
我解決了你的任務。
主要問題不是由於 MP3 編解碼器問題,而是因為您的音樂應該加載/播放/暫停以便set_pos()
工作,即在快進之前您需要下一行額外的行:
mixer.music.load('test.mp3')
mixer.music.play()
mixer.music.pause()
只有這樣你才能使用快進,它的工作原理!
您還需要稍后執行mixer.music.unpause()
以便從您的快進位置(由set_pos()
設置set_pos()
開始播放音樂。 此外,快進也可以在沒有pause()
/ unpause()
,只是應該播放音樂,所以如果現在沒有播放,應該調用play()
。
下面是完整的工作代碼,為了使示例完全可運行,它有一些其他修改,但其余的修改不是解決您的任務所必需的。 主要修改之一是我實現了用於加載 MP3/PNG 資源的函數get_url_file(...)
,因此該示例完全獨立,無需額外文件即可運行,因為 StackOverflow 不允許附加文件,因此我加載它們來自遠程 HTTP 存儲。 在我使用get_url_file(...)
地方只使用像path/to/test.mp3
這樣的文件路徑。
同樣在運行下一個代碼之前,通過python -m pip install --upgrade pgzero pygame<2.0 requests
一次安裝 python pip 模塊。
# Needs: python -m pip install --upgrade pgzero pygame<2.0 requests
import tkinter as tk, datetime, pgzrun
from pygame import mixer
root = tk.Tk()
def fast_forward(event = None):
def func():
global progressbar_music
mixer.music.play()
mixer.music.pause()
current_song_Length = mixer.music.get_pos() // 1000
print(current_song_Length)
# mixer.music.pause()
f = current_song_Length + 10
mixer.music.set_pos(f)
print('{}'.format(str(datetime.timedelta(seconds = f))))
mixer.music.unpause()
#progressbar_music['value'] = f
# mixer.music.progressbar_music(seconds=current_song_Length+10)
#progressbar_music_starttime.configure(text = '{}'.format(str(datetime.timedelta(seconds = f))))
#progressbar_music.after(2, func)
func()
def get_url_file(url, *, fsuf = '', as_ = 'bytes', b64 = False, state = {}):
import requests, io, atexit, base64, tempfile, shutil, secrets, os
res = requests.get(url)
res.raise_for_status()
data = res.content
if b64:
data = base64.b64decode(data)
if as_ == 'bytes':
return data
elif as_ == 'file':
return io.BytesIO(data)
elif as_ == 'path':
if 'td' not in state:
state['td'] = tempfile.TemporaryDirectory()
state['etd'] = state['td'].__enter__()
state['std'] = str(state['etd'])
def cleanup():
state['td'].__exit__(None, None, None)
if os.path.exists(state['std']):
shutil.rmtree(state['std'])
atexit.register(cleanup)
path = state['std'] + '/' + secrets.token_hex(8).upper() + fsuf
with open(path, 'wb') as f:
f.write(data)
return path
else:
assert False, as_
mixer.music.load(get_url_file('https://pastebin.com/raw/8LAeZc1X', as_ = 'file', b64 = True))
fast_forward_icon = tk.PhotoImage(file = get_url_file('https://i.stack.imgur.com/b2wdR.png', as_ = 'path'))
fastForwardBtn = tk.Button(root, bg = '#310A3D', image = fast_forward_icon, command = fast_forward)
fastForwardBtn.place(x = 10, y = 10, relwidth = .7, relheight = .7)
tk.mainloop()
pgzrun.go()
打印到控制台:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
0
0:00:10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.