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 | 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 |
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 | setVideoOutput(QObject *output) |
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 | 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 | 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 | 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; connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); player->setSource(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); player->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::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
[read-only]
bufferProgress : const float
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:
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]
hasAudio : const bool
This property holds the audio availabilty status for the current media.
Note: As the life time of QMediaPlayer can be longer than the playback of one QUrl, this property may change over time.
Access functions:
bool | hasAudio() const |
Notifier signal:
void | hasAudioChanged(bool available) |
[read-only]
hasVideo : 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.
Note: As the life time of QMediaPlayer can be longer than the playback of one QUrl, this property may change over time.
Access functions:
bool | hasVideo() const |
Notifier signal:
void | hasVideoChanged(bool videoAvailable) |
See also QVideoWidget and QUrl.
[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) |
See also state.
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 signal positionChanged().
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.
Member Function Documentation
QMediaPlayer::QMediaPlayer(QObject *parent = nullptr)
Construct a QMediaPlayer instance parented to parent and with flags.
[signal]
void QMediaPlayer::bufferProgressChanged(float filled)
Signal 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)
Signal 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)
Signal 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.
[slot]
void QMediaPlayer::play()
Start or resume playing the current source.
[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)
Signal 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 url can be provided to resolve additional information about the media such as mime type. The device must be open and readable. For macOS the device should be also seekable.
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.
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.
[virtual]
QMediaPlayer::~QMediaPlayer()
Destroys the player object.
int QMediaPlayer::activeAudioTrack() const
Returns the currently active audio track.
See also setActiveAudioTrack().
int QMediaPlayer::activeSubtitleTrack() const
Returns the currently active subtitle track.
See also setActiveSubtitleTrack().
int QMediaPlayer::activeVideoTrack() const
Returns the currently active video track.
See also setActiveVideoTrack().
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.
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 it's creation date.
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().
void QMediaPlayer::setActiveAudioTrack(int index)
Sets the currently active audio track.
By default, the first available audio track will be chosen.
Set to -1 to disable all audio tracks.
See also activeAudioTrack().
void QMediaPlayer::setActiveSubtitleTrack(int index)
Sets the currently active subtitle track.
Setting the property to -1 will disable subtitles.
Subtitles are disabled by default.
See also activeSubtitleTrack().
void QMediaPlayer::setActiveVideoTrack(int index)
Sets the currently active video track.
By default, the first available video track will be chosen.
See also activeVideoTrack().
void QMediaPlayer::setAudioOutput(QAudioOutput *output)
Sets the audio output to device.
Setting a null QAudioDevice, sets the output to the system default.
Returns true if the output could be changed, false otherwise.
Note: Setter function for property audioOutput.
See also audioOutput().
void QMediaPlayer::setVideoOutput(QObject *output)
Attach a video output to the media player.
If the media player has already video output attached, it will be replaced with a new one.
Note: Setter function for property videoOutput.
See also videoOutput().
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.
QList<QMediaMetaData> QMediaPlayer::videoTracks() const
Lists the set of available video tracks inside the media.
The QMediaMetaData returned describes the properties of individual tracks.