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