PyQt/PySide – Music Player in 10 minutes

Nowadays creating an application is not a tedious task. everyone can create one with little effort.

I am going to tell you about how i wrote a simple music player in 10 minute using python.

Qt
Qt is a Cross platform C++ application development framework. K Desktop environment (KUBUNTU) is made using Qt. Qt provides interface for GUI Programming, Database, Threading, Networking, OpenGL etc. I never seen such a good cross platform framework. It support both Desktop and Mobile platforms.

Python + Qt = PyQt

We can do Qt application development using Python. Because python is such a language we can extend its feature by binding existing C, C++ libraries. So i am going to tell you how i created a music player with a little time using PySide.

Note :  There is PyQt and PySide. Both are different bindings for Qt framework. No much difference between them. PySide not support Qt5 but PyQt. for the sake of understanding tutorial consider PySide==PyQt.

Requirements:

QtCreator – To create interface (GUI)
PySide – Python package to develop music player
pyside-tools – To convert GUI file(.ui) to .py
pyside-phonon  – Manage audio files

1. Start Desiner Form using QtCreator. Name it ‘musicplayerUI.ui

2. Design a form for the music player. mine was:

snap2

3) Convert musicplayerUI.ui to musicplayerUI.py using the following command:

pyside-uic musicplayerUI.ui > musicplayerUI.py

4) Create a python file called “musicplayer.py” to implement music player, which contains:

import sys
from PySide.QtCore import *
from PySide.QtGui import *
from musicplayerUI import *

from PySide.phonon import Phonon

class AudioPlayer(QMainWindow, Ui_MusicPlayer):

    def __init__(self, parent=None):

        super(AudioPlayer, self).__init__(parent)
        self.setupUi(self)
        self.actionOpen.triggered.connect(self.open)
        self.actionQuit.triggered.connect(self.quite)
        self.horizontalSlider.setValue(0)
        self.horizontalSlider.setEnabled(False)
        self.horizontalSlider.sliderReleased.connect(self.slider_value_change)
        self.button.clicked.connect(self.play_or_pause)
        self.button.setText("Play")
        self.button.setEnabled(False)
        self.media_obj = Phonon.MediaObject(self)
        self.current_time = 0

    def play_or_pause(self):
        if Phonon.State.PlayingState == self.media_obj.state():
            self.media_obj.pause()
            self.button.setText("Play")
        elif Phonon.State.PausedState == self.media_obj.state():
            self.media_obj.play()
            self.button.setText("Pause")

    def slider_value_change(self):
        value = self.horizontalSlider.value()
        print value
        self.media_obj.seek(value)

    def open(self):
        dialog = QFileDialog()
        dialog.setViewMode(QFileDialog.Detail)
        filename = dialog.getOpenFileName(self,
             'Open audio file', '/home',
             "Audio Files (*.mp3 *.wav *.ogg)")[0]
        self.audio_output = Phonon.AudioOutput(Phonon.MusicCategory, self)
        Phonon.createPath(self.media_obj, self.audio_output)
        self.media_obj.setCurrentSource(Phonon.MediaSource(filename))
        self.media_obj.tick.connect(self.time_change)
        self.media_obj.totalTimeChanged.connect(self.total_time_change)
        self.media_obj.play()
        self.button.setEnabled(True)
        self.button.setText("Pause")
        self.horizontalSlider.setEnabled(True)

    def time_change(self, time):
        if not self.horizontalSlider.isSliderDown():
            self.horizontalSlider.setValue(time)

    def total_time_change(self, time):
        self.horizontalSlider.setRange(0, time)

    def quite(self):
        sys.exit()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = AudioPlayer()
    window.show()
    sys.exit(app.exec_())

5. Run musicplayer.py file. You can play music. enjoy!

I think program is self explanatory. Leave a comment if you have any queries, may i can help you!

Source : https://github.com/faisalp4p/MusicPlayerDemo

Thanks!

Leave a comment