QMediaPlayer¶
The
QMediaPlayerclass allows the playing of a media source. More…

Synopsis¶
Functions¶
def
audioRole()def
bufferStatus()def
currentMedia()def
currentNetworkConfiguration()def
customAudioRole()def
duration()def
error()def
errorString()def
isAudioAvailable()def
isMuted()def
isSeekable()def
isVideoAvailable()def
media()def
mediaStatus()def
mediaStream()def
playbackRate()def
playlist()def
position()def
setAudioRole(audioRole)def
setCustomAudioRole(audioRole)def
setVideoOutput(surface)def
setVideoOutput(surfaces)def
state()def
supportedAudioRoles()def
supportedCustomAudioRoles()def
volume()
Slots¶
def
pause()def
play()def
setMedia(media[, stream=None])def
setMuted(muted)def
setNetworkConfigurations(configurations)def
setPlaybackRate(rate)def
setPlaylist(playlist)def
setPosition(position)def
setVolume(volume)def
stop()
Signals¶
def
audioAvailableChanged(available)def
audioRoleChanged(role)def
bufferStatusChanged(percentFilled)def
currentMediaChanged(media)def
customAudioRoleChanged(role)def
durationChanged(duration)def
error(error)def
mediaChanged(media)def
mediaStatusChanged(status)def
mutedChanged(muted)def
networkConfigurationChanged(configuration)def
playbackRateChanged(rate)def
positionChanged(position)def
seekableChanged(seekable)def
stateChanged(newState)def
videoAvailableChanged(videoAvailable)def
volumeChanged(volume)
Static functions¶
def
hasSupport(mimeType[, codecs=list()[, flags=QMediaPlayer.Flags()]])def
supportedMimeTypes([flags=QMediaPlayer.Flags()])
Detailed Description¶
The
QMediaPlayerclass 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 aQMediaContentobject, which can be thought of as a main or canonical URL with additional information attached. When provided with aQMediaContentplayback may be able to commence.player = new QMediaPlayer; connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); player->setVolume(50); player->play();
QVideoWidgetcan be used withQMediaPlayerfor video rendering andQMediaPlaylistfor accessing playlist functionality.playlist = new QMediaPlaylist; playlist->addMedia(QUrl("http://example.com/movie1.mp4")); playlist->addMedia(QUrl("http://example.com/movie2.mp4")); playlist->addMedia(QUrl("http://example.com/movie3.mp4")); playlist->setCurrentIndex(1); player = new QMediaPlayer; player->setPlaylist(playlist); videoWidget = new QVideoWidget; player->setVideoOutput(videoWidget); videoWidget->show(); player->play();Since
QMediaPlayeris aQMediaObject, you can use several of theQMediaObjectfunctions for things like:
Accessing the currently playing media’s metadata (
metaData()and predefined meta-data keys )Checking to see if the media playback service is currently available (
availability())See also
QMediaObjectQMediaServiceQVideoWidgetQMediaPlaylist
- class PySide2.QtMultimedia.QMediaPlayer([parent=None[, flags=QMediaPlayer.Flags()]])¶
- param parent:
- param flags:
Flags
Construct a
QMediaPlayerinstance parented toparentand withflags.
- PySide2.QtMultimedia.QMediaPlayer.State¶
Defines the current state of a media player.
Constant
Description
QMediaPlayer.StoppedState
The media player is not playing content, playback will begin from the start of the current track.
QMediaPlayer.PlayingState
The media player is currently playing content.
QMediaPlayer.PausedState
The media player has paused playback, playback of the current track will resume from the position the player was paused at.
- PySide2.QtMultimedia.QMediaPlayer.MediaStatus¶
Defines the status of a media player’s current media.
Constant
Description
QMediaPlayer.UnknownMediaStatus
The status of the media cannot be determined.
QMediaPlayer.NoMedia
There is no current media. The player is in the
StoppedState.QMediaPlayer.LoadingMedia
The current media is being loaded. The player may be in any state.
QMediaPlayer.LoadedMedia
The current media has been loaded. The player is in the
StoppedState.QMediaPlayer.StalledMedia
Playback of the current media has stalled due to insufficient buffering or some other temporary interruption. The player is in the
PlayingStateorPausedState.QMediaPlayer.BufferingMedia
The player is buffering data but has enough data buffered for playback to continue for the immediate future. The player is in the
PlayingStateorPausedState.QMediaPlayer.BufferedMedia
The player has fully buffered the current media. The player is in the
PlayingStateorPausedState.QMediaPlayer.EndOfMedia
Playback has reached the end of the current media. The player is in the
StoppedState.QMediaPlayer.InvalidMedia
The current media cannot be played. The player is in the
StoppedState.
- PySide2.QtMultimedia.QMediaPlayer.Flag¶
Constant
Description
QMediaPlayer.LowLatency
The player is expected to be used with simple audio formats, but playback should start without significant delay. Such playback service can be used for beeps, ringtones, etc.
QMediaPlayer.StreamPlayback
The player is expected to play
QIODevicebased streams. If passed toQMediaPlayerconstructor, the service supporting streams playback will be chosen.QMediaPlayer.VideoSurface
The player is expected to be able to render to a
QAbstractVideoSurfaceoutput.
- PySide2.QtMultimedia.QMediaPlayer.Error¶
Defines a media player error condition.
Constant
Description
QMediaPlayer.NoError
No error has occurred.
QMediaPlayer.ResourceError
A media resource couldn’t be resolved.
QMediaPlayer.FormatError
The format of a media resource isn’t (fully) supported. Playback may still be possible, but without an audio or video component.
QMediaPlayer.NetworkError
A network error occurred.
QMediaPlayer.AccessDeniedError
There are not the appropriate permissions to play a media resource.
QMediaPlayer.ServiceMissingError
A valid playback service was not found, playback cannot proceed.
- PySide2.QtMultimedia.QMediaPlayer.audioAvailableChanged(available)¶
- Parameters:
available – bool
- PySide2.QtMultimedia.QMediaPlayer.audioRole()¶
- Return type:
This property holds the role of the audio stream played by the media player..
It can be set to specify the type of audio being played, allowing the system to make appropriate decisions when it comes to volume, routing or post-processing.
The audio role must be set before calling
setMedia().customAudioRoleis cleared when this property is set to anything other thanCustomRole.See also
- PySide2.QtMultimedia.QMediaPlayer.bufferStatus()¶
- Return type:
int
This property holds the percentage of the temporary buffer filled before playback begins or resumes, from
0(empty) to100(full)..When the player object is buffering; this property holds the percentage of the temporary buffer that is filled. The buffer will need to reach 100% filled before playback can start or resume, at which time
mediaStatus()will returnBufferedMediaorBufferingMedia. If the value is anything lower than100,mediaStatus()will returnStalledMedia.See also
- PySide2.QtMultimedia.QMediaPlayer.bufferStatusChanged(percentFilled)¶
- Parameters:
percentFilled – int
- PySide2.QtMultimedia.QMediaPlayer.currentMedia()¶
- Return type:
This property holds the current active media content being played by the player object. This value could be different from
mediaproperty if a playlist is used. In this case indicates the current media content being processed by the player, whilemediaproperty contains the original playlist..See also
- PySide2.QtMultimedia.QMediaPlayer.currentMediaChanged(media)¶
- Parameters:
- PySide2.QtMultimedia.QMediaPlayer.currentNetworkConfiguration()¶
- Return type:
Note
This function is deprecated.
Returns the current network access point in use. If a default contructed
QNetworkConfigurationis returned this feature is not available or that none of the current supplied configurations are in use.
- PySide2.QtMultimedia.QMediaPlayer.customAudioRole()¶
- Return type:
str
This property holds the role of the audio stream played by the media player..
It can be set to specify the type of audio being played when the backend supports audio roles unknown to Qt. Specifying a role allows the system to make appropriate decisions when it comes to volume, routing or post-processing.
The audio role must be set before calling
setMedia().audioRoleis set toCustomRolewhen this property is set.See also
- PySide2.QtMultimedia.QMediaPlayer.customAudioRoleChanged(role)¶
- Parameters:
role – str
- PySide2.QtMultimedia.QMediaPlayer.duration()¶
- Return type:
int
This property holds the duration of the current media..
The value is the total playback time in milliseconds of the current media. The value may change across the life time of the
QMediaPlayerobject and may not be available when initial playback begins, connect to thedurationChanged()signal to receive status notifications.
- PySide2.QtMultimedia.QMediaPlayer.durationChanged(duration)¶
- Parameters:
duration – int
- PySide2.QtMultimedia.QMediaPlayer.error()
- Return type:
Returns the current error state.
- PySide2.QtMultimedia.QMediaPlayer.errorString()¶
- Return type:
str
This property holds a string describing the last error condition..
See also
- static PySide2.QtMultimedia.QMediaPlayer.hasSupport(mimeType[, codecs=list()[, flags=QMediaPlayer.Flags()]])¶
- Parameters:
mimeType – str
codecs – list of strings
flags –
Flags
- Return type:
Returns the level of support a media player has for a
mimeTypeand a set ofcodecs.The
flagsargument allows additional requirements such as performance indicators to be specified.
- PySide2.QtMultimedia.QMediaPlayer.isAudioAvailable()¶
- Return type:
bool
This property holds the audio availabilty status for the current media..
As the life time of
QMediaPlayercan be longer than the playback of oneQMediaContent, this property may change over time, theaudioAvailableChangedsignal can be used to monitor it’s status.
- PySide2.QtMultimedia.QMediaPlayer.isMuted()¶
- Return type:
bool
This property holds the muted state of the current media..
The value will be true if the playback volume is muted; otherwise false.
- PySide2.QtMultimedia.QMediaPlayer.isSeekable()¶
- Return type:
bool
This property holds the seek-able status of the current media.
If seeking is supported this property will be true; false otherwise. The status of this property may change across the life time of the
QMediaPlayerobject, use theseekableChangedsignal to monitor changes.
- PySide2.QtMultimedia.QMediaPlayer.isVideoAvailable()¶
- Return type:
bool
This property holds the video availability status for the current media..
If available, the
QVideoWidgetclass can be used to view the video. As the life time ofQMediaPlayercan be longer than the playback of oneQMediaContent, this property may change over time, thevideoAvailableChangedsignal can be used to monitor it’s status.See also
QVideoWidgetQMediaContent
- PySide2.QtMultimedia.QMediaPlayer.media()¶
- Return type:
This property holds the active media source being used by the player object..
The player object will use the
QMediaContentfor selection of the content to be played.By default this property has a null
QMediaContent.Setting this property to a null
QMediaContentwill cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media.See also
- PySide2.QtMultimedia.QMediaPlayer.mediaChanged(media)¶
- Parameters:
- PySide2.QtMultimedia.QMediaPlayer.mediaStatus()¶
- Return type:
This property holds the status of the current media stream..
The stream status describes how the playback of the current stream is progressing.
By default this property is
NoMediaSee also
- PySide2.QtMultimedia.QMediaPlayer.mediaStatusChanged(status)¶
- Parameters:
status –
MediaStatus
- PySide2.QtMultimedia.QMediaPlayer.mediaStream()¶
- Return type:
Returns the stream source of media data.
This is only valid if a stream was passed to
setMedia().See also
- PySide2.QtMultimedia.QMediaPlayer.mutedChanged(muted)¶
- Parameters:
muted – bool
- PySide2.QtMultimedia.QMediaPlayer.networkConfigurationChanged(configuration)¶
- Parameters:
configuration –
PySide2.QtNetwork.QNetworkConfiguration
Note
This function is deprecated.
- PySide2.QtMultimedia.QMediaPlayer.pause()¶
Pause playing the current source.
- PySide2.QtMultimedia.QMediaPlayer.play()¶
Start or resume playing the current source.
- PySide2.QtMultimedia.QMediaPlayer.playbackRate()¶
- Return type:
float
This property holds the playback rate of the current media..
This value is a multiplier applied to the media’s standard play rate. By default this value is 1.0, indicating that the media is playing at the standard pace. Values higher than 1.0 will increase the rate of play. Values less than zero can be set and indicate the media should rewind at the multiplier of the standard pace.
Not all playback services support change of the playback rate. It is framework defined as to the status and quality of audio and video while fast forwarding or rewinding.
- PySide2.QtMultimedia.QMediaPlayer.playbackRateChanged(rate)¶
- Parameters:
rate – float
- PySide2.QtMultimedia.QMediaPlayer.playlist()¶
- Return type:
This property holds the media playlist being used by the player object..
The player object will use the current playlist item for selection of the content to be played.
By default this property is set to null.
If the media playlist is used as a source,
currentMediais updated with a current playlist item. The current source should be selected withsetCurrentIndex(int) instead ofsetMedia(), otherwise the current playlist will be discarded.See also
- PySide2.QtMultimedia.QMediaPlayer.position()¶
- Return type:
int
This property holds the playback position of the current media..
The value is the current playback position, expressed in milliseconds since the beginning of the media. Periodically changes in the position will be indicated with the signal
positionChanged(), the interval between updates can be set withQMediaObject‘s methodsetNotifyInterval().
- PySide2.QtMultimedia.QMediaPlayer.positionChanged(position)¶
- Parameters:
position – int
- PySide2.QtMultimedia.QMediaPlayer.seekableChanged(seekable)¶
- Parameters:
seekable – bool
- PySide2.QtMultimedia.QMediaPlayer.setAudioRole(audioRole)¶
- Parameters:
audioRole –
Role
This property holds the role of the audio stream played by the media player..
It can be set to specify the type of audio being played, allowing the system to make appropriate decisions when it comes to volume, routing or post-processing.
The audio role must be set before calling
setMedia().customAudioRoleis cleared when this property is set to anything other thanCustomRole.See also
- PySide2.QtMultimedia.QMediaPlayer.setCustomAudioRole(audioRole)¶
- Parameters:
audioRole – str
This property holds the role of the audio stream played by the media player..
It can be set to specify the type of audio being played when the backend supports audio roles unknown to Qt. Specifying a role allows the system to make appropriate decisions when it comes to volume, routing or post-processing.
The audio role must be set before calling
setMedia().audioRoleis set toCustomRolewhen this property is set.See also
- PySide2.QtMultimedia.QMediaPlayer.setMedia(media[, stream=None])¶
- Parameters:
stream –
PySide2.QtCore.QIODevice
Sets the current
mediasource.If a
streamis supplied; media data will be read from it instead of resolving the media source. In this case the url should be provided to resolve additional information about the media such as mime type. Thestreammust be open and readable. For macOS thestreamshould be also seekable.Setting the media to a null
QMediaContentwill cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media.Note
This function returns immediately after recording the specified source of the media. It does not wait for the media to finish loading and does not check for errors. Listen for the
mediaStatusChanged()anderror()signals to be notified when the media is loaded and when an error occurs during loading.Since Qt 5.12.2, the url scheme
gst-pipelineprovides custom pipelines for the GStreamer backend.player = new QMediaPlayer; player->setMedia(QUrl("gst-pipeline: videotestsrc ! autovideosink")); player->play();
If
QAbstractVideoSurfaceis used as the video output,qtvideosinkcan be used as a video sink element directly in the pipeline. After that the surface will receive the video frames inpresent().class Surface : public QAbstractVideoSurface { public: Surface(QObject *p) : QAbstractVideoSurface(p) { } QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType) const override { // Make sure that the driver supports this pixel format. return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_YUYV; } // Video frames are handled here. bool present(const QVideoFrame &) override { return true; } }; player = new QMediaPlayer; player->setVideoOutput(new Surface(player)); player->setMedia(QUrl("gst-pipeline: videotestsrc ! qtvideosink")); player->play();
If
QVideoWidgetis used as the video output and the pipeline contains a video sink element namedqtvideosink, currentQVideoWidgetwill be used to render the video.player = new QMediaPlayer; videoWidget = new QVideoWidget; videoWidget->show(); player->setVideoOutput(videoWidget); player->setMedia(QUrl("gst-pipeline: videotestsrc ! xvimagesink name=\"qtvideosink\"")); player->play();
If the pipeline contains appsrc element, it will be used to push data from
stream.QImage img("images/qt-logo.png"); img = img.convertToFormat(QImage::Format_ARGB32); QByteArray ba(reinterpret_cast<const char *>(img.bits()), img.sizeInBytes()); QBuffer buffer(&ba); buffer.open(QIODevice::ReadOnly); player = new QMediaPlayer; player->setMedia(QUrl("gst-pipeline: appsrc blocksize=4294967295 ! \ video/x-raw,format=BGRx,framerate=30/1,width=200,height=147 ! \ coloreffects preset=heat ! videoconvert ! video/x-raw,format=I420 ! jpegenc ! rtpjpegpay ! \ udpsink host=127.0.0.1 port=5000"), &buffer); player->play(); QMediaPlayer *receiver = new QMediaPlayer; videoWidget = new QVideoWidget; receiver->setVideoOutput(videoWidget); receiver->setMedia(QUrl("gst-pipeline: udpsrc port=5000 ! \ application/x-rtp,encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec ! \ xvimagesink name=qtvideosink")); receiver->play(); // Content will be shown in this widget. videoWidget->show();
See also
- PySide2.QtMultimedia.QMediaPlayer.setMuted(muted)¶
- Parameters:
muted – bool
This property holds the muted state of the current media..
The value will be true if the playback volume is muted; otherwise false.
- PySide2.QtMultimedia.QMediaPlayer.setNetworkConfigurations(configurations)¶
- Parameters:
configurations –
Note
This function is deprecated.
Sets the network access points for remote media playback.
configurationscontains, in ascending preferential order, a list of configuration that can be used for network access.This will invalidate the choice of previous configurations.
- PySide2.QtMultimedia.QMediaPlayer.setPlaybackRate(rate)¶
- Parameters:
rate – float
This property holds the playback rate of the current media..
This value is a multiplier applied to the media’s standard play rate. By default this value is 1.0, indicating that the media is playing at the standard pace. Values higher than 1.0 will increase the rate of play. Values less than zero can be set and indicate the media should rewind at the multiplier of the standard pace.
Not all playback services support change of the playback rate. It is framework defined as to the status and quality of audio and video while fast forwarding or rewinding.
- PySide2.QtMultimedia.QMediaPlayer.setPlaylist(playlist)¶
- Parameters:
playlist –
PySide2.QtMultimedia.QMediaPlaylist
This property holds the media playlist being used by the player object..
The player object will use the current playlist item for selection of the content to be played.
By default this property is set to null.
If the media playlist is used as a source,
currentMediais updated with a current playlist item. The current source should be selected withsetCurrentIndex(int) instead ofsetMedia(), otherwise the current playlist will be discarded.See also
- PySide2.QtMultimedia.QMediaPlayer.setPosition(position)¶
- Parameters:
position – int
This property holds the playback position of the current media..
The value is the current playback position, expressed in milliseconds since the beginning of the media. Periodically changes in the position will be indicated with the signal
positionChanged(), the interval between updates can be set withQMediaObject‘s methodsetNotifyInterval().
- PySide2.QtMultimedia.QMediaPlayer.setVideoOutput(surface)¶
- Parameters:
Sets a video
surfaceas the video output of a media player.If a video output has already been set on the media player the new surface will replace it.
- PySide2.QtMultimedia.QMediaPlayer.setVideoOutput(surfaces)
- Parameters:
surfaces –
- PySide2.QtMultimedia.QMediaPlayer.setVolume(volume)¶
- Parameters:
volume – int
This property holds the current playback volume..
The playback volume is scaled linearly, ranging from
0(silence) to100(full volume). Values outside this range will be clamped.By default the volume is
100.UI volume controls should usually be scaled nonlinearly. For example, using a logarithmic scale will produce linear changes in perceived loudness, which is what a user would normally expect from a volume control. See
convertVolume()for more details.
- PySide2.QtMultimedia.QMediaPlayer.state()¶
- Return type:
This property holds the media player’s playback state..
By default this property is QMediaPlayer::Stopped
See also
- PySide2.QtMultimedia.QMediaPlayer.stop()¶
Stop playing, and reset the play position to the beginning.
- PySide2.QtMultimedia.QMediaPlayer.supportedAudioRoles()¶
- Return type:
Returns a list of supported audio roles.
If setting the audio role is not supported, an empty list is returned.
See also
- PySide2.QtMultimedia.QMediaPlayer.supportedCustomAudioRoles()¶
- Return type:
list of strings
Returns a list of supported custom audio roles. An empty list may indicate that the supported custom audio roles aren’t known. The list may not be complete.
See also
- static PySide2.QtMultimedia.QMediaPlayer.supportedMimeTypes([flags=QMediaPlayer.Flags()])¶
- Parameters:
flags –
Flags- Return type:
list of strings
Returns a list of MIME types supported by the media player.
The
flagsargument causes the resultant list to be restricted to MIME types which can be supported given additional requirements, such as performance indicators.This function may not return useful results on some platforms, and support for a specific file of a given mime type is not guaranteed even if the mime type is in general supported. In addition, in some cases this function will need to load all available media plugins and query them for their support, which may take some time.
- PySide2.QtMultimedia.QMediaPlayer.videoAvailableChanged(videoAvailable)¶
- Parameters:
videoAvailable – bool
- PySide2.QtMultimedia.QMediaPlayer.volume()¶
- Return type:
int
This property holds the current playback volume..
The playback volume is scaled linearly, ranging from
0(silence) to100(full volume). Values outside this range will be clamped.By default the volume is
100.UI volume controls should usually be scaled nonlinearly. For example, using a logarithmic scale will produce linear changes in perceived loudness, which is what a user would normally expect from a volume control. See
convertVolume()for more details.
- PySide2.QtMultimedia.QMediaPlayer.volumeChanged(volume)¶
- Parameters:
volume – int
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.