QMediaPlayer
It is a class used for audio and video playback in PyQt6. It provides support for a variety of media formats and can play local files, streaming media, audio and video, etc. It isQtMultimedia
Part of the module, mainly throughQMediaPlayer
Consumed controls such asQVideoWidget
To display the video content.
Main functions:
1. Loading and playing media:
-
setSource()
: Set the media source to play, which can be a local file, URL, or media stream. -
play()
: Start playing audio or video. -
pause()
: Pause playback. -
stop()
: Stop playback.
2. Control the volume:
-
setVolume()
: Set the volume (between 0 and 100).
3. Control the playback progress:
-
setPosition()
: Sets the current position of playback in milliseconds. -
position()
: Get the current playback location.
4. Obtain media information:
-
duration()
: Total length of time (in milliseconds) of the media. -
media()
: Get the currently played media content.
5. Signal:
-
mediaStatusChanged
: Send a signal when the media playback status changes. -
positionChanged
: Send a signal when the playback progress changes. -
durationChanged
: Send a signal when the total length of the media changes. -
error
: Triggered when a playback error occurs.
6. Video output:
-
setVideoOutput()
: Set a video control (such asQVideoWidget
) to display the played video content.
Sample code:
from import QApplication, QPushButton, QVBoxLayout, QWidget from import QMediaPlayer, QAudioOutput from import QUrl app = QApplication([]) # interfacewindow = QWidget() layout = QVBoxLayout() # Player# Playerplayer = QMediaPlayer() audio_output = QAudioOutput() (audio_output) (("./res/music/prompt sound 1.mp3")) # Specify local audio files() (5) # Loop play # Play buttonbutton = QPushButton("stop") () (button) (layout) () ()
Summary of key methods:
-
setSource()
: Set the media source. -
play()
,pause()
,stop()
: Control playback. -
setVolume()
: Adjust the volume. -
setPosition()
: Set the playback progress. -
setVideoOutput()
: Set the video output window.
This is the article about the use of QMediaPlayer audio and video playback in Pyqt6. For more related content on Pyqt6 QMediaPlayer audio and video playback, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!