QMediaPlayer Class

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

Header: #include <QMediaPlayer>
qmake: QT += multimedia
Inherits: QMediaObject

Public Types

enum Error { NoError, ResourceError, FormatError, NetworkError, AccessDeniedError, ServiceMissingError }
enum Flag { LowLatency, StreamPlayback, VideoSurface }
enum MediaStatus { UnknownMediaStatus, NoMedia, LoadingMedia, LoadedMedia, StalledMedia, …, InvalidMedia }
enum State { StoppedState, PlayingState, PausedState }

Properties

Public Functions

QAudio::Role audioRole() const
int bufferStatus() const
QMediaContent currentMedia() const
QString customAudioRole() const
qint64 duration() const
QString errorString() const
bool isAudioAvailable() const
bool isMuted() const
bool isSeekable() const
bool isVideoAvailable() const
QMediaContent media() const
QMediaPlayer::MediaStatus mediaStatus() const
qreal playbackRate() const
QMediaPlaylist *playlist() const
qint64 position() const
void setAudioRole(QAudio::Role audioRole)
void setCustomAudioRole(const QString &audioRole)
QMediaPlayer::State state() const
int volume() const

Public Slots

void setMedia(const QMediaContent &media, QIODevice *stream = nullptr)
void setMuted(bool muted)
void setPlaybackRate(qreal rate)
void setPlaylist(QMediaPlaylist *playlist)
void setPosition(qint64 position)
void setVolume(int volume)

Signals

void audioAvailableChanged(bool available)
void audioRoleChanged(QAudio::Role role)
void bufferStatusChanged(int percentFilled)
void currentMediaChanged(const QMediaContent &media)
void customAudioRoleChanged(const QString &role)
void durationChanged(qint64 duration)
void mediaChanged(const QMediaContent &media)
void mediaStatusChanged(QMediaPlayer::MediaStatus status)
void mutedChanged(bool muted)
void playbackRateChanged(qreal rate)
void positionChanged(qint64 position)
void seekableChanged(bool seekable)
void stateChanged(QMediaPlayer::State newState)
void videoAvailableChanged(bool videoAvailable)
void volumeChanged(int volume)

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 (QMediaObject::metaData() and predefined meta-data keys)
  • Checking to see if the media playback service is currently available (QMediaObject::availability())

See also QMediaObject, QMediaService, QVideoWidget, and QMediaPlaylist.

Member Type Documentation

enum QMediaPlayer::Error

Defines a media player error condition.

ConstantValueDescription
QMediaPlayer::NoError0No error has occurred.
QMediaPlayer::ResourceError1A media resource couldn't be resolved.
QMediaPlayer::FormatError2The format of a media resource isn't (fully) supported. Playback may still be possible, but without an audio or video component.
QMediaPlayer::NetworkError3A network error occurred.
QMediaPlayer::AccessDeniedError4There are not the appropriate permissions to play a media resource.
QMediaPlayer::ServiceMissingError5A valid playback service was not found, playback cannot proceed.

enum QMediaPlayer::Flag

ConstantValueDescription
QMediaPlayer::LowLatency0x01The 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::StreamPlayback0x02The player is expected to play QIODevice based streams. If passed to QMediaPlayer constructor, the service supporting streams playback will be chosen.
QMediaPlayer::VideoSurface0x04The player is expected to be able to render to a QAbstractVideoSurface output.

enum QMediaPlayer::MediaStatus

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

ConstantValueDescription
QMediaPlayer::UnknownMediaStatus0The status of the media cannot be determined.
QMediaPlayer::NoMedia1There is no current media. The player is in the StoppedState.
QMediaPlayer::LoadingMedia2The current media is being loaded. The player may be in any state.
QMediaPlayer::LoadedMedia3The current media has been loaded. The player is in the StoppedState.
QMediaPlayer::StalledMedia4Playback of the current media has stalled due to insufficient buffering or some other temporary interruption. The player is in the PlayingState or PausedState.
QMediaPlayer::BufferingMedia5The 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::BufferedMedia6The player has fully buffered the current media. The player is in the PlayingState or PausedState.
QMediaPlayer::EndOfMedia7Playback has reached the end of the current media. The player is in the StoppedState.
QMediaPlayer::InvalidMedia8The current media cannot be played. The player is in the StoppedState.

enum QMediaPlayer::State

Defines the current state of a media player.

ConstantValueDescription
QMediaPlayer::StoppedState0The media player is not playing content, playback will begin from the start of the current track.
QMediaPlayer::PlayingState1The media player is currently playing content.
QMediaPlayer::PausedState2The media player has paused playback, playback of the current track will resume from the position the player was paused at.

Property Documentation

audioAvailable : const 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.

Access functions:

bool isAudioAvailable() const

Notifier signal:

void audioAvailableChanged(bool available)

audioRole : QAudio::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 QAudio::CustomRole.

This property was introduced in Qt 5.6.

Access functions:

QAudio::Role audioRole() const
void setAudioRole(QAudio::Role audioRole)

Notifier signal:

void audioRoleChanged(QAudio::Role role)

See also supportedAudioRoles().

bufferStatus : const 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.

Access functions:

int bufferStatus() const

Notifier signal:

void bufferStatusChanged(int percentFilled)

See also mediaStatus().

currentMedia : const QMediaContent

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

Access functions:

QMediaContent currentMedia() const

Notifier signal:

void currentMediaChanged(const QMediaContent &media)

See also QMediaContent and media().

customAudioRole : QString

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 QAudio::CustomRole when this property is set.

This property was introduced in Qt 5.11.

Access functions:

QString customAudioRole() const
void setCustomAudioRole(const QString &audioRole)

Notifier signal:

void customAudioRoleChanged(const QString &role)

See also supportedCustomAudioRoles().

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)

error : const QString

This property holds a string describing the last error condition.

Access functions:

QString errorString() const

See also error().

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

Access functions:

QMediaContent media() const
void setMedia(const QMediaContent &media, QIODevice *stream = nullptr)

Notifier signal:

void mediaChanged(const QMediaContent &media)

See also QMediaContent and currentMedia().

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)

See also state.

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.

Access functions:

bool isMuted() const
void setMuted(bool muted)

Notifier signal:

void mutedChanged(bool muted)

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)

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

Access functions:

QMediaPlaylist *playlist() const
void setPlaylist(QMediaPlaylist *playlist)

See also QMediaContent.

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 signal positionChanged(), the interval between updates can be set with QMediaObject's method setNotifyInterval().

Access functions:

qint64 position() const
void setPosition(qint64 position)

Notifier signal:

void positionChanged(qint64 position)

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)

state : const State

This property holds the media player's playback state.

By default this property is QMediaPlayer::Stopped

Access functions:

QMediaPlayer::State state() const

Notifier signal:

void stateChanged(QMediaPlayer::State newState)

See also mediaStatus(), play(), pause(), and stop().

videoAvailable : const 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.

Access functions:

bool isVideoAvailable() const

Notifier signal:

void videoAvailableChanged(bool videoAvailable)

See also QVideoWidget and QMediaContent.

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 QAudio::convertVolume() for more details.

Access functions:

int volume() const
void setVolume(int volume)

Notifier signal:

void volumeChanged(int volume)