返回顶部

收藏

简单MP3播放器

更多
#coding=utf-8
import sys
import time
from PyQt4 import  QtGui, QtCore  
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.phonon import *

playCss = 'QPushButton#btnPlay{background:url(images/btn_%s.png);border:none;position:relative;}QPushButton#btnPlay:hover{background:url(images/btn_%s_hover.png);position:relative;}QPushButton#btnPlay:pressed{border:none;background:url(images/btn_%s_pressed.png);position:relative;}'
globalCss = 'QPushButton#btnPlay{background:url(images/btn_play.png);border:none;position:relative;}QPushButton#btnPlay:hover{background:url(images/btn_play_hover.png);position:relative;}QPushButton#btnPlay:pressed{border:none;background:url(images/btn_play_pressed.png);position:relative;}QPushButton#btnStop{background:url(images/btn_stop.png);border:none;position:relative;}QPushButton#btnStop:hover{background:url(images/btn_stop_hover.png);position:relative;}QPushButton#btnStop:pressed{border:none;background:url(images/btn_stop_pressed.png);position:relative;}QLabel#title{font-size:bold 14px Arial;color:#7e97ab;width:auto;}QLabel#takeTime{font-size:30px;font-family:Arial;color:#666;text-align:right;}QLabel#timeHour,QLabel#timeMin,QLabel#timeSec{font-size:9px;font-family:Arial;color:#666;text-align:center;}QLabel#timeTotal,QLabel#totalTime{line-height:40px;font-size:14px;font-family:Arial;color:#999;}QPushButton#btnClose{background:red;border-radius:6px;position:absolute;top:10px;}'

class PlayerWindow(QWidget):
    """docstring for PlayerWindow"""
    def __init__(self):
        super(PlayerWindow, self).__init__()
        self.setWindowTitle(QString("Mini Player"))
        #self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.SubWindow )  #
        bitmap = QtGui.QPixmap("images/bg.png");
        self.resize(bitmap.size())
        self.setMask(bitmap.mask())
        pal=QtGui.QPalette(self)
        pal.setBrush(self.backgroundRole(), QtGui.QBrush(bitmap))
        self.setPalette(pal)

        self.rightButton=False

        self.menu()

        self.initUI()

        self.ph = Phonon.createPlayer(Phonon.MusicCategory)
        self.ph.setTickInterval(1000)
        self.connect(self.ph, SIGNAL("tick(qint64)"), self.tickInterval)
        self.connect(self.ph, SIGNAL("finished()"), self.finished)
        self.connect(self.ph, SIGNAL("currentSourceChanged (const Phonon::MediaSource&)"), self.currentSourceChanged)
        self.connect(self.ph, SIGNAL("metaDataChanged()"), self.metaDataChanged)
        self.ph.setCurrentSource(Phonon.MediaSource(QString("x.mp3")))
        self.playing = False

    def initUI(self):
        self.title = QLabel(self)
        self.title.setObjectName("title")
        self.setTitle("Mini Player")

        self.btnClose = QPushButton(self)
        self.btnClose.setFocusPolicy(Qt.NoFocus) #设置焦点样式
        self.btnClose.setObjectName("btnClose")
        self.btnClose.setToolTip(u"关闭播放器")
        self.btnClose.resize(12,12)
        self.btnClose.move(10,10)
        self.connect(self.btnClose, SIGNAL("clicked()"), qApp.quit)

        self.btnPlay = QPushButton(self)
        self.btnPlay.setFocusPolicy(Qt.NoFocus)
        self.btnPlay.setObjectName("btnPlay")
        self.btnPlay.move(30, 60)
        self.btnPlay.resize(47, 47)
        self.btnPlay.clicked.connect(self.player)

        self.btnStop = QPushButton(self)
        self.btnStop.setFocusPolicy(Qt.NoFocus)
        self.btnStop.setObjectName("btnStop")
        self.btnStop.move(80, 60)
        self.btnStop.resize(47, 47)
        self.btnStop.clicked.connect(self.stopPlay)

        self.takeTime = QLabel("00:00:00",self)
        self.takeTime.setObjectName("takeTime")
        self.takeTime.move(150 + 40, 57)

        self.timeHour = QLabel("HOUR", self)
        self.timeHour.setObjectName("timeHour")
        self.timeHour.move(153 + 40, 93)

        self.timeHour = QLabel("MIN", self)
        self.timeHour.setObjectName("timeMin")
        self.timeHour.move(201 + 40, 93)

        self.timeHour = QLabel("SEC", self)
        self.timeHour.setObjectName("timeSec")
        self.timeHour.move(242 + 40, 93)

        self.timeTotal = QLabel("Totel time", self)
        self.timeTotal.setObjectName("timeTotal")
        self.timeTotal.move(18, 145)

        self.totalTime = QLabel(self)
        self.totalTime.setObjectName("totalTime")
        self.setTotalTime(0,0,0)

    def player(self):
        if self.playing:
            self.playing = False
            self.ph.pause()
            self.btnPlay.setStyleSheet(playCss.replace('%s','play'))
        else:
            self.playing = True
            self.ph.play()
            self.btnPlay.setStyleSheet(playCss.replace('%s','pause'))

    def stopPlay(self):
        self.ph.stop()
        self.playing = False
        self.btnPlay.setStyleSheet(playCss.replace('%s','play'))

    def tickInterval(self):
        remainingTime = time.gmtime(self.ph.remainingTime() / 1000)
        self.setCurrentTime(remainingTime.tm_hour, remainingTime.tm_min, remainingTime.tm_sec)

    def finished(self):
        print 'finished'

    def metaDataChanged(self):
        self.setTitle("%s" % unicode(self.ph.metaData("TITLE").takeFirst()))
        totalTime = time.gmtime(self.ph.totalTime() / 1000)
        self.setTotalTime(totalTime.tm_hour, totalTime.tm_min, totalTime.tm_sec)
        self.setCurrentTime(totalTime.tm_hour, totalTime.tm_min, totalTime.tm_sec)

    def currentSourceChanged(self, source):
        print source.deviceName()

    def setTitle(self, title):
        self.title.setText(title)
        self.title.resize(self.title.sizeHint().width(), self.title.sizeHint().height())
        self.title.move(self.width() - self.title.width() - 20,15)
        self.setWindowTitle(u"播放 %s" % title)

    def setTotalTime(self, hour, min, sec):
        self.totalTime.setText("%.2d:%.2d:%.2d" % (hour, min, sec))
        self.totalTime.resize(self.totalTime.sizeHint().width(), self.totalTime.sizeHint().height())
        self.totalTime.move(self.width() - self.totalTime.width() - 16, 145)

    def setCurrentTime(self, hour, min, sec):
        self.takeTime.setText("%.2d:%.2d:%.2d" % (hour, min, sec))

    def menu(self):
        quitAction = QAction(QIcon('quit.png'), u'退出(&Q)', self)
        self.connect(quitAction,SIGNAL("triggered()"),qApp.quit)
        aboutAction = QAction(QIcon(""), u"关于(&A)", self)
        self.connect(aboutAction,SIGNAL("triggered()"),self.about)

        self.popMenu= QtGui.QMenu() 
        self.popMenu.addAction(aboutAction) 
        self.popMenu.addAction(quitAction) 

    def about(self):
        about = QMessageBox(self)
        about.setWindowTitle(u'关于Mini Player')
        about.setText(u"Mini Player 是一个使用PyQt编写的简单MP3播放器\\n\\twapznw@gmail.com")
        about.show()

    def mouseReleaseEvent(self,e): 
        if self.rightButton == True:
            self.rightButton=False
            self.popMenu.popup(e.globalPos())

    def mouseMoveEvent(self, e):
        if e.buttons() & Qt.LeftButton:
            self.move(e.globalPos()-self.dragPos)
            e.accept()
    def mousePressEvent(self, e):
        if e.button() == Qt.LeftButton: 
            self.dragPos=e.globalPos()-self.frameGeometry().topLeft() 
            e.accept()
        if e.button() == Qt.RightButton and self.rightButton == False:
            self.rightButton=True

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    app.setStyleSheet(globalCss);
    w = PlayerWindow()
    w.show()
    sys.exit(app.exec_())
#该片段来自于http://outofmemory.cn

标签:python,图形

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Freewind 发表 2012-02-20 13:21:22 Python中的is
  2. admin 发表 2017-05-10 06:01:03 后台运行python程序(gui and not gui)
  3. fmnisme 发表 2014-08-17 06:33:40 python保证只能运行一个脚本实例
  4. the5fire 发表 2017-05-22 11:50:30 [备忘]新主机Python部署环境初始化清单
  5. master 发表 2014-08-26 16:05:26 Python的闭包(概念和实例)
  6. techug 发表 2017-05-29 02:22:06 用Python破解斗地主残局
  7. 田俊 发表 2014-09-10 11:32:39 【翻译】Writing an Hadoop MapReduce Program in Python
  8. 大象会跳舞 发表 2017-06-06 02:02:26 Python 学习 | 基础篇
  9. 博主 发表 2014-07-01 03:30:00 Python Algorithms - C9 Graphs
  10. 大象会跳舞 发表 2017-06-16 02:50:37 Twitter数据挖掘:如何使用Python分析大数据
  11. 木木 发表 2014-09-23 06:12:14 python 的内嵌time模板翻译及说明
  12. 妙音 发表 2017-06-22 15:44:03 python打乱数组顺序

发表评论