W3cubDocs

/Qt

QMediaPlayer Class

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

Header: #include <QMediaPlayer>
CMake: find_package(Qt6 COMPONENTS Multimedia REQUIRED) target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia
Instantiated By: MediaPlayer
Inherits: QObject

Public Types

enum Error { NoError, ResourceError, FormatError, NetworkError, AccessDeniedError }
enum Loops { Infinite, Once }
enum MediaStatus { NoMedia, LoadingMedia, LoadedMedia, StalledMedia, BufferingMedia, …, InvalidMedia }
enum PlaybackState { StoppedState, PlayingState, PausedState }

Properties

Public Functions

QMediaPlayer(QObject *parent = nullptr)
virtual ~QMediaPlayer()
int activeAudioTrack() const
int activeSubtitleTrack() const
int activeVideoTrack() const
QAudioOutput * audioOutput() const
QList<QMediaMetaData> audioTracks() const
float bufferProgress() const
QMediaTimeRange bufferedTimeRange() const
qint64 duration() const
QMediaPlayer::Error error() const
QString errorString() const
bool hasAudio() const
bool hasVideo() const
bool isAvailable() const
bool isSeekable() const
int loops() const
QMediaPlayer::MediaStatus mediaStatus() const
QMediaMetaData metaData() const
qreal playbackRate() const
QMediaPlayer::PlaybackState playbackState() const
qint64 position() const
void setActiveAudioTrack(int index)
void setActiveSubtitleTrack(int index)
void setActiveVideoTrack(int index)
void setAudioOutput(QAudioOutput *output)
void setLoops(int loops)
void setVideoOutput(QObject *)
QUrl source() const
const QIODevice * sourceDevice() const
QList<QMediaMetaData> subtitleTracks() const
QObject * videoOutput() const
QList<QMediaMetaData> videoTracks() const

Public Slots

void pause()
void play()
void setPlaybackRate(qreal rate)
void setPosition(qint64 position)
void setSource(const QUrl &source)
void setSourceDevice(QIODevice *device, const QUrl &sourceUrl = QUrl())
void stop()

Signals

void activeTracksChanged()
void audioOutputChanged()
void bufferProgressChanged(float filled)
void durationChanged(qint64 duration)
void errorChanged()
void errorOccurred(QMediaPlayer::Error error, const QString &errorString)
void hasAudioChanged(bool available)
void hasVideoChanged(bool videoAvailable)
void loopsChanged()
void mediaStatusChanged(QMediaPlayer::MediaStatus status)
void metaDataChanged()
void playbackRateChanged(qreal rate)
void playbackStateChanged(QMediaPlayer::PlaybackState newState)
void positionChanged(qint64 position)
void seekableChanged(bool seekable)
void sourceChanged(const QUrl &media)
void tracksChanged()
void videoOutputChanged()

Detailed Description

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 = new QMediaPlayer;
audioOutput = new QAudioOutput;
player->setAudioOutput(audioOutput);
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
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.

Member Type Documentation

enum QMediaPlayer::Error

Defines a media player error condition.

Constant Value Description
QMediaPlayer::NoError 0 No error has occurred.
QMediaPlayer::ResourceError 1 A media resource couldn't be resolved.
QMediaPlayer::FormatError 2 The format of a media resource isn't (fully) supported. Playback may still be possible, but without an audio or video component.
QMediaPlayer::NetworkError 3 A network error occurred.
QMediaPlayer::AccessDeniedError 4 There are not the appropriate permissions to play a media resource.

enum QMediaPlayer::Loops

Some predefined constants for the loops property.

Constant Value Description
QMediaPlayer::Infinite -1 Loop forever.
QMediaPlayer::Once 1 Play the media once (the default).

enum QMediaPlayer::MediaStatus

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

Constant Value Description
QMediaPlayer::NoMedia 0 The is no current media. The player is in the StoppedState.
QMediaPlayer::LoadingMedia 1 The current media is being loaded. The player may be in any state.
QMediaPlayer::LoadedMedia 2 The current media has been loaded. The player is in the StoppedState.
QMediaPlayer::StalledMedia 3 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 4 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 5 The player has fully buffered the current media. The player is in the PlayingState or PausedState.
QMediaPlayer::EndOfMedia 6 Playback has reached the end of the current media. The player is in the StoppedState.
QMediaPlayer::InvalidMedia 7 The current media cannot be played. The player is in the StoppedState.

enum QMediaPlayer::PlaybackState

Defines the current state of a media player.

Constant Value Description
QMediaPlayer::StoppedState 0 The media player is not playing content, playback will begin from the start of the current track.
QMediaPlayer::PlayingState 1 The media player is currently playing content.
QMediaPlayer::PausedState 2 The media player has paused playback, playback of the current track will resume from the position the player was paused at.

Property Documentation

activeAudioTrack : int

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:

int activeAudioTrack() const
void setActiveAudioTrack(int index)

Notifier signal:

void activeTracksChanged()

activeSubtitleTrack : int

Returns the currently active subtitle track.

Set index to -1 to disable subtitles.

Subtitles are disabled by default.

Access functions:

int activeSubtitleTrack() const
void setActiveSubtitleTrack(int index)

Notifier signal:

void activeTracksChanged()

activeVideoTrack : int

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:

int activeVideoTrack() const
void setActiveVideoTrack(int index)

Notifier signal:

void activeTracksChanged()

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:

QAudioOutput * audioOutput() const
void setAudioOutput(QAudioOutput *output)

Notifier signal:

void audioOutputChanged()

[read-only] bufferProgress : const 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.

Access functions:

float bufferProgress() const

Notifier signal:

void bufferProgressChanged(float filled)

See also mediaStatus().

[read-only] duration : const qint64

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:

qint64 duration() const

Notifier signal:

void durationChanged(qint64 duration)

[read-only] error : const Error

This property holds a string describing the last error condition.

Access functions:

QMediaPlayer::Error error() const

Notifier signal:

void errorChanged()

See also error().

[read-only] errorString : const QString

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

Access functions:

QString errorString() const

Notifier signal:

void errorChanged()

[read-only] hasAudio : const bool

This property holds whether the media contains audio.

Access functions:

bool hasAudio() const

Notifier signal:

void hasAudioChanged(bool available)

[read-only] hasVideo : const bool

This property holds whether the media contains video.

Access functions:

bool hasVideo() const

Notifier signal:

void hasVideoChanged(bool videoAvailable)

loops : int

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

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

Access functions:

int loops() const
void setLoops(int loops)

Notifier signal:

void loopsChanged()

[read-only] mediaStatus : const 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 QMediaPlayer::NoMedia

Access functions:

QMediaPlayer::MediaStatus mediaStatus() const

Notifier signal:

void mediaStatusChanged(QMediaPlayer::MediaStatus status)

playbackRate : qreal

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.

Access functions:

qreal playbackRate() const
void setPlaybackRate(qreal rate)

Notifier signal:

void playbackRateChanged(qreal rate)

position : qint64

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:

qint64 position() const
void setPosition(qint64 position)

Notifier signal:

void positionChanged(qint64 position)

[read-only] seekable : const 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:

bool isSeekable() const

Notifier signal:

void seekableChanged(bool seekable)

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.

Access functions:

QUrl source() const
void setSource(const QUrl &source)

Notifier signal:

void sourceChanged(const QUrl &media)

See also QUrl.

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:

QObject * videoOutput() const
void setVideoOutput(QObject *)

Notifier signal:

void videoOutputChanged()

Member Function Documentation

QMediaPlayer::QMediaPlayer(QObject *parent = nullptr)

Constructs a QMediaPlayer instance as a child of parent.

[signal] void QMediaPlayer::bufferProgressChanged(float filled)

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

Note: Notifier signal for property bufferProgress.

[signal] void QMediaPlayer::durationChanged(qint64 duration)

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

Note: Notifier signal for property duration.

[signal] void QMediaPlayer::errorOccurred(QMediaPlayer::Error error, const QString &errorString)

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

See also errorString().

[signal] void QMediaPlayer::hasAudioChanged(bool available)

Signals the availability of audio content has changed to available.

Note: Notifier signal for property hasAudio.

[signal] void QMediaPlayer::hasVideoChanged(bool videoAvailable)

Signals the availability of visual content has changed to videoAvailable.

Note: Notifier signal for property hasVideo.

[signal] void QMediaPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)

Signals that the status of the current media has changed.

Note: Notifier signal for property mediaStatus.

See also mediaStatus().

[slot] void QMediaPlayer::pause()

Pause playing the current source.

See also play() and stop().

[slot] void QMediaPlayer::play()

Start or resume playing the current source.

See also pause() and stop().

[signal] void QMediaPlayer::playbackRateChanged(qreal rate)

Signals the playbackRate has changed to rate.

Note: Notifier signal for property playbackRate.

[signal] void QMediaPlayer::positionChanged(qint64 position)

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

Note: Notifier signal for property position.

[signal] void QMediaPlayer::seekableChanged(bool seekable)

Signals the seekable status of the player object has changed.

Note: Notifier signal for property seekable.

[slot] void QMediaPlayer::setSource(const QUrl &source)

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.

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: Setter function for property source.

See also source().

[slot] void QMediaPlayer::setSourceDevice(QIODevice *device, const QUrl &sourceUrl = QUrl())

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

[signal] void QMediaPlayer::sourceChanged(const QUrl &media)

Signals that the media source has been changed to media.

Note: Notifier signal for property source.

[slot] void QMediaPlayer::stop()

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

See also play() and pause().

[virtual] QMediaPlayer::~QMediaPlayer()

Destroys the player object.

QList<QMediaMetaData> QMediaPlayer::audioTracks() const

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.

Note: Getter function for property audioTracks.

float QMediaPlayer::bufferProgress() const

Returns a number betwee 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.

Note: Getter function for property bufferProgress.

QMediaTimeRange QMediaPlayer::bufferedTimeRange() const

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.

qint64 QMediaPlayer::duration() const

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.

Note: Getter function for property duration.

QMediaPlayer::Error QMediaPlayer::error() const

Returns the current error state.

Note: Getter function for property error.

bool QMediaPlayer::isAvailable() const

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

bool QMediaPlayer::isSeekable() const

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

Note: Getter function for property seekable.

See also position.

QMediaMetaData QMediaPlayer::metaData() const

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.

Note: Getter function for property metaData.

qreal QMediaPlayer::playbackRate() const

Returns the current playback rate.

Note: Getter function for property playbackRate.

See also setPlaybackRate().

qint64 QMediaPlayer::position() const

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.

Note: Getter function for property position.

See also setPosition().

const QIODevice *QMediaPlayer::sourceDevice() const

Returns the stream source of media data.

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

See also setSourceDevice() and setSource().

QList<QMediaMetaData> QMediaPlayer::subtitleTracks() const

Lists the set of available subtitle tracks inside the media.

The QMediaMetaData returned describes the properties of individual tracks.

Note: Getter function for property subtitleTracks.

QList<QMediaMetaData> QMediaPlayer::videoTracks() const

Lists the set of available video tracks inside the media.

The QMediaMetaData returned describes the properties of individual tracks.

Note: Getter function for property videoTracks.

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.2/qmediaplayer.html