Python でオンライン音楽プレーヤーを作成するコード例

Y2J
リリース: 2017-05-08 16:13:13
オリジナル
2052 人が閲覧しました

この記事では、主にオンライン音楽プレーヤーを実装するための Python の関連情報を詳しく紹介します。興味のある方は参考にしてください。ここ数日間、私は Python について少し学習し、クローラーを比較しました。興味があったので、Python ライブラリ Tkinsert を使用してインターフェイスを作成しました。このライブラリは、リクエスト モジュールを介して音楽データを取得します。 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(&#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()
ログイン後にコピー

プログラムの実行結果:

[関連する推奨事項]

1.

Python オブジェクト指向のビデオチュートリアル

3.

Python の基本的なチュートリアル

以上がPython でオンライン音楽プレーヤーを作成するコード例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!