這篇文章主要為大家詳細介紹了Python實現線上音樂播放器的相關資料,具有一定的參考價值,有興趣的小伙伴們可以參考一下
最近這幾天,學習了一下python,對於爬蟲比較感興趣,就做了一個簡單的爬蟲項目,使用Python的庫Tkinsert做了一個界面,感覺這個庫使用起來還是挺方便的,音樂的數據來自網易云音樂的一個接口,通過requests模組,get請求將資料獲得,使用Json模組進行資料的解析,最終使用python的mp3play庫進行對音樂的線上播放,以下是該程式的源碼。
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2016-12-28 21:03:21 # @Author : Donoy (172829352@qq.com) # @Link : http://www.cnblogs.com/Donoy/ # @Version : $Id$ from Tkinter import * import tkMessageBox import requests import json import urllib import mp3play import threading import time def center_window(root, width, height): screenwidth = root.winfo_screenwidth() screenheight = root.winfo_screenheight() size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2) root.geometry(size) def createWnd(): global root global listBox global text root = Tk() root.title('-----DMPlayer------来自网易云音乐-----') center_window(root, 440, 250) root['background'] = '#C7EDCC' text = Entry(font='宋体',width=36) text.pack() button = Button(root,text='搜索',width=18,fg='red',background='#CDCDC1',command=searchM).pack() listBox = Listbox(root, height=12,width=72,background='#C7EDCC') listBox.bind('<Double-Button-1>',play) listBox.pack() root.mainloop() def searchM(): global m_List itemCount = 50 if not text.get(): tkMessageBox.showinfo('温馨提示','您可以输入以下内容进行搜索\n1.歌曲名\n2.歌手名\n3.部分歌词') return #获得输入的歌名 url = 'http://s.music.163.com/search/get/?type=1&s=%s&limit=%s'%(text.get(),itemCount) #get请求 header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36'} html = requests.get(url,header) data = json.loads(html.text) m_List = [] try: listBox.delete(0,listBox.size()) for MusicData in data['result']['songs']: listBox.insert(END,MusicData['name'] +'------'+'(' +MusicData['artists'][0]['name'] + ')') m_List.append(MusicData['audio']) except Exception as e: tkMessageBox.showinfo('温馨提示','查询过程出现错误,请重试') #print '查询过程出现错误,请重试' def play(args): try: global mp3 sy = listBox.curselection()[0] mp3 = mp3play.load(m_List[int(sy)]) mp3.play() #time.sleep(1000) except Exception as e: pass def main(): createWnd() if __name__ == '__main__': main()
程式運行結果:
以上就是本文的全部內容,希望對大家的學習有幫助,也希望大家多多支援PHP中文網。
更多Python實現線上音樂播放器範例相關文章請關注PHP中文網!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具