class QMediaPlayer

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

Inheritance diagram of PySide6.QtMultimedia.QMediaPlayer

Synopsis

Properties

Methods

Slots

Signals

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

The QMediaPlayer class is a high level media playback class. It can be used to playback audio of video media files. The content to playback is specified as a QUrl object.

player = QMediaPlayer()
audioOutput = QAudioOutput()
player.setAudioOutput(audioOutput)
player.positionChanged.connect(self.positionChanged)
player.setSource(QUrl.fromLocalFile("/Users/me/Music/coolsong.mp3"))
audioOutput.setVolume(50)
player.play()

QVideoWidget can be used with QMediaPlayer for video rendering.

See also

QVideoWidget

class PlaybackState

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. This indicates the same as the playing property.

QMediaPlayer.PausedState

The media player has paused playback, playback of the current track will resume from the position the player was paused at.

Added in version 6.1.

class MediaStatus

Defines the status of a media player’s current media.

Constant

Description

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 .

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

class Loops

(inherits enum.IntEnum) Some predefined constants for the loops property.

Constant

Description

QMediaPlayer.Infinite

Loop forever.

QMediaPlayer.Once

Play the media once (the default).

Added in version 6.2.3.

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property activeAudioTrackᅟ: int

This property holds Returns the currently active audio track..

By default, the first available audio track will be chosen.

Set index to -1 to disable all audio tracks.

Access functions:
property activeSubtitleTrackᅟ: int

This property holds Returns the currently active subtitle track..

Set index to -1 to disable subtitles.

Subtitles are disabled by default.

Access functions:
property activeVideoTrackᅟ: int

This property holds Returns the currently active video track..

By default, the first available audio track will be chosen.

Set index to -1 to disable all video tracks.

Access functions:
property audioBufferOutputᅟ: QAudioBufferOutput

This property holds The output audio buffer used by the media player..

Sets an audio buffer output to the media player.

If QAudioBufferOutput is specified and the media source contains an audio stream, the media player, it will emit the signal audioBufferReceived with audio buffers containing decoded audio data. At the end of the audio stream, QMediaPlayer emits an empty QAudioBuffer .

QMediaPlayer emits outputs audio buffers at the same time as it pushes the matching data to the audio output if it’s specified. However, the sound can be played with a small delay due to audio bufferization.

The format of emitted audio buffers is taken from the specified output or from the matching audio stream if the output returns an invalid format. Emitted audio data is not scaled depending on the current playback rate.

Potential use cases of utilizing QAudioBufferOutput with QMediaPlayer might be:

  • Audio visualization. If the playback rate of the media player is not 1, you may scale the output image dimensions, or image update interval according to the requirements of the visualizer.

  • Any AI sound processing, e.g. voice recognition.

  • Sending the data to external audio output. Playback rate changing, synchronization with video, and manual flushing on stoping and seeking should be considered. We don’t recommend using the audio buffer output for this purpose unless you have a strong reason for this.

Access functions:
property audioOutputᅟ: QAudioOutput

This property holds The audio output device used by the media player..

The current audio output to be used when playing back media. Setting a new audio output will replace the currently used output.

Setting this property to nullptr will disable any audio output.

Access functions:
property audioTracksᅟ: list of QMediaMetaData

Lists the set of available audio tracks inside the media.

The QMediaMetaData returned describes the properties of individual tracks.

Different audio tracks can for example contain audio in different languages.

Access functions:
property bufferProgressᅟ: float

This property holds the percentage of the temporary buffer filled before playback begins or resumes, from 0. (empty) to 1. (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()

Access functions:
property durationᅟ: 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.

Access functions:
property errorᅟ: QMediaPlayer.Error

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

See also

error()

Access functions:
property errorStringᅟ: str

This property holds This property holds a string describing the current error condition in more detail..

Access functions:
property hasAudioᅟ: bool

This property holds This property holds whether the media contains audio..

Access functions:
property hasVideoᅟ: bool

This property holds This property holds whether the media contains video..

Access functions:
property loopsᅟ: int

Determines how often the media is played before the player stops. Set to Infinite to loop the current media file forever.

The default value is 1. Setting this property to 0 has no effect.

Access functions:
property mediaStatusᅟ: QMediaPlayer.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

Access functions:
property metaDataᅟ: QMediaMetaData

Returns meta data for the current media used by the media player.

Meta data can contain information such as the title of the video or its creation date.

Note

The Windows implementation provides metadata only for media located on the local file system.

Access functions:
property playbackRateᅟ: float

This property holds the playback rate of the current media..

This value is a multiplier applied to the media’s standard playback rate. By default this value is 1.0, indicating that the media is playing at the standard speed. Values higher than 1.0 will increase the playback speed, while values between 0.0 and 1.0 results in slower playback. Negative playback rates are not supported.

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.

Access functions:
property playbackStateᅟ: QMediaPlayer.PlaybackState

Returns the PlaybackState .

See also

playing

Access functions:
property playingᅟ: bool

This property holds Whether the media is playing..

Access functions:
property 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 positionChanged() signal.

If the seekable property is true, this property can be set to milliseconds.

Access functions:
property seekableᅟ: 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.

Access functions:
property sourceᅟ: QUrl

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

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

By default this property has a null QUrl.

Setting this property to a null QUrl 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.

See also

QUrl

Access functions:
property subtitleTracksᅟ: list of QMediaMetaData

Lists the set of available subtitle tracks inside the media.

The QMediaMetaData returned describes the properties of individual tracks.

Access functions:
property videoOutputᅟ: QObject

This property holds The video output to be used by the media player..

A media player can only have one video output attached, so setting this property will replace the previously connected video output.

Setting this property to nullptr will disable video output.

Access functions:
property videoTracksᅟ: list of QMediaMetaData

Lists the set of available video tracks inside the media.

The QMediaMetaData returned describes the properties of individual tracks.

Access functions:
__init__([parent=None])
Parameters:

parentQObject

Constructs a QMediaPlayer instance as a child of parent.

activeAudioTrack()
Return type:

int

Getter of property activeAudioTrackᅟ .

activeSubtitleTrack()
Return type:

int

Getter of property activeSubtitleTrackᅟ .

activeTracksChanged()

Notification signal of property activeAudioTrackᅟ .

activeVideoTrack()
Return type:

int

Getter of property activeVideoTrackᅟ .

audioBufferOutput()
Return type:

QAudioBufferOutput

Getter of property audioBufferOutputᅟ .

audioBufferOutputChanged()

Notification signal of property audioBufferOutputᅟ .

audioOutput()
Return type:

QAudioOutput

See also

setAudioOutput()

Getter of property audioOutputᅟ .

audioOutputChanged()

Notification signal of property audioOutputᅟ .

audioTracks()
Return type:

.list of QMediaMetaData

Getter of property audioTracksᅟ .

bufferProgress()
Return type:

float

Returns a number between 0 and 1 when buffering data.

0 means that there is no buffered data available, playback is usually stalled in this case. Playback will resume once the buffer reaches 1, meaning enough data has been buffered to be able to resume playback.

bufferProgress() will always return 1 for local files.

Getter of property bufferProgressᅟ .

bufferProgressChanged(progress)
Parameters:

progress – float

Signals the amount of the local buffer filled as a number between 0 and 1.

Notification signal of property bufferProgressᅟ .

bufferedTimeRange()
Return type:

QMediaTimeRange

Returns a QMediaTimeRange describing the currently buffered data.

When streaming media from a remote source, different parts of the media file can be available locally. The returned QMediaTimeRange object describes the time ranges that are buffered and available for immediate playback.

See also

QMediaTimeRange

duration()
Return type:

int

Returns the duration of the current media in ms.

Returns 0 if the media player doesn’t have a valid media file or stream. For live streams, the duration usually changes during playback as more data becomes available.

Getter of property durationᅟ .

durationChanged(duration)
Parameters:

duration – int

Signals the duration of the content has changed to duration, expressed in milliseconds.

Notification signal of property durationᅟ .

error()
Return type:

Error

Returns the current error state.

Getter of property errorᅟ .

errorChanged()

Notification signal of property errorᅟ .

errorOccurred(error, errorString)
Parameters:
  • errorError

  • errorString – str

Signals that an error condition has occurred, with errorString containing a description of the error.

See also

errorString()

errorString()
Return type:

str

Getter of property errorStringᅟ .

hasAudio()
Return type:

bool

Getter of property hasAudioᅟ .

hasAudioChanged(available)
Parameters:

available – bool

Signals the availability of audio content has changed to available.

Notification signal of property hasAudioᅟ .

hasVideo()
Return type:

bool

Getter of property hasVideoᅟ .

hasVideoChanged(videoAvailable)
Parameters:

videoAvailable – bool

Signals the availability of visual content has changed to videoAvailable.

Notification signal of property hasVideoᅟ .

isAvailable()
Return type:

bool

Returns true if the media player is supported on this platform.

isPlaying()
Return type:

bool

Getter of property playingᅟ .

isSeekable()
Return type:

bool

Returns true if the media is seekable. Most file based media files are seekable, but live streams usually are not.

See also

position

Getter of property seekableᅟ .

loops()
Return type:

int

See also

setLoops()

Getter of property loopsᅟ .

loopsChanged()

Notification signal of property loopsᅟ .

mediaStatus()
Return type:

MediaStatus

Getter of property mediaStatusᅟ .

mediaStatusChanged(status)
Parameters:

statusMediaStatus

Signals that the status of the current media has changed.

See also

mediaStatus()

Notification signal of property mediaStatusᅟ .

metaData()
Return type:

QMediaMetaData

Getter of property metaDataᅟ .

metaDataChanged()

Notification signal of property metaDataᅟ .

pause()

Pause playing the current source.

See also

play() stop()

play()

Start or resume playing the current source.

See also

pause() stop()

playbackRate()
Return type:

float

Returns the current playback rate.

Getter of property playbackRateᅟ .

playbackRateChanged(rate)
Parameters:

rate – float

Signals the playbackRate has changed to rate.

Notification signal of property playbackRateᅟ .

playbackState()
Return type:

PlaybackState

Getter of property playbackStateᅟ .

playbackStateChanged(newState)
Parameters:

newStatePlaybackState

Notification signal of property playbackStateᅟ .

playingChanged(playing)
Parameters:

playing – bool

Notification signal of property playingᅟ .

position()
Return type:

int

Returns the current position inside the media being played back in ms.

Returns 0 if the media player doesn’t have a valid media file or stream. For live streams, the duration usually changes during playback as more data becomes available.

See also

setPosition()

Getter of property positionᅟ .

positionChanged(position)
Parameters:

position – int

Signals the position of the content has changed to position, expressed in milliseconds.

Notification signal of property positionᅟ .

seekableChanged(seekable)
Parameters:

seekable – bool

Signals the seekable status of the player object has changed.

Notification signal of property seekableᅟ .

setActiveAudioTrack(index)
Parameters:

index – int

Setter of property activeAudioTrackᅟ .

setActiveSubtitleTrack(index)
Parameters:

index – int

Setter of property activeSubtitleTrackᅟ .

setActiveVideoTrack(index)
Parameters:

index – int

Setter of property activeVideoTrackᅟ .

setAudioBufferOutput(output)
Parameters:

outputQAudioBufferOutput

Setter of property audioBufferOutputᅟ .

setAudioOutput(output)
Parameters:

outputQAudioOutput

See also

audioOutput()

Setter of property audioOutputᅟ .

setLoops(loops)
Parameters:

loops – int

See also

loops()

Setter of property loopsᅟ .

setPlaybackRate(rate)
Parameters:

rate – float

See also

playbackRate()

Setter of property playbackRateᅟ .

setPosition(position)
Parameters:

position – int

See also

position()

Setter of property positionᅟ .

setSource(source)
Parameters:

sourceQUrl

Sets the current source.

Setting the media to a null QUrl 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. Setting the media will stop the playback.

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.

Note

FFmpeg, used by the FFmpeg media backend, restricts use of nested protocols for security reasons. In controlled environments where all inputs are trusted, the list of approved protocols can be overridden using the QT_FFMPEG_PROTOCOL_WHITELIST environment variable. This environment variable is Qt’s private API and can change between patch releases without notice.

See also

source()

Setter of property sourceᅟ .

setSourceDevice(device[, sourceUrl=QUrl()])
Parameters:

Sets the current source device.

The media data will be read from device. The sourceUrl can be provided to resolve additional information about the media, mime type etc. The device must be open and readable.

For macOS the device should also be seek-able.

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 if an error occurs during loading.

See also

sourceDevice()

setVideoOutput(arg__1)
Parameters:

arg__1QObject

See also

videoOutput()

Setter of property videoOutputᅟ .

setVideoSink(sink)
Parameters:

sinkQVideoSink

Sets sink to be the QVideoSink instance to retrieve video data.

See also

videoSink()

source()
Return type:

QUrl

See also

setSource()

Getter of property sourceᅟ .

sourceChanged(media)
Parameters:

mediaQUrl

Signals that the media source has been changed to media.

Notification signal of property sourceᅟ .

sourceDevice()
Return type:

QIODevice

Returns the stream source of media data.

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

stop()

Stop playing, and reset the play position to the beginning.

See also

play() pause()

subtitleTracks()
Return type:

.list of QMediaMetaData

Getter of property subtitleTracksᅟ .

tracksChanged()

Notification signal of property audioTracksᅟ .

videoOutput()
Return type:

QObject

See also

setVideoOutput()

Getter of property videoOutputᅟ .

videoOutputChanged()

Notification signal of property videoOutputᅟ .

videoSink()
Return type:

QVideoSink

Returns the QVideoSink instance.

See also

setVideoSink()

videoTracks()
Return type:

.list of QMediaMetaData

Getter of property videoTracksᅟ .