QMediaPlayer

The QMediaPlayer class allows the playing of a media source. More

Inheritance diagram of PySide2.QtMultimedia.QMediaPlayer

Synopsis

Functions

Slots

Signals

Static functions

Detailed Description

The 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.

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();

QVideoWidget can be used with QMediaPlayer for video rendering and QMediaPlaylist for 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 QMediaPlayer is a QMediaObject , you can use several of the QMediaObject functions 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

QMediaObject QMediaService QVideoWidget QMediaPlaylist

class PySide2.QtMultimedia.QMediaPlayer([parent=None[, flags=QMediaPlayer.Flags()]])
param parent:

PySide2.QtCore.QObject

param flags:

Flags

Construct a QMediaPlayer instance parented to parent and with flags .

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 PlayingState or PausedState .

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 PlayingState or PausedState .

QMediaPlayer.BufferedMedia

The player has fully buffered the current media. The player is in the PlayingState or PausedState .

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 QIODevice based streams. If passed to QMediaPlayer constructor, the service supporting streams playback will be chosen.

QMediaPlayer.VideoSurface

The player is expected to be able to render to a QAbstractVideoSurface output .

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:

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() .

customAudioRole is cleared when this property is set to anything other than CustomRole .

PySide2.QtMultimedia.QMediaPlayer.audioRoleChanged(role)
Parameters:

roleRole

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) to 100 (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 return BufferedMedia or BufferingMedia . If the value is anything lower than 100 , mediaStatus() will return StalledMedia .

See also

mediaStatus()

PySide2.QtMultimedia.QMediaPlayer.bufferStatusChanged(percentFilled)
Parameters:

percentFilled – int

PySide2.QtMultimedia.QMediaPlayer.currentMedia()
Return type:

PySide2.QtMultimedia.QMediaContent

This property holds the current active media content being played by the player object. This value could be different from media property if a playlist is used. In this case indicates the current media content being processed by the player, while media property contains the original playlist..

PySide2.QtMultimedia.QMediaPlayer.currentMediaChanged(media)
Parameters:

mediaPySide2.QtMultimedia.QMediaContent

PySide2.QtMultimedia.QMediaPlayer.currentNetworkConfiguration()
Return type:

PySide2.QtNetwork.QNetworkConfiguration

Note

This function is deprecated.

Returns the current network access point in use. If a default contructed QNetworkConfiguration is 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() .

audioRole is set to CustomRole when this property is set.

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 QMediaPlayer object and may not be available when initial playback begins, connect to the durationChanged() signal to receive status notifications.

PySide2.QtMultimedia.QMediaPlayer.durationChanged(duration)
Parameters:

duration – int

PySide2.QtMultimedia.QMediaPlayer.error(error)
Parameters:

errorError

PySide2.QtMultimedia.QMediaPlayer.error()
Return type:

Error

Returns the current error state.

PySide2.QtMultimedia.QMediaPlayer.errorString()
Return type:

str

This property holds a string describing the last error condition..

See also

error()

static PySide2.QtMultimedia.QMediaPlayer.hasSupport(mimeType[, codecs=list()[, flags=QMediaPlayer.Flags()]])
Parameters:
  • mimeType – str

  • codecs – list of strings

  • flagsFlags

Return type:

SupportEstimate

Returns the level of support a media player has for a mimeType and a set of codecs .

The flags argument 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 QMediaPlayer can be longer than the playback of one QMediaContent , this property may change over time, the audioAvailableChanged signal 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 QMediaPlayer object, use the seekableChanged signal to monitor changes.

PySide2.QtMultimedia.QMediaPlayer.isVideoAvailable()
Return type:

bool

This property holds the video availability status for the current media..

If available, the QVideoWidget class can be used to view the video. As the life time of QMediaPlayer can be longer than the playback of one QMediaContent , this property may change over time, the videoAvailableChanged signal can be used to monitor it’s status.

See also

QVideoWidget QMediaContent

PySide2.QtMultimedia.QMediaPlayer.media()
Return type:

PySide2.QtMultimedia.QMediaContent

This property holds the active media source being used by the player object..

The player object will use the QMediaContent for selection of the content to be played.

By default this property has a null QMediaContent .

Setting this property to a null QMediaContent will cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media.

PySide2.QtMultimedia.QMediaPlayer.mediaChanged(media)
Parameters:

mediaPySide2.QtMultimedia.QMediaContent

PySide2.QtMultimedia.QMediaPlayer.mediaStatus()
Return type:

MediaStatus

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 NoMedia

See also

state

PySide2.QtMultimedia.QMediaPlayer.mediaStatusChanged(status)
Parameters:

statusMediaStatus

PySide2.QtMultimedia.QMediaPlayer.mediaStream()
Return type:

PySide2.QtCore.QIODevice

Returns the stream source of media data.

This is only valid if a stream was passed to setMedia() .

See also

setMedia()

PySide2.QtMultimedia.QMediaPlayer.mutedChanged(muted)
Parameters:

muted – bool

PySide2.QtMultimedia.QMediaPlayer.networkConfigurationChanged(configuration)
Parameters:

configurationPySide2.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:

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, currentMedia is updated with a current playlist item. The current source should be selected with setCurrentIndex (int) instead of setMedia() , otherwise the current playlist will be discarded.

See also

QMediaContent

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 with QMediaObject ‘s method setNotifyInterval() .

PySide2.QtMultimedia.QMediaPlayer.positionChanged(position)
Parameters:

position – int

PySide2.QtMultimedia.QMediaPlayer.seekableChanged(seekable)
Parameters:

seekable – bool

PySide2.QtMultimedia.QMediaPlayer.setAudioRole(audioRole)
Parameters:

audioRoleRole

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() .

customAudioRole is cleared when this property is set to anything other than CustomRole .

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() .

audioRole is set to CustomRole when this property is set.

PySide2.QtMultimedia.QMediaPlayer.setMedia(media[, stream=None])
Parameters:

Sets the current media source.

If a stream is 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. The stream must be open and readable. For macOS the stream should be also seekable.

Setting the media to a null QMediaContent will 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() and error() 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-pipeline provides custom pipelines for the GStreamer backend.

player = new QMediaPlayer;
player->setMedia(QUrl("gst-pipeline: videotestsrc ! autovideosink"));
player->play();

If QAbstractVideoSurface is used as the video output, qtvideosink can be used as a video sink element directly in the pipeline. After that the surface will receive the video frames in present() .

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 QVideoWidget is used as the video output and the pipeline contains a video sink element named qtvideosink , current QVideoWidget will 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

media()

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. configurations contains, 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:

playlistPySide2.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, currentMedia is updated with a current playlist item. The current source should be selected with setCurrentIndex (int) instead of setMedia() , otherwise the current playlist will be discarded.

See also

QMediaContent

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 with QMediaObject ‘s method setNotifyInterval() .

PySide2.QtMultimedia.QMediaPlayer.setVideoOutput(surface)
Parameters:

surfacePySide2.QtMultimedia.QAbstractVideoSurface

Sets a video surface as 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) to 100 (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:

State

This property holds the media player’s playback state..

By default this property is QMediaPlayer::Stopped

PySide2.QtMultimedia.QMediaPlayer.stateChanged(newState)
Parameters:

newStateState

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

audioRole

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

customAudioRole

static PySide2.QtMultimedia.QMediaPlayer.supportedMimeTypes([flags=QMediaPlayer.Flags()])
Parameters:

flagsFlags

Return type:

list of strings

Returns a list of MIME types supported by the media player.

The flags argument 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) to 100 (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