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

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

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

The 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

availablebool

PySide2.QtMultimedia.QMediaPlayer.audioRole()
Return type

Role

See also

setAudioRole()

PySide2.QtMultimedia.QMediaPlayer.audioRoleChanged(role)
Parameters

roleRole

PySide2.QtMultimedia.QMediaPlayer.bufferStatus()
Return type

int

PySide2.QtMultimedia.QMediaPlayer.bufferStatusChanged(percentFilled)
Parameters

percentFilledint

PySide2.QtMultimedia.QMediaPlayer.currentMedia()
Return type

QMediaContent

PySide2.QtMultimedia.QMediaPlayer.currentMediaChanged(media)
Parameters

mediaQMediaContent

PySide2.QtMultimedia.QMediaPlayer.currentNetworkConfiguration()
Return type

QNetworkConfiguration

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

unicode

PySide2.QtMultimedia.QMediaPlayer.customAudioRoleChanged(role)
Parameters

role – unicode

PySide2.QtMultimedia.QMediaPlayer.duration()
Return type

qint64

PySide2.QtMultimedia.QMediaPlayer.durationChanged(duration)
Parameters

durationqint64

PySide2.QtMultimedia.QMediaPlayer.error()
Return type

Error

Returns the current error state.

PySide2.QtMultimedia.QMediaPlayer.error(error)
Parameters

errorError

PySide2.QtMultimedia.QMediaPlayer.errorString()
Return type

unicode

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

  • 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

PySide2.QtMultimedia.QMediaPlayer.isMuted()
Return type

bool

PySide2.QtMultimedia.QMediaPlayer.isSeekable()
Return type

bool

PySide2.QtMultimedia.QMediaPlayer.isVideoAvailable()
Return type

bool

PySide2.QtMultimedia.QMediaPlayer.media()
Return type

QMediaContent

See also

setMedia()

PySide2.QtMultimedia.QMediaPlayer.mediaChanged(media)
Parameters

mediaQMediaContent

PySide2.QtMultimedia.QMediaPlayer.mediaStatus()
Return type

MediaStatus

PySide2.QtMultimedia.QMediaPlayer.mediaStatusChanged(status)
Parameters

statusMediaStatus

PySide2.QtMultimedia.QMediaPlayer.mediaStream()
Return type

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

mutedbool

PySide2.QtMultimedia.QMediaPlayer.networkConfigurationChanged(configuration)
Parameters

configurationQNetworkConfiguration

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

qreal

PySide2.QtMultimedia.QMediaPlayer.playbackRateChanged(rate)
Parameters

rateqreal

PySide2.QtMultimedia.QMediaPlayer.playlist()
Return type

QMediaPlaylist

See also

setPlaylist()

PySide2.QtMultimedia.QMediaPlayer.position()
Return type

qint64

See also

setPosition()

PySide2.QtMultimedia.QMediaPlayer.positionChanged(position)
Parameters

positionqint64

PySide2.QtMultimedia.QMediaPlayer.seekableChanged(seekable)
Parameters

seekablebool

PySide2.QtMultimedia.QMediaPlayer.setAudioRole(audioRole)
Parameters

audioRoleRole

See also

audioRole()

PySide2.QtMultimedia.QMediaPlayer.setCustomAudioRole(audioRole)
Parameters

audioRole – unicode

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 media source may still be used to resolve additional information about the media such as mime type. The stream must be open and readable.

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 the pipeline contains a video sink element named qtvideosink , current QVideoWidget can be used to render the video.

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

mutedbool

See also

isMuted()

PySide2.QtMultimedia.QMediaPlayer.setNetworkConfigurations(configurations)
Parameters

configurations

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

rateqreal

See also

playbackRate()

PySide2.QtMultimedia.QMediaPlayer.setPlaylist(playlist)
Parameters

playlistQMediaPlaylist

See also

playlist()

PySide2.QtMultimedia.QMediaPlayer.setPosition(position)
Parameters

positionqint64

See also

position()

PySide2.QtMultimedia.QMediaPlayer.setVideoOutput(surface)
Parameters

surfaceQAbstractVideoSurface

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(arg__1)
Parameters

arg__1QGraphicsVideoItem

Attach a QGraphicsVideoItem video output to the media player.

If the media player has already video output attached, it will be replaced with a new one.

PySide2.QtMultimedia.QMediaPlayer.setVideoOutput(arg__1)
Parameters

arg__1QVideoWidget

Attach a QVideoWidget video output to the media player.

If the media player has already video output attached, it will be replaced with a new one.

PySide2.QtMultimedia.QMediaPlayer.setVolume(volume)
Parameters

volumeint

See also

volume()

PySide2.QtMultimedia.QMediaPlayer.state()
Return type

State

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

videoAvailablebool

PySide2.QtMultimedia.QMediaPlayer.volume()
Return type

int

See also

setVolume()

PySide2.QtMultimedia.QMediaPlayer.volumeChanged(volume)
Parameters

volumeint