Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmediaplayer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/qaudiooutput.h>
7#include <QtMultimedia/qvideosink.h>
8#include <QtMultimedia/private/qaudiobufferoutput_p.h>
9#include <QtMultimedia/private/qmultimediautils_p.h>
10#include <QtMultimedia/private/qplatformmediaintegration_p.h>
11
12#include <QtCore/qdebug.h>
13#include <QtCore/qtemporaryfile.h>
14
16
17using TrackType = QPlatformMediaPlayer::TrackType;
18
19/*!
20 \class QMediaPlayer
21 \brief The QMediaPlayer class allows the playing of a media files.
22 \inmodule QtMultimedia
23 \ingroup multimedia
24 \ingroup multimedia_playback
25 \ingroup multimedia_video
26
27 The QMediaPlayer class is a high level media playback class. It can be used
28 to playback audio of video media files. The content
29 to playback is specified as a QUrl object.
30
31 \snippet multimedia-snippets/media.cpp Player
32
33 QVideoWidget can be used with QMediaPlayer for video rendering.
34
35 \sa QVideoWidget
36*/
37
38/*!
39 \qmltype MediaPlayer
40 \nativetype QMediaPlayer
41 \brief Adds media playback to a scene.
42
43 \inqmlmodule QtMultimedia
44 \ingroup multimedia_qml
45 \ingroup multimedia_audio_qml
46 \ingroup multimedia_video_qml
47
48 \qml
49 Text {
50 text: "Click Me!";
51 font.pointSize: 24;
52 width: 150; height: 50;
53
54 MediaPlayer {
55 id: playMusic
56 source: "music.wav"
57 audioOutput: AudioOutput {}
58 }
59 MouseArea {
60 anchors.fill: parent
61 onPressed: { playMusic.play() }
62 }
63 }
64 \endqml
65
66 You can use MediaPlayer together with a MultiMedia::AudioOutput to play audio content, or you can use it
67 in conjunction with a Multimedia::VideoOutput for rendering video.
68
69 \qml
70 Item {
71 MediaPlayer {
72 id: mediaplayer
73 source: "groovy_video.mp4"
74 audioOutput: AudioOutput {}
75 videoOutput: videoOutput
76 }
77
78 VideoOutput {
79 id: videoOutput
80 anchors.fill: parent
81 }
82
83 MouseArea {
84 anchors.fill: parent
85 onPressed: mediaplayer.play();
86 }
87 }
88 \endqml
89
90 \sa AudioOutput, VideoOutput
91*/
92
93void QMediaPlayerPrivate::setState(QMediaPlayer::PlaybackState toState)
94{
95 Q_Q(QMediaPlayer);
96
97 if (toState != state) {
98 const auto fromState = std::exchange(state, toState);
99 if (toState == QMediaPlayer::PlayingState || fromState == QMediaPlayer::PlayingState)
100 emit q->playingChanged(toState == QMediaPlayer::PlayingState);
101 emit q->playbackStateChanged(toState);
102 }
103}
104
105void QMediaPlayerPrivate::setStatus(QMediaPlayer::MediaStatus s)
106{
107 Q_Q(QMediaPlayer);
108
109 emit q->mediaStatusChanged(s);
110}
111
112void QMediaPlayerPrivate::setError(QMediaPlayer::Error error, const QString &errorString)
113{
114 Q_Q(QMediaPlayer);
115
116 this->error.setAndNotify(error, errorString, *q);
117}
118
119void QMediaPlayerPrivate::setMedia(QUrl media, QIODevice *stream)
120{
121 using namespace QtMultimediaPrivate;
122
123 setError(QMediaPlayer::NoError, {});
124
125 if (!control)
126 return;
127
128 media = m_sourceResolver->resolve(media);
129
130 std::unique_ptr<QFile> file;
131
132 // Some backends can't play qrc files directly.
133 // If the back end supports StreamPlayback, we pass a QFile for that resource.
134 // If it doesn't, we copy the data to a temporary file and pass its path.
135 if (!media.isEmpty() && !stream && media.scheme() == u"qrc" && !control->canPlayQrc()) {
136 qrcMedia = media;
137
138 control->mediaStatusChanged(QMediaPlayer::LoadingMedia);
139
140 file.reset(new QFile(QLatin1Char(':') + media.path()));
141 if (!file->open(QFile::ReadOnly)) {
142 file.reset();
143 control->setInvalidMediaWithError(
144 QMediaPlayer::ResourceError,
145 QMediaPlayer::tr("Attempting to play invalid Qt resource"));
146
147 } else if (control->streamPlaybackSupported()) {
148 control->setMedia(media, file.get());
149 } else {
150 auto extractedQrcMedia = qCopyQrcToTemporaryFile(*file, media);
151 if (!extractedQrcMedia) {
152 control->setInvalidMediaWithError(QMediaPlayer::ResourceError,
153 extractedQrcMedia.error());
154 return;
155 }
156 file = std::move(extractedQrcMedia->file);
157 control->setMedia(extractedQrcMedia->url, nullptr);
158 }
159 } else {
160 qrcMedia = QUrl();
161 QUrl url = qMediaFromUserInput(media);
162 if (url.scheme() == u"content" && !stream) {
163 file.reset(new QFile(media.url()));
164 stream = file.get();
165 }
166
167 control->setMedia(url, stream);
168 }
169
170 qrcFile.swap(file); // Cleans up any previous file
171}
172
174{
175 QList<QMediaMetaData> tracks;
176 if (control) {
177 int count = control->trackCount(s);
178 for (int i = 0; i < count; ++i) {
179 tracks.append(control->trackMetaData(s, i));
180 }
181 }
182 return tracks;
183}
184
185/*!
186 Constructs a QMediaPlayer instance as a child of \a{parent}.
187*/
188
189QMediaPlayer::QMediaPlayer(QObject *parent)
190 : QObject(*new QMediaPlayerPrivate, parent)
191{
192 Q_D(QMediaPlayer);
193
194 auto maybeControl = QPlatformMediaIntegration::instance()->createPlayer(this);
195 if (maybeControl) {
196 d->control = maybeControl.value();
197 d->state = d->control->state();
198 } else {
199 qWarning() << "Failed to initialize QMediaPlayer" << maybeControl.error();
200 d->setError(QMediaPlayer::ResourceError, maybeControl.error());
201 }
202}
203
204
205/*!
206 Destroys the player object.
207*/
208
209QMediaPlayer::~QMediaPlayer()
210{
211 Q_D(QMediaPlayer);
212
213 // prevents emitting audioOutputChanged and videoOutputChanged.
214 QSignalBlocker blocker(this);
215
216 // Reset audio output and video sink to ensure proper unregistering of the source
217 // To be investigated: registering of the source might be removed after switching on the ffmpeg
218 // backend;
219
220 // Workaround to prevent freeze in GStreamer when setting audioOutput while stopped
221 if (d->control)
222 d->control->qmediaplayerDestructorCalled = true;
223 setAudioOutput(nullptr);
224
225 d->setVideoSink(nullptr);
226 delete d->control;
227}
228
229QUrl QMediaPlayer::source() const
230{
231 Q_D(const QMediaPlayer);
232
233 return d->source;
234}
235
236/*!
237 Returns the stream source of media data.
238
239 This is only valid if a stream was passed to setSource().
240
241 \sa setSource()
242*/
243
244const QIODevice *QMediaPlayer::sourceDevice() const
245{
246 Q_D(const QMediaPlayer);
247
248 return d->stream;
249}
250
251/*!
252 \property QMediaPlayer::playbackState
253
254 Returns the \l{QMediaPlayer::}{PlaybackState}.
255
256 \sa playing
257*/
258QMediaPlayer::PlaybackState QMediaPlayer::playbackState() const
259{
260 Q_D(const QMediaPlayer);
261
262 // In case if EndOfMedia status is already received
263 // but state is not.
264 if (d->control
265 && d->control->mediaStatus() == QMediaPlayer::EndOfMedia
266 && d->state != d->control->state()) {
267 return d->control->state();
268 }
269
270 return d->state;
271}
272
273QMediaPlayer::MediaStatus QMediaPlayer::mediaStatus() const
274{
275 Q_D(const QMediaPlayer);
276 return d->control ? d->control->mediaStatus() : NoMedia;
277}
278
279/*!
280 Returns the duration of the current media in ms.
281
282 Returns 0 if the media player doesn't have a valid media file or stream.
283 For live streams, the duration usually changes during playback as more
284 data becomes available.
285*/
286qint64 QMediaPlayer::duration() const
287{
288 Q_D(const QMediaPlayer);
289 return d->control ? d->control->duration() : 0;
290}
291
292/*!
293 Returns the current position inside the media being played back in ms.
294
295 Returns 0 if the media player doesn't have a valid media file or stream.
296 For live streams, the duration usually changes during playback as more
297 data becomes available.
298*/
299qint64 QMediaPlayer::position() const
300{
301 Q_D(const QMediaPlayer);
302 return d->control ? d->control->position() : 0;
303}
304
305/*!
306 Returns a number between 0 and 1 when buffering data.
307
308 0 means that there is no buffered data available, playback is usually
309 stalled in this case. Playback will resume once the buffer reaches 1,
310 meaning enough data has been buffered to be able to resume playback.
311
312 bufferProgress() will always return 1 for local files.
313*/
314float QMediaPlayer::bufferProgress() const
315{
316 Q_D(const QMediaPlayer);
317 return d->control ? d->control->bufferProgress() : 0;
318}
319
320/*!
321 Returns a QMediaTimeRange describing the currently buffered data.
322
323 When streaming media from a remote source, different parts of the media
324 file can be available locally. The returned QMediaTimeRange object describes
325 the time ranges that are buffered and available for immediate playback.
326
327 \sa QMediaTimeRange
328*/
329QMediaTimeRange QMediaPlayer::bufferedTimeRange() const
330{
331 Q_D(const QMediaPlayer);
332 return d->control ? d->control->availablePlaybackRanges() : QMediaTimeRange{};
333}
334
335/*!
336 \qmlproperty bool QtMultimedia::MediaPlayer::hasAudio
337
338 This property holds whether the media contains audio.
339*/
340
341/*!
342 \property QMediaPlayer::hasAudio
343 \brief This property holds whether the media contains audio.
344*/
345bool QMediaPlayer::hasAudio() const
346{
347 Q_D(const QMediaPlayer);
348 return d->control && d->control->isAudioAvailable();
349}
350
351/*!
352 \qmlproperty bool QtMultimedia::MediaPlayer::hasVideo
353
354 This property holds whether the media contains video.
355*/
356
357/*!
358 \property QMediaPlayer::hasVideo
359 \brief This property holds whether the media contains video.
360*/
361bool QMediaPlayer::hasVideo() const
362{
363 Q_D(const QMediaPlayer);
364 return d->control && d->control->isVideoAvailable();
365}
366
367/*!
368 Returns true if the media is seekable. Most file based media files are seekable,
369 but live streams usually are not.
370
371 \sa position
372*/
373bool QMediaPlayer::isSeekable() const
374{
375 Q_D(const QMediaPlayer);
376 return d->control && d->control->isSeekable();
377}
378
379bool QMediaPlayer::isPlaying() const
380{
381 Q_D(const QMediaPlayer);
382 return d->state == QMediaPlayer::PlayingState;
383}
384
385/*!
386 Returns the current playback rate.
387*/
388qreal QMediaPlayer::playbackRate() const
389{
390 Q_D(const QMediaPlayer);
391 return d->control ? d->control->playbackRate() : 0.;
392}
393
394/*!
395 \enum QMediaPlayer::Loops
396
397 Some predefined constants for the \l loops property.
398
399 \value Infinite Loop forever.
400 \value Once Play the media once (the default).
401*/
402
403/*!
404 \property QMediaPlayer::loops
405
406 Determines how often the media is played before the player stops.
407 Set to QMediaPlayer::Infinite to loop the current media file forever.
408
409 The default value is \c 1. Setting this property to \c 0 has no effect.
410*/
411
412/*!
413 \qmlproperty int QtMultimedia::MediaPlayer::loops
414
415 Determines how often the media is played before the player stops.
416 Set to MediaPlayer::Infinite to loop the current media file forever.
417
418 The default value is \c 1. Setting this property to \c 0 has no effect.
419*/
420int QMediaPlayer::loops() const
421{
422 Q_D(const QMediaPlayer);
423 return d->control ? d->control->loops() : 1;
424}
425
426void QMediaPlayer::setLoops(int loops)
427{
428 Q_D(QMediaPlayer);
429 if (loops == 0)
430 return;
431 if (d->control)
432 d->control->setLoops(loops);
433}
434
435/*!
436 Returns the current error state.
437*/
438QMediaPlayer::Error QMediaPlayer::error() const
439{
440 return d_func()->error.code();
441}
442
443/*!
444 \qmlproperty string QtMultimedia::MediaPlayer::errorString
445
446 This property holds a string describing the current error condition in more
447 detail.
448*/
449
450/*!
451 \property QMediaPlayer::errorString
452 \brief This property holds a string describing the current error condition in
453 more detail.
454*/
455QString QMediaPlayer::errorString() const
456{
457 return d_func()->error.description();
458}
459
460/*!
461 \qmlmethod void QtMultimedia::MediaPlayer::play()
462
463 Starts or resumes playback of the media.
464
465 Sets the \l playbackState property to PlayingState, and changes
466 \l playing to \c true.
467*/
468
469/*!
470 Start or resume playing the current source.
471
472 \sa pause(), stop()
473*/
474void QMediaPlayer::play()
475{
476 Q_D(QMediaPlayer);
477
478 if (!d->control)
479 return;
480
481 d->control->play();
482}
483
484/*!
485 \qmlmethod void QtMultimedia::MediaPlayer::pause()
486
487 Pauses playback of the media.
488
489 Sets the \l playbackState property to PausedState,
490 and changes \l playing to \c false.
491*/
492
493/*!
494 Pause playing the current source.
495
496 \sa play(), stop()
497*/
498void QMediaPlayer::pause()
499{
500 Q_D(QMediaPlayer);
501
502 if (d->control)
503 d->control->pause();
504}
505
506/*!
507 \qmlmethod void QtMultimedia::MediaPlayer::stop()
508
509 Stops playback of the media.
510
511 Sets the \l playbackState property to StoppedState,
512 and changes \l playing to \c false.
513*/
514
515/*!
516 Stop playing, and reset the play position to the beginning.
517
518 \sa play(), pause()
519*/
520void QMediaPlayer::stop()
521{
522 Q_D(QMediaPlayer);
523
524 if (d->control)
525 d->control->stop();
526}
527
528void QMediaPlayer::setPosition(qint64 position)
529{
530 Q_D(QMediaPlayer);
531
532 if (!d->control)
533 return;
534 if (!d->control->isSeekable())
535 return;
536 d->control->setPosition(qMax(position, 0ll));
537}
538
539void QMediaPlayer::setPlaybackRate(qreal rate)
540{
541 Q_D(QMediaPlayer);
542
543 if (d->control)
544 d->control->setPlaybackRate(rate);
545}
546
547/*!
548 \qmlproperty url QtMultimedia::MediaPlayer::source
549
550 This property holds the source URL of the media.
551
552 \snippet multimedia-snippets/qtvideosink.qml complete
553
554 \sa QMediaPlayer::setSource()
555*/
556
557/*!
558 Sets the current \a source.
559
560 Setting the media to a null QUrl will cause the player to discard all
561 information relating to the current media source and to cease all I/O operations related
562 to that media. Setting the media will stop the playback.
563
564 \note This function returns immediately after recording the specified source of the media.
565 It does not wait for the media to finish loading and does not check for errors. Listen for
566 the mediaStatusChanged() and error() signals to be notified when the media is loaded and
567 when an error occurs during loading.
568
569 \note FFmpeg, used by the FFmpeg media backend, restricts use of nested protocols for
570 security reasons. In controlled environments where all inputs are trusted, the list of
571 approved protocols can be overridden using the QT_FFMPEG_PROTOCOL_WHITELIST environment
572 variable. This environment variable is Qt's private API and can change between patch
573 releases without notice.
574*/
575
576void QMediaPlayer::setSource(const QUrl &source)
577{
578 Q_D(QMediaPlayer);
579 stop();
580
581 if (d->source == source && d->stream == nullptr)
582 return;
583
584 d->source = source;
585 d->stream = nullptr;
586
587 d->setMedia(source, nullptr);
588 emit sourceChanged(d->source);
589}
590
591/*!
592 Sets the current source \a device.
593
594 The media data will be read from \a device. The \a sourceUrl can be provided
595 to resolve additional information about the media, mime type etc. The
596 \a device must be open and readable.
597
598 For macOS the \a device should also be seek-able.
599
600 \note This function returns immediately after recording the specified source
601 of the media. It does not wait for the media to finish loading and does not
602 check for errors. Listen for the mediaStatusChanged() and error() signals to
603 be notified when the media is loaded, and if an error occurs during loading.
604*/
605void QMediaPlayer::setSourceDevice(QIODevice *device, const QUrl &sourceUrl)
606{
607 Q_D(QMediaPlayer);
608 stop();
609
610 if (d->source == sourceUrl && d->stream == device)
611 return;
612
613 d->source = sourceUrl;
614 d->stream = device;
615
616 d->setMedia(d->source, device);
617 emit sourceChanged(d->source);
618}
619
620/*!
621 \qmlproperty QAudioBufferOutput QtMultimedia::MediaPlayer::audioBufferOutput
622 \since 6.8
623
624 This property holds the target audio buffer output.
625
626 Normal usage of MediaPlayer from QML should not require using this property.
627
628 \sa QMediaPlayer::audioBufferOutput()
629*/
630
631/*!
632 \property QMediaPlayer::audioBufferOutput
633 \since 6.8
634 \brief The output audio buffer used by the media player.
635
636 Sets an audio buffer \a output to the media player.
637
638 If \l QAudioBufferOutput is specified and the media source
639 contains an audio stream, the media player, it will emit
640 the signal \l{QAudioBufferOutput::audioBufferReceived} with
641 audio buffers containing decoded audio data. At the end of
642 the audio stream, \c QMediaPlayer emits an empty \l QAudioBuffer.
643
644 \c QMediaPlayer emits outputs audio buffers at the same time as it
645 pushes the matching data to the audio output if it's specified.
646 However, the sound can be played with a small delay due to
647 audio bufferization.
648
649 The format of emitted audio buffers is taken from the
650 specified \a output or from the matching audio stream
651 if the \a output returns an invalid format. Emitted
652 audio data is not scaled depending on the current playback rate.
653
654 Potential use cases of utilizing \c QAudioBufferOutput
655 with \c QMediaPlayer might be:
656 \list
657 \li Audio visualization. If the playback rate of the media player
658 is not \c 1, you may scale the output image dimensions,
659 or image update interval according to the requirements
660 of the visualizer.
661 \li Any AI sound processing, e.g. voice recognition.
662 \li Sending the data to external audio output.
663 Playback rate changing, synchronization with video, and manual
664 flushing on stoping and seeking should be considered.
665 We don't recommend using the audio buffer output
666 for this purpose unless you have a strong reason for this.
667 \endlist
668
669*/
670void QMediaPlayer::setAudioBufferOutput(QAudioBufferOutput *output)
671{
672 Q_D(QMediaPlayer);
673
674 QAudioBufferOutput *oldOutput = d->audioBufferOutput;
675 if (oldOutput == output)
676 return;
677
678 d->audioBufferOutput = output;
679
680 if (oldOutput) {
681 auto oldPlayer = QAudioBufferOutputPrivate::exchangeMediaPlayer(*oldOutput, this);
682 if (oldPlayer)
683 oldPlayer->setAudioBufferOutput(nullptr);
684 }
685
686 if (d->control)
687 d->control->setAudioBufferOutput(output);
688
689 emit audioBufferOutputChanged();
690}
691
692QAudioBufferOutput *QMediaPlayer::audioBufferOutput() const
693{
694 Q_D(const QMediaPlayer);
695 return d->audioBufferOutput;
696}
697
698/*!
699 \qmlproperty AudioOutput QtMultimedia::MediaPlayer::audioOutput
700
701 This property holds the target audio output.
702 Accepts one AudioOutput elements.
703
704 \sa QMediaPlayer::setAudioOutput()
705*/
706
707
708/*!
709 \property QMediaPlayer::audioOutput
710 \brief The audio output device used by the media player.
711
712 The current audio output to be used when playing back media. Setting
713 a new audio output will replace the currently used output.
714
715 Setting this property to \c nullptr will disable any audio output.
716*/
717void QMediaPlayer::setAudioOutput(QAudioOutput *output)
718{
719 Q_D(QMediaPlayer);
720 auto oldOutput = d->audioOutput;
721 if (oldOutput == output)
722 return;
723 d->audioOutput = output;
724 if (d->control)
725 d->control->setAudioOutput(nullptr);
726 if (oldOutput)
727 oldOutput->setDisconnectFunction({});
728 if (output) {
729 output->setDisconnectFunction([this](){ setAudioOutput(nullptr); });
730 if (d->control)
731 d->control->setAudioOutput(output->handle());
732 }
733 emit audioOutputChanged();
734}
735
736QAudioOutput *QMediaPlayer::audioOutput() const
737{
738 Q_D(const QMediaPlayer);
739 return d->audioOutput;
740}
741
742/*!
743 \qmlsignal QtMultimedia::MediaPlayer::tracksChanged()
744
745 This signal is emitted when the \l{audioTracks}, \l{subtitleTracks}
746 or \l{videoTracks} properties are changed.
747*/
748
749/*!
750 \fn QMediaPlayer::tracksChanged()
751*/
752
753/*!
754 \qmlproperty list<mediaMetaData> QtMultimedia::MediaPlayer::audioTracks
755
756 This property holds a list of metadata.
757 Each index refers to an audio track.
758
759 The metadata holds properties describing the individual tracks. For
760 audio tracks the \l{QMediaMetaData}{Language} is usually the most
761 important property.
762
763 This property emits the \l{tracksChanged} signal when modified.
764
765 \sa mediaMetaData
766*/
767
768/*!
769 \property QMediaPlayer::audioTracks
770
771 Lists the set of available audio tracks inside the media.
772
773 The QMediaMetaData returned describes the properties of individual
774 tracks.
775
776 Different audio tracks can for example contain audio in different languages.
777*/
778QList<QMediaMetaData> QMediaPlayer::audioTracks() const
779{
780 Q_D(const QMediaPlayer);
781 return d->trackMetaData(TrackType::AudioStream);
782}
783
784/*!
785 \qmlproperty list<mediaMetaData> QtMultimedia::MediaPlayer::videoTracks
786
787 This property holds a list of metadata.
788 Each index refers to a video track.
789
790 The metadata holds properties describing the individual tracks.
791
792 This property emits the \l{tracksChanged} signal when modified.
793
794 \sa mediaMetaData
795*/
796
797/*!
798 \property QMediaPlayer::videoTracks
799
800 Lists the set of available video tracks inside the media.
801
802 The QMediaMetaData returned describes the properties of individual
803 tracks.
804*/
805QList<QMediaMetaData> QMediaPlayer::videoTracks() const
806{
807 Q_D(const QMediaPlayer);
808 return d->trackMetaData(TrackType::VideoStream);
809}
810
811/*!
812 \qmlproperty list<mediaMetaData> QtMultimedia::MediaPlayer::subtitleTracks
813
814 This property holds a list of metadata.
815 Each index refers to a subtitle track.
816
817 The metadata holds properties describing the individual tracks. For
818 subtitle tracks the \l{QMediaMetaData}{Language} is usually the most
819 important property.
820
821 This property emits the \l{tracksChanged} signal when modified.
822
823 \sa mediaMetaData
824*/
825
826/*!
827 \property QMediaPlayer::subtitleTracks
828
829 Lists the set of available subtitle tracks inside the media.
830
831 The QMediaMetaData returned describes the properties of individual
832 tracks.
833*/
834QList<QMediaMetaData> QMediaPlayer::subtitleTracks() const
835{
836 Q_D(const QMediaPlayer);
837 return d->trackMetaData(TrackType::SubtitleStream);
838}
839
840/*!
841 \qmlproperty int QtMultimedia::MediaPlayer::activeAudioTrack
842
843 This property holds the track number of the currently active audio track.
844 Set to \c{-1} to disable audio track.
845
846 The default property value is \c{0}: the first audio track.
847*/
848
849/*!
850 \property QMediaPlayer::activeAudioTrack
851 \brief Returns the currently active audio track.
852
853 By default, the first available audio track will be chosen.
854
855 Set \a index to \c -1 to disable all audio tracks.
856*/
857int QMediaPlayer::activeAudioTrack() const
858{
859 Q_D(const QMediaPlayer);
860 return d->control ? d->control->activeTrack(TrackType::AudioStream) : 0;
861}
862
863/*!
864 \since 6.2
865 \qmlproperty int QtMultimedia::MediaPlayer::activeVideoTrack
866
867 This property holds the track number of the currently active video audio track.
868 Set to \c{-1} to disable video track.
869
870 The default property value is \c{0}: the first video track.
871*/
872
873/*!
874 \property QMediaPlayer::activeVideoTrack
875 \brief Returns the currently active video track.
876
877 By default, the first available audio track will be chosen.
878
879 Set \a index to \c -1 to disable all video tracks.
880*/
881int QMediaPlayer::activeVideoTrack() const
882{
883 Q_D(const QMediaPlayer);
884 return d->control ? d->control->activeTrack(TrackType::VideoStream) : -1;
885}
886
887/*!
888 \since 6.2
889 \qmlproperty int QtMultimedia::MediaPlayer::activeSubtitleTrack
890
891 This property holds the track number of the currently active subtitle track.
892 Set to \c{-1} to disable subtitle track.
893
894 The default property value is \c{-1}: no subtitles active.
895*/
896
897/*!
898 \property QMediaPlayer::activeSubtitleTrack
899 \brief Returns the currently active subtitle track.
900
901 Set \a index to \c -1 to disable subtitles.
902
903 Subtitles are disabled by default.
904*/
905int QMediaPlayer::activeSubtitleTrack() const
906{
907 Q_D(const QMediaPlayer);
908 return d->control ? d->control->activeTrack(TrackType::SubtitleStream) : -1;
909}
910
911void QMediaPlayer::setActiveAudioTrack(int index)
912{
913 Q_D(QMediaPlayer);
914 if (!d->control)
915 return;
916
917 if (activeAudioTrack() == index)
918 return;
919 d->control->setActiveTrack(TrackType::AudioStream, index);
920}
921
922void QMediaPlayer::setActiveVideoTrack(int index)
923{
924 Q_D(QMediaPlayer);
925 if (!d->control)
926 return;
927
928 if (activeVideoTrack() == index)
929 return;
930 d->control->setActiveTrack(TrackType::VideoStream, index);
931}
932
933void QMediaPlayer::setActiveSubtitleTrack(int index)
934{
935 Q_D(QMediaPlayer);
936 if (!d->control)
937 return;
938
939 if (activeSubtitleTrack() == index)
940 return;
941 d->control->setActiveTrack(TrackType::SubtitleStream, index);
942}
943
944/*!
945 \qmlproperty VideoOutput QtMultimedia::MediaPlayer::videoOutput
946
947 This property holds the target video output.
948 Accepts one VideoOutput elements.
949
950 \sa QMediaPlayer::setVideoOutput()
951*/
952
953/*!
954 \property QMediaPlayer::videoOutput
955 \brief The video output to be used by the media player.
956
957 A media player can only have one video output attached, so
958 setting this property will replace the previously connected
959 video output.
960
961 Setting this property to \c nullptr will disable video output.
962*/
963QObject *QMediaPlayer::videoOutput() const
964{
965 Q_D(const QMediaPlayer);
966 return d->videoOutput;
967}
968
969void QMediaPlayer::setVideoOutput(QObject *output)
970{
971 Q_D(QMediaPlayer);
972 if (d->videoOutput == output)
973 return;
974
975 auto *sink = qobject_cast<QVideoSink *>(output);
976 if (!sink && output) {
977 auto *mo = output->metaObject();
978 mo->invokeMethod(output, "videoSink", Q_RETURN_ARG(QVideoSink *, sink));
979 }
980 d->videoOutput = output;
981 d->setVideoSink(sink);
982}
983
984/*!
985 Sets \a sink to be the QVideoSink instance to
986 retrieve video data.
987*/
988void QMediaPlayer::setVideoSink(QVideoSink *sink)
989{
990 Q_D(QMediaPlayer);
991 d->videoOutput = nullptr;
992 d->setVideoSink(sink);
993}
994
995/*!
996 Returns the QVideoSink instance.
997*/
998QVideoSink *QMediaPlayer::videoSink() const
999{
1000 Q_D(const QMediaPlayer);
1001 return d->videoSink;
1002}
1003
1004
1005#if 0
1006/*
1007 \since 5.15
1008 Sets multiple video sinks as the video output of a media player.
1009 This allows the media player to render video frames on several outputs.
1010
1011 If a video output has already been set on the media player the new surfaces
1012 will replace it.
1013*/
1014void QMediaPlayer::setVideoOutput(const QList<QVideoSink *> &sinks)
1015{
1016 // ### IMPLEMENT ME
1017 Q_UNUSED(sinks);
1018// setVideoOutput(!surfaces.empty() ? new QVideoSurfaces(surfaces, this) : nullptr);
1019}
1020#endif
1021
1022/*!
1023 Returns true if the media player is supported on this platform.
1024*/
1025bool QMediaPlayer::isAvailable() const
1026{
1027 Q_D(const QMediaPlayer);
1028 return bool(d->control);
1029}
1030
1031/*!
1032 \qmlproperty mediaMetaData QtMultimedia::MediaPlayer::metaData
1033
1034 Returns meta data for the current media used by the media player.
1035
1036 Meta data can contain information such as the title of the video or its creation date.
1037
1038 \note The Windows implementation provides metadata only for media located on the local file
1039 system.
1040*/
1041
1042/*!
1043 \property QMediaPlayer::metaData
1044
1045 Returns meta data for the current media used by the media player.
1046
1047 Meta data can contain information such as the title of the video or its creation date.
1048
1049 \note The Windows implementation provides metadata only for media located on the local file
1050 system.
1051*/
1052QMediaMetaData QMediaPlayer::metaData() const
1053{
1054 Q_D(const QMediaPlayer);
1055 return d->control ? d->control->metaData() : QMediaMetaData{};
1056}
1057
1058/*!
1059 \qmlproperty bool QtMultimedia::MediaPlayer::pitchCompensation
1060 \since 6.10
1061
1062 This property holds whether pitch compensation is enabled.
1063*/
1064
1065/*!
1066 \property QMediaPlayer::pitchCompensation
1067 \brief The pitch compensation status of the media player.
1068 \since 6.10
1069
1070 Indicates whether pitch compensation is enabled. When enabled, changing the playback rate
1071 will not affect the pitch of the audio signal.
1072
1073 \note The pitch compensation will increase the CPU load of the QMediaPlayer.
1074
1075 By default is \c{true} if pitch compensation, is available, else \c{false}.
1076*/
1077
1078/*!
1079 Returns the state of pitch compensation.
1080 \since 6.10
1081*/
1082bool QMediaPlayer::pitchCompensation() const
1083{
1084 Q_D(const QMediaPlayer);
1085 return d->control ? d->control->pitchCompensation() : false;
1086}
1087
1088/*!
1089 Sets the state (\a enabled or disabled) of pitch compensation. This only
1090 has an effect if the audio pitch compensation can be configured on the
1091 backend at runtime.
1092 \since 6.10
1093*/
1094void QMediaPlayer::setPitchCompensation(bool enabled) const
1095{
1096 Q_D(const QMediaPlayer);
1097 if (d->control)
1098 d->control->setPitchCompensation(enabled);
1099}
1100
1101/*!
1102 \enum QMediaPlayer::PitchCompensationAvailability
1103 \since 6.10
1104
1105 Availablility of pitch compensation.
1106
1107 Different backends have different behavior regarding pitch compensation when changing
1108 playback rate.
1109
1110 \value AlwaysOn The media player is always performing pitch compensation.
1111 \value Available The media player can be configured to use pitch compensation.
1112 If pitch compensation is available on the current platform, it will be enabled by default,
1113 but users can disable if needed.
1114 \value Unavailable The media player is not able to perform pitch compensation
1115 on the current platform.
1116*/
1117
1118/*!
1119 \qmlproperty enumeration QtMultimedia::MediaPlayer::pitchCompensationAvailability
1120 \since 6.10
1121
1122 Indicates the availability of pitch compensation of the \c MediaPlayer on the current backend.
1123 The enumeration \c PitchCompensationAvailability is scoped.
1124
1125 \qmlenumeratorsfrom QMediaPlayer::PitchCompensationAvailability
1126*/
1127
1128/*!
1129 \property QMediaPlayer::pitchCompensationAvailability
1130 \brief The pitch compensation availability of the current QtMultimedia backend.
1131 \since 6.10
1132
1133 Indicates the availability of pitch compensation of the QMediaPlayer on the current backend.
1134
1135 \note Different backends may have different behavior.
1136
1137 For more information, see \l{QMediaPlayer::PitchCompensationAvailability}.
1138*/
1139
1140/*!
1141 Returns availability of pitch compensation of the current backend.
1142 \since 6.10
1143*/
1144
1145QMediaPlayer::PitchCompensationAvailability QMediaPlayer::pitchCompensationAvailability() const
1146{
1147 Q_D(const QMediaPlayer);
1148 return d->control ? d->control->pitchCompensationAvailability()
1149 : PitchCompensationAvailability::Unavailable;
1150}
1151
1152/*!
1153 \qmlproperty PlaybackOptions MediaPlayer::playbackOptions
1154 \since 6.10
1155
1156 This property exposes the \l PlaybackOptions API that gives low-level control of media playback
1157 options. Although we strongly recommend to rely on the default settings of \l MediaPlayer,
1158 this API can be used to optimize media playback for specific use cases where the default
1159 options are not ideal.
1160
1161 Playback options take effect the next time \l MediaPlayer::source is changed.
1162*/
1163
1164/*!
1165 \property QMediaPlayer::playbackOptions
1166 \brief Advanced playback options used to configure media playback and decoding.
1167 \since 6.10
1168
1169 This property exposes the \l QPlaybackOptions API that gives low-level control of media
1170 playback options. Although we strongly recommend to rely on the default settings of
1171 \l QMediaPlayer, this API can be used to optimize media playback for specific use cases where
1172 the default options are not ideal.
1173
1174 Playback options take effect the next time \l QMediaPlayer::setSource() is called.
1175*/
1176
1177QPlaybackOptions QMediaPlayer::playbackOptions() const
1178{
1179 Q_D(const QMediaPlayer);
1180 return d->playbackOptions;
1181}
1182
1183void QMediaPlayer::setPlaybackOptions(const QPlaybackOptions &options)
1184{
1185 Q_D(QMediaPlayer);
1186 if (std::exchange(d->playbackOptions, options) != options)
1187 emit playbackOptionsChanged();
1188}
1189
1190void QMediaPlayer::resetPlaybackOptions()
1191{
1192 Q_D(QMediaPlayer);
1193 QPlaybackOptions defaultOptions{ };
1194 if (std::exchange(d->playbackOptions, defaultOptions) != defaultOptions)
1195 emit playbackOptionsChanged();
1196}
1197
1198// Enums
1199/*!
1200 \enum QMediaPlayer::PlaybackState
1201
1202 Defines the current state of a media player.
1203
1204 \value StoppedState The media player is not playing content, playback will begin from the start
1205 of the current track.
1206 \value PlayingState The media player is currently playing content. This indicates the same as the \l playing property.
1207 \value PausedState The media player has paused playback, playback of the current track will
1208 resume from the position the player was paused at.
1209*/
1210
1211/*!
1212 \qmlproperty enumeration QtMultimedia::MediaPlayer::playbackState
1213
1214 This property holds the state of media playback. It can be one of the following:
1215
1216 \table
1217 \header \li Property value
1218 \li Description
1219 \row \li PlayingState
1220 \li The media is currently playing. This indicates the same as the \l playing property.
1221 \row \li PausedState
1222 \li Playback of the media has been suspended.
1223 \row \li StoppedState
1224 \li Playback of the media is yet to begin.
1225 \endtable
1226*/
1227
1228/*!
1229 \qmlsignal QtMultimedia::MediaPlayer::playbackStateChanged()
1230
1231 This signal is emitted when the \l playbackState property is altered.
1232*/
1233
1234/*!
1235 \qmlsignal QtMultimedia::MediaPlayer::playingChanged()
1236
1237 This signal is emitted when the \l playing property changes.
1238*/
1239
1240/*!
1241 \enum QMediaPlayer::MediaStatus
1242
1243 Defines the status of a media player's current media.
1244
1245 \value NoMedia The is no current media. The player is in the StoppedState.
1246 \value LoadingMedia The current media is being loaded. The player may be in any state.
1247 \value LoadedMedia The current media has been loaded. The player is in the StoppedState.
1248 \value StalledMedia Playback of the current media has stalled due to insufficient buffering or
1249 some other temporary interruption. The player is in the PlayingState or PausedState.
1250 \value BufferingMedia The player is buffering data but has enough data buffered for playback to
1251 continue for the immediate future. The player is in the PlayingState or PausedState.
1252 \value BufferedMedia The player has fully buffered the current media. The player is in the
1253 PlayingState or PausedState.
1254 \value EndOfMedia Playback has reached the end of the current media. The player is in the
1255 StoppedState.
1256 \value InvalidMedia The current media cannot be played. The player is in the StoppedState.
1257*/
1258
1259/*!
1260 \qmlproperty enumeration QtMultimedia::MediaPlayer::mediaStatus
1261
1262 This property holds the status of media loading. It can be one of the following:
1263
1264 \qmlenumeratorsfrom QMediaPlayer::MediaStatus
1265*/
1266
1267/*!
1268 \qmlproperty enumeration QtMultimedia::MediaPlayer::error
1269
1270 This property holds the error state of the audio. It can be one of the following.
1271
1272 \qmlenumeratorsfrom QMediaPlayer::Error
1273*/
1274
1275/*!
1276 \enum QMediaPlayer::Error
1277
1278 Defines a media player error condition.
1279
1280 \value NoError No error has occurred.
1281 \value ResourceError A media resource couldn't be resolved.
1282 \value FormatError The format of a media resource isn't (fully) supported. Playback may still
1283 be possible, but without an audio or video component.
1284 \value NetworkError A network error occurred.
1285 \value AccessDeniedError There are not the appropriate permissions to play a media resource.
1286*/
1287
1288/*!
1289 \qmlsignal QtMultimedia::MediaPlayer::errorOccurred(error, errorString)
1290
1291 This signal is emitted when an \a error has occurred. The \a errorString
1292 parameter may contain more detailed information about the error.
1293
1294 \sa QMediaPlayer::Error
1295*/
1296
1297/*!
1298 \fn QMediaPlayer::errorOccurred(QMediaPlayer::Error error, const QString &errorString)
1299
1300 Signals that an \a error condition has occurred, with \a errorString
1301 containing a description of the error.
1302
1303 \sa errorString()
1304*/
1305
1306/*!
1307 \fn QMediaPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)
1308
1309 Signals that the \a status of the current media has changed.
1310
1311 \sa mediaStatus()
1312*/
1313
1314/*!
1315 \fn void QMediaPlayer::sourceChanged(const QUrl &media);
1316
1317 Signals that the media source has been changed to \a media.
1318*/
1319
1320/*!
1321 \fn void QMediaPlayer::playbackRateChanged(qreal rate);
1322
1323 Signals the playbackRate has changed to \a rate.
1324*/
1325
1326/*!
1327 \fn void QMediaPlayer::seekableChanged(bool seekable);
1328
1329 Signals the \a seekable status of the player object has changed.
1330*/
1331
1332// Properties
1333/*!
1334 \property QMediaPlayer::error
1335 \brief a string describing the last error condition.
1336
1337 \sa error()
1338*/
1339
1340/*!
1341 \property QMediaPlayer::source
1342 \brief the active media source being used by the player object.
1343
1344 The player object will use the QUrl for selection of the content to
1345 be played.
1346
1347 By default this property has a null QUrl.
1348
1349 Setting this property to a null QUrl will cause the player to discard all
1350 information relating to the current media source and to cease all I/O operations related
1351 to that media.
1352
1353 \sa QUrl
1354*/
1355
1356/*!
1357 \property QMediaPlayer::mediaStatus
1358 \brief the status of the current media stream.
1359
1360 The stream status describes how the playback of the current stream is
1361 progressing.
1362
1363 By default this property is QMediaPlayer::NoMedia
1364
1365*/
1366
1367/*!
1368 \qmlproperty int QtMultimedia::MediaPlayer::duration
1369
1370 This property holds the duration of the media in milliseconds.
1371
1372 If the media doesn't have a fixed duration (a live stream for example) this
1373 will be set to \c{0}.
1374*/
1375
1376/*!
1377 \property QMediaPlayer::duration
1378 \brief the duration of the current media.
1379
1380 The value is the total playback time in milliseconds of the current media.
1381 The value may change across the life time of the QMediaPlayer object and
1382 may not be available when initial playback begins, connect to the
1383 durationChanged() signal to receive status notifications.
1384*/
1385
1386/*!
1387 \qmlproperty int QtMultimedia::MediaPlayer::position
1388
1389 The value is the current playback position, expressed in milliseconds since
1390 the beginning of the media. Periodically changes in the position will be
1391 indicated with the positionChanged() signal.
1392
1393 If the \l seekable property is true, this property can be set to milliseconds.
1394*/
1395
1396/*!
1397 \property QMediaPlayer::position
1398 \brief the playback position of the current media.
1399
1400 The value is the current playback position, expressed in milliseconds since
1401 the beginning of the media. Periodically changes in the position will be
1402 indicated with the positionChanged() signal.
1403
1404 If the \l seekable property is true, this property can be set to milliseconds.
1405*/
1406
1407/*!
1408 \qmlproperty real QtMultimedia::MediaPlayer::bufferProgress
1409
1410 This property holds how much of the data buffer is currently filled,
1411 from \c 0.0 (empty) to \c 1.0 (full).
1412
1413 Playback can start or resume only when the buffer is entirely filled.
1414 When the buffer is filled, \c MediaPlayer.Buffered is true.
1415 When buffer progress is between \c 0.0 and \c 1.0, \c MediaPlayer.Buffering
1416 is set to \c{true}.
1417
1418 A value lower than \c 1.0 implies that the property \c MediaPlayer.StalledMedia
1419 is \c{true}.
1420
1421 \sa mediaStatus
1422 */
1423
1424/*!
1425 \property QMediaPlayer::bufferProgress
1426 \brief the percentage of the temporary buffer filled before playback begins or resumes, from
1427 \c 0. (empty) to \c 1. (full).
1428
1429 When the player object is buffering; this property holds the percentage of
1430 the temporary buffer that is filled. The buffer will need to reach 100%
1431 filled before playback can start or resume, at which time mediaStatus() will return
1432 BufferedMedia or BufferingMedia. If the value is anything lower than \c 100, mediaStatus() will
1433 return StalledMedia.
1434
1435 \sa mediaStatus()
1436*/
1437
1438/*!
1439 \qmlproperty bool QtMultimedia::MediaPlayer::seekable
1440
1441 This property holds whether the \l position of the media can be changed.
1442*/
1443
1444/*!
1445 \property QMediaPlayer::seekable
1446 \brief the seek-able status of the current media
1447
1448 If seeking is supported this property will be true; false otherwise. The
1449 status of this property may change across the life time of the QMediaPlayer
1450 object, use the seekableChanged signal to monitor changes.
1451*/
1452
1453/*!
1454 \qmlproperty bool QtMultimedia::MediaPlayer::playing
1455 \since 6.5
1456
1457 Indicates whether the media is currently playing.
1458
1459 \sa playbackState
1460*/
1461
1462/*!
1463 \property QMediaPlayer::playing
1464 \brief Whether the media is playing.
1465 \since 6.5
1466
1467 \sa playbackState, PlayingState
1468*/
1469
1470/*!
1471 \qmlproperty real QtMultimedia::MediaPlayer::playbackRate
1472
1473 This property holds the rate at which media is played at as a multiple of
1474 the normal rate.
1475
1476 For more information, see \l{QMediaPlayer::playbackRate}.
1477
1478 Defaults to \c{1.0}.
1479*/
1480
1481/*!
1482 \property QMediaPlayer::playbackRate
1483 \brief the playback rate of the current media.
1484
1485 This value is a multiplier applied to the media's standard playback
1486 rate. By default this value is 1.0, indicating that the media is
1487 playing at the standard speed. Values higher than 1.0 will increase
1488 the playback speed, while values between 0.0 and 1.0 results in
1489 slower playback. Negative playback rates are not supported.
1490
1491 Not all playback services support change of the playback rate. It is
1492 framework defined as to the status and quality of audio and video
1493 while fast forwarding or rewinding.
1494*/
1495
1496/*!
1497 \fn void QMediaPlayer::durationChanged(qint64 duration)
1498
1499 Signals the duration of the content has changed to \a duration, expressed in milliseconds.
1500*/
1501
1502/*!
1503 \fn void QMediaPlayer::positionChanged(qint64 position)
1504
1505 Signals the position of the content has changed to \a position, expressed in
1506 milliseconds.
1507*/
1508
1509/*!
1510 \fn void QMediaPlayer::hasVideoChanged(bool videoAvailable)
1511
1512 Signals the availability of visual content has changed to \a videoAvailable.
1513*/
1514
1515/*!
1516 \fn void QMediaPlayer::hasAudioChanged(bool available)
1517
1518 Signals the availability of audio content has changed to \a available.
1519*/
1520
1521/*!
1522 \fn void QMediaPlayer::bufferProgressChanged(float filled)
1523
1524 Signals the amount of the local buffer \a filled as a number between 0 and 1.
1525*/
1526
1527QT_END_NAMESPACE
1528
1529#include "moc_qmediaplayer.cpp"
QPlatformMediaPlayer * control
void setState(QMediaPlayer::PlaybackState state)
\qmltype MediaPlayer \nativetype QMediaPlayer
QList< QMediaMetaData > trackMetaData(QPlatformMediaPlayer::TrackType s) const
void setMedia(QUrl media, QIODevice *stream=nullptr)
void setStatus(QMediaPlayer::MediaStatus status)
Combined button and popup list for selecting options.