python實現音樂播放器

來源:互聯網
上載者:User

標籤:rom   term   禁用   ini   pcl   啟用   載入   dia   proc   

開發環境:
windows10X64
python3.6
安裝的包:
tkinter(顯示介面)
pygame(播放音樂)
#- - coding: utf-8 --
import os
import tkinter
import tkinter.filedialog
import time
import threading
import pygame

root = tkinter.Tk()
root.title(‘音樂播放器v1.0‘)
#視窗大小及位置
root.geometry(‘460x600+500+100‘)

root.resizable(False, False)
folder = ‘‘
res = []
num = 0
now_music = ‘‘
#添加檔案函數
def buttonChooseClick():
""" 選擇要播放的音樂檔案夾"""

global folder
global res
if not folder:
folder = tkinter.filedialog.askdirectory()
musics = [folder + ‘\‘ + music

for music in os.listdir(folder) \
\
if music.endswith((‘.mp3‘, ‘.wav‘, ‘.ogg‘))]
#res = musics
#print(res)
ret = []
for i in musics:
ret.append(i.split(‘\‘)[1:])
res.append(i.replace(‘\‘,‘/‘))
var2 = tkinter.StringVar()
var2.set(ret)
lb = tkinter.Listbox(root, listvariable=var2)
lb.place(x=50, y=100, width=260, height=300)
if not folder:
return
global playing

playing = True

"" 根據情況禁用和啟用相應的按鈕""

buttonPlay[‘state‘] = ‘normal‘

buttonStop[‘state‘] = ‘normal‘

#buttonPause[‘state‘] = ‘normal‘

pause_resume.set(‘播放‘)
#播放音樂函數
def play():
""初始化混音器裝置"""
if len(res):
pygame.mixer.init()
global num
while playing:
if not pygame.mixer.music.get_busy():

nextMusic = res[num]
print(nextMusic)
print(num)
pygame.mixer.music.load(nextMusic.encode())
"""播放一次"""
pygame.mixer.music.play(1)
#print(len(res)-1)
if len(res)-1 == num:
num = 0
else:
num = num + 1
nextMusic = nextMusic.split(‘\‘)[1:]
musicName.set(‘playing....‘ + ‘‘.join(nextMusic))
else:
time.sleep(0.1)
#點擊播放
def buttonPlayClick():
buttonNext[‘state‘] = ‘normal‘

buttonPrev[‘state‘] = ‘normal‘
"""選擇要播放的音樂檔案夾"""
if pause_resume.get() == ‘播放‘:
pause_resume.set(‘暫停‘)
global folder

if not folder:
folder = tkinter.filedialog.askdirectory()

if not folder:
return

global playing

playing = True

""" 建立一個線程來播放音樂,當前主線程用來接收使用者操作"""

t = threading.Thread(target=play)

t.start()

elif pause_resume.get() == ‘暫停‘:

pygame.mixer.music.pause()

pause_resume.set(‘繼續‘)

elif pause_resume.get() == ‘繼續‘:

pygame.mixer.music.unpause()

pause_resume.set(‘暫停‘)

#停止播放
def buttonStopClick():
global playing

playing = False

pygame.mixer.music.stop()

#下一首
def buttonNextClick():
global playing

playing = False

pygame.mixer.music.stop()

global num

if len(res) == num:
num = 0

playing = True

""建立一個線程來播放音樂,當前主線程用來接收使用者操作"""

t = threading.Thread(target=play)

t.start()
#關閉視窗
def closeWindow():
"""修改變數,結束線程中的迴圈"""

global playing

playing = False

time.sleep(0.3)

try:

"""停止播放,如果已停止,

再次停止時會拋出異常,所以放在異常處理結構中"""

pygame.mixer.music.stop()

pygame.mixer.quit()

except:

pass

root.destroy()

#聲音控制
def control_voice(value=0.5):
"""設定背景音樂的音量。取值從0.0到1.0。在新的音樂載入前設定,音樂載入時生效。
注意; 音樂載入時生效"""
pygame.mixer.music.set_volume(float(value))

#上一首
def buttonPrevClick():
global playing

playing = False

pygame.mixer.music.stop()

global num

if num == 0:
num = len(res)-2
#num -= 1
elif num == len(res) -1:
num -=2
else:
num -=2
#num -= 1
print(num)

playing = True

""" 建立一個線程來播放音樂,當前主線程用來接收使用者操作"""

t = threading.Thread(target=play)

t.start()

root.protocol(‘WM_DELETE_WINDOW‘, closeWindow)

buttonChoose = tkinter.Button(root,text=‘添加‘,command=buttonChooseClick)

buttonChoose.place(x=50, y=10, width=50, height=20)

#print(res)
pause_resume = tkinter.StringVar(root, value=‘播放‘)
buttonPlay = tkinter.Button(root,textvariable=pause_resume,command=buttonPlayClick)

buttonPlay.place(x=190, y=10, width=50, height=20)
buttonPlay[‘state‘] = ‘disabled‘
#停止播放
buttonStop = tkinter.Button(root,text=‘停止‘,command=buttonStopClick)

buttonStop.place(x=120, y=10, width=50, height=20)

buttonStop[‘state‘] = ‘disabled‘

#暫停/繼續

#下一首
buttonNext = tkinter.Button(root,text=‘下一首‘,command=buttonNextClick)

buttonNext.place(x=260, y=10, width=50, height=20)

buttonNext[‘state‘] = ‘disabled‘
#上一首
buttonPrev = tkinter.Button(root,text=‘上一首‘,command=buttonPrevClick)

buttonPrev.place(x=330, y=10, width=50, height=20)

buttonPrev[‘state‘] = ‘disabled‘

musicName = tkinter.StringVar(root,value=‘暫時沒有播放音樂...‘)

labelName = tkinter.Label(root,textvariable=musicName)

labelName.place(x=10, y=30, width=260, height=20)

#HORIZONTAL表示為水平放置,預設為豎直,豎直為vertical
s = tkinter.Scale(root, label=‘音量‘, from_=0, to=1, orient=tkinter.HORIZONTAL,
length=240, showvalue=0, tickinterval=2, resolution=0.1, command=control_voice)
s.place(x=50, y=50 ,width=200)
#啟動訊息迴圈
root.mainloop()

如下:

python實現音樂播放器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.