How to Build Python Media Player with PyQt5

In this Python PyQt5 article we want to learn How to Build Python Media Player with PyQt5, Python is powerful language that can be used for different types of applications including creating multimedia applications. PyQt5 is popular Python library that allows developers to create graphical user interfaces for desktop applications. in this article we want to talk about how to build Python media player with PyQt5.

 

 

How to Build Python Media Player with PyQt5

First of all we need to install PyQt5 and you can use pip for the installation.

 

 

This is the complete code for Python PyQt5 Media Player

 

 

 

So first of all we have imported the required libraries and modules from PyQt5.

 

 

This code defines class called MediaPlayer that extends from QWidget class. __init__ method is the constructor of the class which takes an optional parent argument. in the __init__ method super function is called to initialize the QWidget base class. next line sets the window title of the media player to “GeeksCoders – Media Player”. after that an instance of the QMediaPlayer class is created and assigned to the instance variable self.mediaPlayer. QMediaPlayer object is initialized with two arguments: None and QMediaPlayer.VideoSurface, first argument is the parent object of the media player which is set to None, second argument specifies the type of media that the player can handle, which is set to video in this case, also we set volume of the media player to 50 using the setVolume method of the QMediaPlayer object.

 

 

 

In this code we have create two QPushButton, one for playing the video and another one for stopping the video, also we have connected the clicked signal of these buttons to the specific method or slots.

 

 

This code initializes horizontal QSlider object and assigns it to the instance variable self.seekSlider. slider is created using the Qt.Horizontal parameter which sets the orientation of the slider to horizontal. setRange method is then called on the self.seekSlider object to set the range of the slider to (0, 0). this effectively makes the slider unusable since it has no range to move along. also sliderMoved signal of the self.seekSlider object is connected to the setPosition method. this means that when the user moves the slider the setPosition method will be called which will handle setting the position of the media playback accordingly.

 

 

This code creates an instance of the QPushButton class and assigns it to the instance variable self.openButton. button is initialized with the label Open. clicked signal of the self.openButton object is then connected to the openFile method. this means that when the user clicks on the self.openButton openFile method will be called which will handle opening the file that the user wants to play.

 

 

This code initializes horizontal box layout object controlLayout and sets its content margins to zero on all sides using setContentsMargins method. after that addWidget method is called on the controlLayout object four times to add four widgets: self.playButton, self.stopButton, self.seekSlider and self.openButton. these widgets are added to the layout in the order they are listed.

 

 

This code creates an instance of the QVideoWidget class and assigns it to the instance variable self.videoWidget. QVideoWidget class is a widget that can be used to display video content. it provides a surface for the QMediaPlayer to display its video output.

 

 

This code connects three signals emitted by the QMediaPlayer object to their slot methods. first line connects stateChanged signal of the self.mediaPlayer object to the mediaStateChanged method. this means that when the state of the media player changes for example when it goes from playing to paused , mediaStateChanged method will be called to handle the change.

second line connects positionChanged signal of the self.mediaPlayer object to the positionChanged method. this means that when the position of the media playback changes for example when the user seeks to a new position in the video, positionChanged method will be called to handle the change.

third line connects the durationChanged signal of the self.mediaPlayer object to the durationChanged method. this means that when the duration of the media playback changes for example when new video file is loaded , durationChanged method will be called to handle the change.

by connecting these signals to their slot methods, media player object can communicate important changes in its state to the rest of the program and trigger the necessary actions to respond to those changes.

 

 

In this code we are using QFileDialog to open a video file 

 

 

This code defines two methods play and stop which are called when the corresponding buttons are clicked.

play method first checks the state of the media player by calling self.mediaPlayer.state(). if the media player is already in the PlayingState the method pauses the playback using pause() method of the media player object. if the media player is not in the PlayingState the method starts playing the media using the play() method of the media player object.

stop method simply stops the media playback using the stop() method of the media player object. when this method is called, media player will stop playing the current media file and reset the playback position to the beginning of the file.

 

 

This code defines a method setPosition that is called when the user changes the position of the seek slider widget. the method takes single parameter position which is the new position that the user has set.

 

 

This code defines a method mediaStateChanged that is called when the state of the QMediaPlayer object changes. this method takes single parameter state which represents the new state of the media player.

mediaStateChanged method then checks the current state of the media player using the state() method of the media player object. 

 

 

This code defines a method positionChanged that is called when the playback position of the media file being played changes. this method takes single parameter position which represents the new playback position of the media file being played.

 

 

This code defines a method durationChanged that is called when the duration of the media file being played changes. this method takes a single parameter duration which represents the new duration of the media file being played.

 

 

 

Run the complete code and this will be the result

How to Build Python Media Player with PyQt5
How to Build Python Media Player with PyQt5

 

 

 

Learn More on PyQt5

Leave a Comment