python中gui表白中放音乐_Python上带GUI的Mp3播放器

Music Player为音乐播放器提供了一个非常简单的带有GUI的演示。这可以很容易地扩展以提供更多的特性。链接页面上给出的示例是:import musicplayer, sys, os, fnmatch, random, pprint, Tkinter

class Song:

def __init__(self, fn):

self.url = fn

self.f = open(fn)

# `__eq__` is used for the peek stream management

def __eq__(self, other):

return self.url == other.url

# this is used by the player as the data interface

def readPacket(self, bufSize):

return self.f.read(bufSize)

def seekRaw(self, offset, whence):

r = self.f.seek(offset, whence)

return self.f.tell()

files = []

def getFiles(path):

for f in sorted(os.listdir(path), key=lambda k: random.random()):

f = os.path.join(path, f)

if os.path.isdir(f): getFiles(f) # recurse

if len(files) > 1000: break # break if we have enough

if fnmatch.fnmatch(f, '*.mp3'): files.append(f)

getFiles(os.path.expanduser("~/Music"))

random.shuffle(files) # shuffle some more

i = 0

def songs():

global i, files

while True:

yield Song(files[i])

i += 1

if i >= len(files): i = 0

def peekSongs(n):

nexti = i + 1

if nexti >= len(files): nexti = 0

return map(Song, (files[nexti:] + files[:nexti])[:n])

# Create our Music Player.

player = musicplayer.createPlayer()

player.outSamplerate = 96000 # support high quality :)

player.queue = songs()

player.peekQueue = peekSongs

# Setup a simple GUI.

window = Tkinter.Tk()

window.title("Music Player")

songLabel = Tkinter.StringVar()

def onSongChange(**kwargs): songLabel.set(pprint.pformat(player.curSongMetadata))

def cmdPlayPause(*args): player.playing = not player.playing

def cmdNext(*args): player.nextSong()

Tkinter.Label(window, textvariable=songLabel).pack()

Tkinter.Button(window, text="Play/Pause", command=cmdPlayPause).pack()

Tkinter.Button(window, text="Next", command=cmdNext).pack()

player.onSongChange = onSongChange

player.playing = True # start playing

window.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值