網站首頁 編程語言 正文
1. 需要用到的Python庫
- pygame
- tkinter
2. 簡易UI設計
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. 功能模塊實現
3.1 選擇音頻文件進行播放
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 控制音頻音量大小
這里可以定義一個全局變量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 播放器初始化等細節
def playAudio(): ? ? try: ? ? ? ? mixer.init() ? ? ? ? mixer.music.load(filePath) ? ? ? ? mixer.music.set_volume(x) ? ? ? ? playBtn.config(text='pause') ? ? ? ? mixer.music.play() ? ? except: ? ? ? ? pass
4. 運行
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. 簡易音頻播放器展示圖
- ①點擊“
OpenFile
”按鈕可以打開本地音頻文件 - ②“?”和“?”分別控制音量的減小和增大
- ③點擊"
Restart
"按鈕可以重新播放當前選中的音頻
6. 總結
本文僅僅是實現了一個簡易的音頻播放器,UI極其簡陋,為了僅僅是實現音頻播放的功能,僅供學習參考。
原文鏈接:https://blog.csdn.net/hutianle/article/details/123204633
相關推薦
- 2023-03-28 python方法如何實現字符串反轉_python
- 2022-09-10 PyCharm:method?may?be?static問題及解決_python
- 2022-06-21 C#常用日期時間方法匯總_C#教程
- 2022-03-21 .NetCore使用Swagger+API多版本控制的流程分析_實用技巧
- 2021-12-01 一篇文章了解c++中的new和delete_C 語言
- 2022-12-12 React?中state與props更新深入解析_React
- 2023-04-18 Android?懸浮按鈕之實現兔兔按鈕示例_Android
- 2022-08-10 etcd通信接口之客戶端API核心方法實戰_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支