IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QMediaPlayer Class

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QMediaPlayer Class

  • Header: QMediaPlayer

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Multimedia)

    target_link_libraries(mytarget PRIVATE Qt6::Multimedia)

  • qmake: QT += multimedia

  • Inherited By:

  • Instantiated By: qml-qtmultimedia-mediaplayer.xml

  • Inherits: QObject

  • Inherited By:

  • Group: QMediaPlayer is part of multimedia, multimedia_playback, multimedia_video

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.

 
Sélectionnez
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

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:

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:

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:

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:

  • *audioOutput() const

  • void setAudioOutput( *output)

Notifier signal:

  • void audioOutputChanged()

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

  • <> audioTracks() const

Notifier signal:

[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:

Notifier signal:

See Also

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:

Notifier signal:

[read-only] error : const Error

This property holds a string describing the last error condition.

Access functions:

Notifier signal:

See Also

See also error()

[read-only] errorString : const QString

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

Access functions:

  • errorString() const

Notifier signal:

[read-only] hasAudio : const bool

This property holds whether the media contains audio.

Access functions:

  • bool hasAudio() const

Notifier signal:

[read-only] hasVideo : const bool

This property holds whether the media contains video.

Access functions:

  • bool hasVideo() const

Notifier signal:

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:

  • mediaStatus() const

Notifier signal:

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

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

Access functions:

  • metaData() const

Notifier signal:

  • void metaDataChanged()

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:

[read-only] playbackState : const PlaybackState

Returns the PlaybackState.

Access functions:

  • playbackState() const

Notifier signal:

  • void playbackStateChanged( newState)

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:

[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:

Notifier signal:

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:

  • source() const

  • void setSource(const &source)

Notifier signal:

See Also

See also QUrl

[read-only] subtitleTracks : const QList<QMediaMetaData>

Lists the set of available subtitle tracks inside the media.

The QMediaMetaData returned describes the properties of individual tracks.

Access functions:

  • <> subtitleTracks() const

Notifier signal:

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:

  • *videoOutput() const

  • void setVideoOutput(QObject *)

Notifier signal:

  • void videoOutputChanged()

Member Function Documentation

 

[explicit] QMediaPlayer::QMediaPlayer(QObject *parent = nullptr)

Constructs a QMediaPlayer instance as a child of parent.

[virtual] QMediaPlayer::~QMediaPlayer()

Destroys the player object.

float QMediaPlayer::bufferProgress() const

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 function for property bufferProgress.

void QMediaPlayer::bufferProgressChanged(float filled)

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

Notifier signal 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

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.

Getter function for property duration.

void QMediaPlayer::durationChanged(qint64 duration)

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

Notifier signal for property duration.

QMediaPlayer::Error QMediaPlayer::error() const

Returns the current error state.

Getter function for property error.

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

See also errorString()

void QMediaPlayer::hasAudioChanged(bool available)

Signals the availability of audio content has changed to available.

Notifier signal for property hasAudio.

void QMediaPlayer::hasVideoChanged(bool videoAvailable)

Signals the availability of visual content has changed to videoAvailable.

Notifier signal for property hasVideo.

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.

Getter function for property seekable.

See Also

See also position

void QMediaPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)

Signals that the status of the current media has changed.

Notifier signal for property mediaStatus.

See Also

See also mediaStatus()

void QMediaPlayer::pause()

Pause playing the current source.

See Also

See also play(), stop()

void QMediaPlayer::play()

Start or resume playing the current source.

See Also

See also pause(), stop()

qreal QMediaPlayer::playbackRate() const

Returns the current playback rate.

Getter function for property playbackRate.

See Also

See also setPlaybackRate()

void QMediaPlayer::playbackRateChanged(qreal rate)

Signals the playbackRate has changed to rate.

Notifier signal for property playbackRate.

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.

Getter function for property position.

See Also

See also setPosition()

void QMediaPlayer::positionChanged(qint64 position)

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

Notifier signal for property position.

void QMediaPlayer::seekableChanged(bool seekable)

Signals the seekable status of the player object has changed.

Notifier signal for property seekable.

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.

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.

Setter function for property source.

See Also

See also source()

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.

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

See also sourceDevice()

void QMediaPlayer::setVideoSink(QVideoSink *sink)

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

See Also

See also videoSink()

void QMediaPlayer::sourceChanged(const QUrl &media)

Signals that the media source has been changed to media.

Notifier signal for property source.

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

See also setSourceDevice(), setSource()

void QMediaPlayer::stop()

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

See Also

See also play(), pause()

QVideoSink *QMediaPlayer::videoSink() const

Returns the QVideoSink instance.

See Also

See also setVideoSink()

QList<QMediaMetaData> QMediaPlayer::videoTracks() const

Lists the set of available video tracks inside the media.

The QMediaMetaData returned describes the properties of individual tracks.

Getter function for property videoTracks.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+