網(wǎng)站首頁 編程語言 正文
1. 需要用到的Python庫
- pygame
- tkinter
2. 簡易UI設(shè)計(jì)
audio_player = Tk() audio_player.title('Audio Player v1.0') audio_player.geometry('100x100+570+200') audio_player.maxsize(height=110, width=220) audio_player.minsize(height=110, width=220)
3. 功能模塊實(shí)現(xiàn)
3.1 選擇音頻文件進(jìn)行播放
def selectFile(): ? ? file = filedialog.askopenfile(mode='r', filetypes=[('AudioFile', '*.mp3')]) ? ? global filePath ? ? filePath = str(file).split("'")[1] ? ? try: ? ? ? ? playAudio() ? ? except: ? ? ? ? pass
3.2 控制音頻播放、暫停
def changeText(text): ? ? if text == 'play': ? ? ? ? return 'pause' ? ? if text == 'pause': ? ? ? ? return 'play' def playStop(): ? ? playBtn.config(text=changeText(playBtn.config('text')[4])) ? ? if playBtn.config('text')[4] == 'pause': ? ? ? ? mixer.music.unpause() ? ? else: ? ? ? ? if playBtn.config('text')[4] == 'play': ? ? ? ? ? ? mixer.music.pause()
3.3 控制音頻音量大小
這里可以定義一個(gè)全局變量x,初始化為值0.5。
def audioINC(y): ? ? mixer.music.set_volume(y + 0.1) ? ? global x ? ? x += 0.1 def audioDEC(y): ? ? mixer.music.set_volume(y - 0.1) ? ? global x ? ? x -= 0.1
3.4 播放器初始化等細(xì)節(jié)
def playAudio(): ? ? try: ? ? ? ? mixer.init() ? ? ? ? mixer.music.load(filePath) ? ? ? ? mixer.music.set_volume(x) ? ? ? ? playBtn.config(text='pause') ? ? ? ? mixer.music.play() ? ? except: ? ? ? ? pass
4. 運(yùn)行
frame = Frame(app) frame.place(x=35, y=20) openBtn = Button(frame, text='OpenFile', command=selectFile, width=8).grid(row=0, column=1) audioDec = Button(frame, text='?', command=lambda: audioDEC(x)).grid(row=1, column=0) playBtn = Button(frame, text='...', command=playStop, width=8) playBtn.grid(row=1, column=1) audioInc = Button(frame, text='?', command=lambda: audioINC(x)).grid(row=1, column=2) restartBtn = Button(frame, text='Restart', command=playAudio, width=8).grid(row=2, column=1) app.mainloop()
5. 簡易音頻播放器展示圖
- ①點(diǎn)擊“
OpenFile
”按鈕可以打開本地音頻文件 - ②“?”和“?”分別控制音量的減小和增大
- ③點(diǎn)擊"
Restart
"按鈕可以重新播放當(dāng)前選中的音頻
6. 總結(jié)
本文僅僅是實(shí)現(xiàn)了一個(gè)簡易的音頻播放器,UI極其簡陋,為了僅僅是實(shí)現(xiàn)音頻播放的功能,僅供學(xué)習(xí)參考。
原文鏈接:https://blog.csdn.net/hutianle/article/details/123204633
相關(guān)推薦
- 2022-12-15 Apache中偽靜態(tài)Rewrite的使用方法和URL重寫規(guī)則表達(dá)式講解_Linux
- 2022-10-22 Go語言同步等待組sync.WaitGroup結(jié)構(gòu)體對(duì)象方法詳解_Golang
- 2022-04-25 ASP.NET?Core?MVC中Form?Tag?Helpers用法介紹_實(shí)用技巧
- 2022-07-26 Nginx本地配置SSL訪問的實(shí)例教程_nginx
- 2022-06-12 Redis高并發(fā)場景下秒殺超賣解決方案(秒殺場景)_Redis
- 2024-03-04 JQ實(shí)現(xiàn)將div的滾動(dòng)條滾動(dòng)到指定子元素所在的位置
- 2022-06-22 React?數(shù)據(jù)共享useContext的實(shí)現(xiàn)_React
- 2022-04-01 使用lsof命令恢復(fù)已刪除文件(正在使用的文件)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支