>  기사  >  백엔드 개발  >  온라인 음악 플레이어의 Python 구현 예

온라인 음악 플레이어의 Python 구현 예

高洛峰
高洛峰원래의
2017-03-06 11:32:313199검색

이 글은 주로 온라인 음악 플레이어를 구현하기 위한 Python 관련 정보를 자세히 소개합니다. 관심 있는 친구들이 참고할 수 있습니다.

지난 며칠 동안 많은 것을 배웠습니다. Python을 배우면서 크롤러에 더 관심을 가지게 되었고, Python 라이브러리인 Tkinsert를 사용하여 NetEase의 인터페이스에서 음악 데이터를 가져오는 것이 매우 편리하다고 생각합니다. Cloud Music은 요청 모듈을 통해 데이터 획득 요청을 받고, 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(&#39;<Double-Button-1>&#39;,play)
 listBox.pack()
 
 root.mainloop()
 
def searchM():
 global m_List 
 itemCount = 50
 
 if not text.get():
  tkMessageBox.showinfo(&#39;温馨提示&#39;,&#39;您可以输入以下内容进行搜索\n1.歌曲名\n2.歌手名\n3.部分歌词&#39;)
  return
 
 #获得输入的歌名
 url = &#39;http://s.music.163.com/search/get/?type=1&s=%s&limit=%s&#39;%(text.get(),itemCount)
  
 #get请求
 header = {&#39;User-Agent&#39;:&#39;Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36&#39;}
 html = requests.get(url,header)
 data = json.loads(html.text)
 m_List = []
 
 try:
  listBox.delete(0,listBox.size())
  for MusicData in data[&#39;result&#39;][&#39;songs&#39;]:
   listBox.insert(END,MusicData[&#39;name&#39;] +&#39;------&#39;+&#39;(&#39; +MusicData[&#39;artists&#39;][0][&#39;name&#39;] + &#39;)&#39;)
   m_List.append(MusicData[&#39;audio&#39;])
 except Exception as e: 
  tkMessageBox.showinfo(&#39;温馨提示&#39;,&#39;查询过程出现错误,请重试&#39;)
  #print &#39;查询过程出现错误,请重试&#39;
  
  
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__ == &#39;__main__&#39;:
 main()

프로그램 실행 결과:

위 내용이 이 글의 전체 내용이길 바랍니다. 모든 사람의 학습에 도움이 될 것입니다. 도움이 되며, 모든 사람이 PHP 중국어 웹사이트를 지지해 주기를 바랍니다.

온라인 음악 플레이어 예제의 Python 구현과 관련된 더 많은 기사를 보려면 PHP 중국어 웹사이트를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.