python開發簡易版線上音樂播放器範例程式碼

來源:互聯網
上載者:User
線上音樂播放器,使用python的Tkinter庫做了一個介面,感覺這個庫使用起來還是挺方便的,音樂的資料來自網易雲音樂的一個介面,通過urllib.urlopen模組開啟網址,使用Json模組進行資料的解析,最後使用mp3play庫對音樂進行線上播放,也可以同時下載mp3,開發環境:python2.7 , 附上原始碼如下:

# _*_ coding:utf-8 _*_from Tkinter import *import tkMessageBoximport urllibimport jsonimport mp3play def music(): text = entry.get() text = text.encode('utf-8') text = urllib.quote(text) if not text:  tkMessageBox.showinfo('溫馨提示', '您可以輸入以下內容進行搜尋\n1.歌曲名\n2.歌手名\n3.部分歌詞')  return html=urllib.urlopen('http://s.music.163.com/search/get/?type=1&s=%s&limit=9' %text).read() text = json.loads(html) list_s = text['result']['songs'] list_url = [] global list_url list_name = [] global list_name listbox.delete(0,listbox.size()) for i in list_s:  listbox.insert(END,i['name']+ "("+i['artists'][0]['name']+")")  list_url.append(i['audio'])  list_name.append(i['name']) def play(event): global mp3 sy = listbox.curselection()[0] mp3 = mp3play.load(list_url[sy]) mp3.play() urllib.urlretrieve(list_url[sy], list_name[sy] + '.mp3') root = Tk()root.title("Tkinter Music")root.geometry('+300+100')entry = Entry(root)entry.pack()button = Button(root,text='搜尋歌曲',command=music)button.pack()listbox = Listbox(root,width=50)listbox.bind('<Double-Button-1>',play)listbox.pack()mainloop()

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援topic.alibabacloud.com。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.