Python How to Create Media Player in PyQt5

In this Python GUI article i want to show you How to Create Media Player in PyQt5, so when it comes to Python Programming Language, you different options for building GUI Applications, in this article we are going to learn How to Create Media Player in Python & PyQt5, first of all let’s talk about PyQt5.

 

 

What is PyQt5 ?

PyQt5 is Python binding for the Qt cross platform application framework. Using PyQt5 you can create desktop applications with graphical user interface (GUI) with Python programming language. PyQt5 is developed by Riverbank Computing and is licensed under the GPL and commercial licenses.

On the other hand Qt is popular framework for developing graphical user interfaces using C++ programming language, and PyQt5 provides access to all of Qt’s functionality, including support for widgets, layouts, graphics, multimedia and networking. it also provides Pythonic API for working with Qt, and PyQt5 makes it easy to create and manage GUIs in Python.

 

 

Some of the key features of PyQt5 include:

  1. Cross-platform support: PyQt5 allows you to create applications that run on multiple platforms, including Windows, macOS and Linux.
  2. Qt Designer integration: PyQt5 includes integration with Qt Designer, Qt Designer is a visual tool for designing and laying out GUIs.
  3. Good documentation: PyQt5 provides good documentation and examples, which make it easy to learn and use.
  4. Support for modern Python features: PyQt5 supports the latest features of Python such as type annotations, async/await and f-strings.
  5. Large community: PyQt5 has large and active community of developers, which provides support, guidance and contributions to the project.

so PyQt5 is often used in desktop application development for creating GUIs with  functionalities, such as data visualization, multimedia playback and database integration. it is also often used in scientific and engineering applications for creating custom visualization and analysis tools, in this article you will see that how easily we can use multimedia module for creating of Media Player with Python & PyQt5.

 

 

What is PyQt5 Qt Multimedia ?

Qt Multimedia is an important module in PyQt5 that handle multimedia content. It also provides necessary APIs to access the camera and radio functionality. The included Qt Audio Engine provides types for 3D positional audio playback and content management. check Documentation for Qt Multimedia. and from QtMultimedia we are going to use QMediaPlayer and QMediaContent.

 

 

 

QMediaPlayer Class 

QMediaPlayer class is a high level media playback class. It can be used to playback such content as songs, movies and internet radio. The content to playback is specified as a QMediaContent object, which can be thought of as a main or canonical URL with additional information attached. When provided with a QMediaContent playback may

be able to commence.

 

 

Note: Make sure that you download and install K-Lite Codec Pack Basic, if you don’t do this

then the media player will not play the video.

 

 

So now this is the complete code for Python How to Create Media Player in PyQt5

 

 

 

OK now let me describe the above code, first of all we have imported our required classes from PyQt5 library. and these line of codes are for our window like title, icon, width and height of the window.

 

 

 

In here we are going to change the color of our window, we are going to use QPalette class, so the QPalette class contains color groups for each widget state.

 

 

 

OK now we are going to create the object of QMediaPlayer with QMediaContent.

 

 

 

And now these are our widgets that we want to use in our media player like QPushButton, QSlider, QLabel, QHBoxLayout and QVBoxLayout.

 

 

 

 

In here we are going to set our layout to the window and also we need to set the QVideoWidget object to the media player, if you don’t do this you will not receive any output.

 

 

 

These are the signals for our media player, and we have connected these signals with the slot that we are going to create.

 

 

 

This method is for opening the directory, we are going to use QFileDialog for this, the QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory.

 

 

 

In this method we are going to play the selected video, basically in the first we are checking the state of media player, if our state is PlayingState, we are going to pause our media player, in the else case we play our video in the media player.

 

 

 

And now in this method we are checking the media state, because when a user want to pause playing the video, we want to change the icon from play to pause and vice versa. also we are using the built in icons from pyqt5.

 

 

 

 

And these are the methods or slots that we have connected with the media player signals at the top. the first and second methods are for automatically slider change, and the third method is for if a user changes the slider, we have connected this method with the slider signal.

 

 

 

This is for handling the errors.

 

 

 

 

Also every PyQt5 application must create an application object. The sys.argv parameter is a list of arguments from a command line. 

 

 

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. 

 

 

 

 

 

So now run the complete code and this will be the result.

Python How to Create Media Player in PyQt5
Python How to Create Media Player in PyQt5

 

 

 

Adding Full Screen and Volume Bar to Python Media Player

For adding full-screen functionality and a volume control bar to our existing code, we can follow these steps:

  1. Add a volume control slider (QSlider) and a full-screen button (QPushButton) to the user interface.
  2. Connect the volume control slider to a method that adjusts the volume of the media player.
  3. Connect the full-screen button to a method that toggles the window between full-screen and normal modes.

 

 

 

This is the complete code 

Now, you have a volume control slider and a full-screen button added to your media player application. You can adjust the volume using the slider and switch between full-screen and normal modes using the full-screen button.

 

 

Run the code and this will be the result

How to Create Media Player in Python
How to Create Media Player in Python

 

 

 

Adding Pause and Stop Button to Python PyQt5 Media Player

For adding pause, play and stop buttons to the existing code, you can follow these steps:

  1. Create QPushButton instances for the pause, play and stop buttons.
  2. Connect each button to its respective method to handle the corresponding action (pausing, playing, stopping).
  3. Arrange the buttons in the user interface layout.

 

This is modified code with the pause, play and stop buttons added:

In this modified code:

  • Three new buttons (playBtn, pauseBtn, stopBtn) are created for play, pause and stop actions.
  • Each button is connected to its corresponding method (play_video, pause_video, stop_video) to handle the action.
  • The mediastate_changed method is updated to enable/disable the buttons based on the media player state.

 

 

 

Run the code and this will be the result

Media Player in Python
Media Player in Python

 

 

How to Customize Appearance of Python PyQt5 Media Player?

For creating stylish and modern design for the buttons and sliders that be like a media player, you can use stylesheets in PyQt5. Below is an updated version of the code with customized stylesheets applied to the buttons and sliders:

This code adds custom stylesheets to the buttons and slider, and it gives them more modern and nice appearance. You can adjust the colors and styles in the stylesheet strings to match your preferred design.

 

 

Run the complete code and this will be the result

Customize PyQt5 Media Player
Customize PyQt5 Media Player

 

 

 

FAQs:

 

How to build a media player in Python?

You can build a media player in Python using libraries such as PyQt5 or Pygame or TKinter, You can use these libraries for creating the graphical user interface (GUI) and managing media playback. This is  a basic overview of the steps:

  1. Choose a GUI library: Select a suitable GUI library like PyQt5, Tkinter or Pygame for building the user interface of your media player.
  2. Design the user interface: Create widgets such as buttons, sliders and labels to control media playback and display information.
  3. Implement media playback: Use a media library like QtMultimedia or Pygame to handle the playback of audio and video files.
  4. Add functionality: Implement features like play, pause, stop volume control, seeking and fullscreen mode to enhance the user experience.
  5. Test and debug: Test your media player to ensure all functionalities work as expected and debug any issues that arise.

By following these steps and using the correct libraries, you can build a functional media player in Python.

 

 

How to play audio in PyQt5?

For playing audio in PyQt5, you can use the QMediaPlayer class from the QtMultimedia module. 

 

 

How do I add a media player in PyQt5?

For adding a media player in PyQt5, you can use the QMediaPlayer and QVideoWidget classes from the QtMultimediaWidgets module.

 

 

Also you can check more Python GUI articles in the below links.

Subscribe and Get Free Video Courses & Articles in your Email

 

17 thoughts on “Python How to Create Media Player in PyQt5”

  1. Hi Parwiz,
    I’ve watched many of your videos posted in YouTube, and I have one question regarding one of this How To Export File As PDF In PyQt5 #32, https://www.youtube.com/watch?v=XHlABoZWke0,
    you mentioned that you can export the data from the QtextEdit to a PDF file, and I’d like to export but the info from a QTableWidget and when I type the code …

    self.ui.tableWidget.document().print_(printer)

    I got the following error:
    line 52, in pdf
    self.ui.tableWidget.document().print_(printer)
    AttributeError: ‘QTableWidget’ object has no attribute ‘document’…

    So my question is:
    do you know which attribute should I need to use with QtableWidget?

    Reply
      • Hmm in which part should I need to create the QTextDocument and also how do you create the textCursor, I have not seen that before, the funtion that I’m trying to call is the following…

        def pdf(self):
        fn, _= QFileDialog.getSaveFileName(self, “Export PDF”, None, “PDF files (.pdf);;All Files()”)
        if fn != ”:
        if QFileInfo(fn).suffix() == “”:fn += ‘.pdf’

        printer = QPrinter(QPrinter.HighResolution)
        printer.setOutputFormat(QPrinter.PdfFormat)
        printer.setOutputFileName(fn)
        self.ui.tableWidget.document.print_(printer)

        But as I mentioned before I got an error AttributeError: ‘QTableWidget’ object has no attribute ‘document’…,

        So my question is is it possible to export the whole table content to a PDF with a similar funcion if so, how do yo create the function to do this¿?

        Reply
  2. Hi Parwiz,

    I wonder if do you have an email where I can send you one question regarding one video related with PyQT?
    I’ve already sent you a FB message but I didn’t receive an answer.

    Reply
  3. I executed the exact code but after selecting the video file it throwing an error
    “DirectShowPlayerService::doRender: Unresolved error code 0x80040266 (IDispatch error #102)”
    I am using python 3.7.4(64 bit)
    Please help me out.

    Reply
  4. Hi Parwiz,

    Thanks for the youtube tutorial and the source code, I copied and pasted the source code into python 3.7 and executed it but the video player didn’t work, I selected three videos which were .MP4 but none of them played after I clicked “Open video” and selected one, the media player just stayed as a black screen. Is there a restriction on the type of video format?

    Reply
    • no there is no restriction, maybe there will be operating system problem, because i have used windows for this tutorial or maybe python version, the python version in this video was 3.6

      Reply
  5. Hi. Thanks for such a nice and detailed tutorial.
    I have a question. How to load names of multiple image files using QFileDialog and add them to QListWidget. It’s like a playlist for music files but want to do with images. Thanks

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap
×