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 Q_EMIT q->playingChanged(toState == QMediaPlayer::PlayingState);
101 Q_EMIT q->playbackStateChanged(toState);
102 }
103}
104
105void QMediaPlayerPrivate::setStatus(QMediaPlayer::MediaStatus s)
106{
107 Q_Q(QMediaPlayer);
108
109 Q_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. Changing 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 setSourceDevice(nullptr, source);
579}
580
581/*!
582 Sets the current source \a device.
583
584 The media data will be read from \a device. The \a sourceUrl can be provided
585 to resolve additional information about the media, mime type etc. The
586 \a device must be open and readable.
587
588 For macOS the \a device should also be seek-able.
589
590 Setting the same device and source URL again does nothing:
591 playback continues and sourceChanged() is not emitted.
592
593 \note This function returns immediately after recording the specified source
594 of the media. It does not wait for the media to finish loading and does not
595 check for errors. Listen for the mediaStatusChanged() and error() signals to
596 be notified when the media is loaded, and if an error occurs during loading.
597*/
598void QMediaPlayer::setSourceDevice(QIODevice *device, const QUrl &sourceUrl)
599{
600 Q_D(QMediaPlayer);
601
602 if (d->source == sourceUrl && d->stream == device)
603 return;
604
605 stop();
606 d->source = sourceUrl;
607 d->stream = device;
608
609 d->setMedia(d->source, device);
610 Q_EMIT sourceChanged(d->source);
611}
612
613/*!
614 \qmlproperty QAudioBufferOutput QtMultimedia::MediaPlayer::audioBufferOutput
615 \since 6.8
616
617 This property holds the target audio buffer output.
618
619 Normal usage of MediaPlayer from QML should not require using this property.
620
621 \sa QMediaPlayer::audioBufferOutput()
622*/
623
624/*!
625 \property QMediaPlayer::audioBufferOutput
626 \since 6.8
627 \brief The output audio buffer used by the media player.
628
629 Sets an audio buffer \a output to the media player.
630
631 If \l QAudioBufferOutput is specified and the media source
632 contains an audio stream, the media player, it will emit
633 the signal \l{QAudioBufferOutput::audioBufferReceived} with
634 audio buffers containing decoded audio data. At the end of
635 the audio stream, \c QMediaPlayer emits an empty \l QAudioBuffer.
636
637 \c QMediaPlayer emits outputs audio buffers at the same time as it
638 pushes the matching data to the audio output if it's specified.
639 However, the sound can be played with a small delay due to
640 audio bufferization.
641
642 The format of emitted audio buffers is taken from the
643 specified \a output or from the matching audio stream
644 if the \a output returns an invalid format. Emitted
645 audio data is not scaled depending on the current playback rate.
646
647 Potential use cases of utilizing \c QAudioBufferOutput
648 with \c QMediaPlayer might be:
649 \list
650 \li Audio visualization. If the playback rate of the media player
651 is not \c 1, you may scale the output image dimensions,
652 or image update interval according to the requirements
653 of the visualizer.
654 \li Any AI sound processing, e.g. voice recognition.
655 \li Sending the data to external audio output.
656 Playback rate changing, synchronization with video, and manual
657 flushing on stoping and seeking should be considered.
658 We don't recommend using the audio buffer output
659 for this purpose unless you have a strong reason for this.
660 \endlist
661
662*/
663void QMediaPlayer::setAudioBufferOutput(QAudioBufferOutput *output)
664{
665 Q_D(QMediaPlayer);
666
667 QAudioBufferOutput *oldOutput = d->audioBufferOutput;
668 if (oldOutput == output)
669 return;
670
671 d->audioBufferOutput = output;
672
673 if (oldOutput) {
674 auto oldPlayer = QAudioBufferOutputPrivate::exchangeMediaPlayer(*oldOutput, this);
675 if (oldPlayer)
676 oldPlayer->setAudioBufferOutput(nullptr);
677 }
678
679 if (d->control)
680 d->control->setAudioBufferOutput(output);
681
682 Q_EMIT audioBufferOutputChanged();
683}
684
685QAudioBufferOutput *QMediaPlayer::audioBufferOutput() const
686{
687 Q_D(const QMediaPlayer);
688 return d->audioBufferOutput;
689}
690
691/*!
692 \qmlproperty AudioOutput QtMultimedia::MediaPlayer::audioOutput
693
694 This property holds the target audio output.
695 Accepts one AudioOutput elements.
696
697 \sa QMediaPlayer::setAudioOutput()
698*/
699
700
701/*!
702 \property QMediaPlayer::audioOutput
703 \brief The audio output device used by the media player.
704
705 The current audio output to be used when playing back media. Setting
706 a new audio output will replace the currently used output.
707
708 Setting this property to \c nullptr will disable any audio output.
709*/
710void QMediaPlayer::setAudioOutput(QAudioOutput *output)
711{
712 Q_D(QMediaPlayer);
713 auto oldOutput = d->audioOutput;
714 if (oldOutput == output)
715 return;
716 d->audioOutput = output;
717 if (d->control)
718 d->control->setAudioOutput(nullptr);
719 if (oldOutput)
720 oldOutput->setDisconnectFunction({});
721 if (output) {
722 output->setDisconnectFunction([this](){ setAudioOutput(nullptr); });
723 if (d->control)
724 d->control->setAudioOutput(output->handle());
725 }
726 Q_EMIT audioOutputChanged();
727}
728
729QAudioOutput *QMediaPlayer::audioOutput() const
730{
731 Q_D(const QMediaPlayer);
732 return d->audioOutput;
733}
734
735/*!
736 \qmlsignal QtMultimedia::MediaPlayer::tracksChanged()
737
738 This signal is emitted when the \l{audioTracks}, \l{subtitleTracks}
739 or \l{videoTracks} properties are changed.
740*/
741
742/*!
743 \fn QMediaPlayer::tracksChanged()
744*/
745
746/*!
747 \qmlproperty list<mediaMetaData> QtMultimedia::MediaPlayer::audioTracks
748
749 This property holds a list of metadata.
750 Each index refers to an audio track.
751
752 The metadata holds properties describing the individual tracks. For
753 audio tracks the \l{QMediaMetaData}{Language} is usually the most
754 important property.
755
756 This property emits the \l{tracksChanged} signal when modified.
757
758 \sa mediaMetaData
759*/
760
761/*!
762 \property QMediaPlayer::audioTracks
763
764 Lists the set of available audio tracks inside the media.
765
766 The QMediaMetaData returned describes the properties of individual
767 tracks.
768
769 Different audio tracks can for example contain audio in different languages.
770*/
771QList<QMediaMetaData> QMediaPlayer::audioTracks() const
772{
773 Q_D(const QMediaPlayer);
774 return d->trackMetaData(TrackType::AudioStream);
775}
776
777/*!
778 \qmlproperty list<mediaMetaData> QtMultimedia::MediaPlayer::videoTracks
779
780 This property holds a list of metadata.
781 Each index refers to a video track.
782
783 The metadata holds properties describing the individual tracks.
784
785 This property emits the \l{tracksChanged} signal when modified.
786
787 \sa mediaMetaData
788*/
789
790/*!
791 \property QMediaPlayer::videoTracks
792
793 Lists the set of available video tracks inside the media.
794
795 The QMediaMetaData returned describes the properties of individual
796 tracks.
797*/
798QList<QMediaMetaData> QMediaPlayer::videoTracks() const
799{
800 Q_D(const QMediaPlayer);
801 return d->trackMetaData(TrackType::VideoStream);
802}
803
804/*!
805 \qmlproperty list<mediaMetaData> QtMultimedia::MediaPlayer::subtitleTracks
806
807 This property holds a list of metadata.
808 Each index refers to a subtitle track.
809
810 The metadata holds properties describing the individual tracks. For
811 subtitle tracks the \l{QMediaMetaData}{Language} is usually the most
812 important property.
813
814 This property emits the \l{tracksChanged} signal when modified.
815
816 \sa mediaMetaData
817*/
818
819/*!
820 \property QMediaPlayer::subtitleTracks
821
822 Lists the set of available subtitle tracks inside the media.
823
824 The QMediaMetaData returned describes the properties of individual
825 tracks.
826*/
827QList<QMediaMetaData> QMediaPlayer::subtitleTracks() const
828{
829 Q_D(const QMediaPlayer);
830 return d->trackMetaData(TrackType::SubtitleStream);
831}
832
833/*!
834 \qmlproperty int QtMultimedia::MediaPlayer::activeAudioTrack
835
836 This property holds the track number of the currently active audio track.
837 Set to \c{-1} to disable audio track.
838
839 The default property value is \c{0}: the first audio track.
840*/
841
842/*!
843 \property QMediaPlayer::activeAudioTrack
844 \brief Returns the currently active audio track.
845
846 By default, the first available audio track will be chosen.
847
848 Set \a index to \c -1 to disable all audio tracks.
849*/
850int QMediaPlayer::activeAudioTrack() const
851{
852 Q_D(const QMediaPlayer);
853 return d->control ? d->control->activeTrack(TrackType::AudioStream) : 0;
854}
855
856/*!
857 \since 6.2
858 \qmlproperty int QtMultimedia::MediaPlayer::activeVideoTrack
859
860 This property holds the track number of the currently active video audio track.
861 Set to \c{-1} to disable video track.
862
863 The default property value is \c{0}: the first video track.
864*/
865
866/*!
867 \property QMediaPlayer::activeVideoTrack
868 \brief Returns the currently active video track.
869
870 By default, the first available audio track will be chosen.
871
872 Set \a index to \c -1 to disable all video tracks.
873*/
874int QMediaPlayer::activeVideoTrack() const
875{
876 Q_D(const QMediaPlayer);
877 return d->control ? d->control->activeTrack(TrackType::VideoStream) : -1;
878}
879
880/*!
881 \since 6.2
882 \qmlproperty int QtMultimedia::MediaPlayer::activeSubtitleTrack
883
884 This property holds the track number of the currently active subtitle track.
885 Set to \c{-1} to disable subtitle track.
886
887 The default property value is \c{-1}: no subtitles active.
888*/
889
890/*!
891 \property QMediaPlayer::activeSubtitleTrack
892 \brief Returns the currently active subtitle track.
893
894 Set \a index to \c -1 to disable subtitles.
895
896 Subtitles are disabled by default.
897*/
898int QMediaPlayer::activeSubtitleTrack() const
899{
900 Q_D(const QMediaPlayer);
901 return d->control ? d->control->activeTrack(TrackType::SubtitleStream) : -1;
902}
903
904void QMediaPlayer::setActiveAudioTrack(int index)
905{
906 Q_D(QMediaPlayer);
907 if (!d->control)
908 return;
909
910 if (activeAudioTrack() == index)
911 return;
912 d->control->setActiveTrack(TrackType::AudioStream, index);
913}
914
915void QMediaPlayer::setActiveVideoTrack(int index)
916{
917 Q_D(QMediaPlayer);
918 if (!d->control)
919 return;
920
921 if (activeVideoTrack() == index)
922 return;
923 d->control->setActiveTrack(TrackType::VideoStream, index);
924}
925
926void QMediaPlayer::setActiveSubtitleTrack(int index)
927{
928 Q_D(QMediaPlayer);
929 if (!d->control)
930 return;
931
932 if (activeSubtitleTrack() == index)
933 return;
934 d->control->setActiveTrack(TrackType::SubtitleStream, index);
935}
936
937/*!
938 \qmlproperty VideoOutput QtMultimedia::MediaPlayer::videoOutput
939
940 This property holds the target video output.
941 Accepts one VideoOutput elements.
942
943 \sa QMediaPlayer::setVideoOutput()
944*/
945
946/*!
947 \property QMediaPlayer::videoOutput
948 \brief The video output to be used by the media player.
949
950 A media player can only have one video output attached, so
951 setting this property will replace the previously connected
952 video output.
953
954 Setting this property to \c nullptr will disable video output.
955*/
956QObject *QMediaPlayer::videoOutput() const
957{
958 Q_D(const QMediaPlayer);
959 return d->videoOutput;
960}
961
962void QMediaPlayer::setVideoOutput(QObject *output)
963{
964 Q_D(QMediaPlayer);
965 if (d->videoOutput == output)
966 return;
967
968 auto *sink = qobject_cast<QVideoSink *>(output);
969 if (!sink && output) {
970 auto *mo = output->metaObject();
971 mo->invokeMethod(output, "videoSink", Q_RETURN_ARG(QVideoSink *, sink));
972 }
973 d->videoOutput = output;
974 d->setVideoSink(sink);
975}
976
977/*!
978 Sets \a sink to be the QVideoSink instance to
979 retrieve video data.
980*/
981void QMediaPlayer::setVideoSink(QVideoSink *sink)
982{
983 Q_D(QMediaPlayer);
984 d->videoOutput = nullptr;
985 d->setVideoSink(sink);
986}
987
988/*!
989 Returns the QVideoSink instance.
990*/
991QVideoSink *QMediaPlayer::videoSink() const
992{
993 Q_D(const QMediaPlayer);
994 return d->videoSink;
995}
996
997
998#if 0
999/*
1000 \since 5.15
1001 Sets multiple video sinks as the video output of a media player.
1002 This allows the media player to render video frames on several outputs.
1003
1004 If a video output has already been set on the media player the new surfaces
1005 will replace it.
1006*/
1007void QMediaPlayer::setVideoOutput(const QList<QVideoSink *> &sinks)
1008{
1009 // ### IMPLEMENT ME
1010 Q_UNUSED(sinks);
1011// setVideoOutput(!surfaces.empty() ? new QVideoSurfaces(surfaces, this) : nullptr);
1012}
1013#endif
1014
1015/*!
1016 Returns true if the media player is supported on this platform.
1017*/
1018bool QMediaPlayer::isAvailable() const
1019{
1020 Q_D(const QMediaPlayer);
1021 return bool(d->control);
1022}
1023
1024/*!
1025 \qmlproperty mediaMetaData QtMultimedia::MediaPlayer::metaData
1026
1027 Returns meta data for the current media used by the media player.
1028
1029 Meta data can contain information such as the title of the video or its creation date.
1030
1031 \note The Windows implementation provides metadata only for media located on the local file
1032 system.
1033*/
1034
1035/*!
1036 \property QMediaPlayer::metaData
1037
1038 Returns meta data for the current media used by the media player.
1039
1040 Meta data can contain information such as the title of the video or its creation date.
1041
1042 \note The Windows implementation provides metadata only for media located on the local file
1043 system.
1044*/
1045QMediaMetaData QMediaPlayer::metaData() const
1046{
1047 Q_D(const QMediaPlayer);
1048 return d->control ? d->control->metaData() : QMediaMetaData{};
1049}
1050
1051/*!
1052 \qmlproperty bool QtMultimedia::MediaPlayer::pitchCompensation
1053 \since 6.10
1054
1055 This property holds whether pitch compensation is enabled.
1056*/
1057
1058/*!
1059 \property QMediaPlayer::pitchCompensation
1060 \brief The pitch compensation status of the media player.
1061 \since 6.10
1062
1063 Indicates whether pitch compensation is enabled. When enabled, changing the playback rate
1064 will not affect the pitch of the audio signal.
1065
1066 \note The pitch compensation will increase the CPU load of the QMediaPlayer.
1067
1068 By default is \c{true} if pitch compensation, is available, else \c{false}.
1069*/
1070
1071/*!
1072 Returns the state of pitch compensation.
1073 \since 6.10
1074*/
1075bool QMediaPlayer::pitchCompensation() const
1076{
1077 Q_D(const QMediaPlayer);
1078 return d->control ? d->control->pitchCompensation() : false;
1079}
1080
1081/*!
1082 Sets the state (\a enabled or disabled) of pitch compensation. This only
1083 has an effect if the audio pitch compensation can be configured on the
1084 backend at runtime.
1085 \since 6.10
1086*/
1087void QMediaPlayer::setPitchCompensation(bool enabled) const
1088{
1089 Q_D(const QMediaPlayer);
1090 if (d->control)
1091 d->control->setPitchCompensation(enabled);
1092}
1093
1094/*!
1095 \enum QMediaPlayer::PitchCompensationAvailability
1096 \since 6.10
1097
1098 Availablility of pitch compensation.
1099
1100 Different backends have different behavior regarding pitch compensation when changing
1101 playback rate.
1102
1103 \value AlwaysOn The media player is always performing pitch compensation.
1104 \value Available The media player can be configured to use pitch compensation.
1105 If pitch compensation is available on the current platform, it will be enabled by default,
1106 but users can disable if needed.
1107 \value Unavailable The media player is not able to perform pitch compensation
1108 on the current platform.
1109*/
1110
1111/*!
1112 \qmlproperty enumeration QtMultimedia::MediaPlayer::pitchCompensationAvailability
1113 \since 6.10
1114
1115 Indicates the availability of pitch compensation of the \c MediaPlayer on the current backend.
1116 The enumeration \c PitchCompensationAvailability is scoped.
1117
1118 \qmlenumeratorsfrom QMediaPlayer::PitchCompensationAvailability
1119*/
1120
1121/*!
1122 \property QMediaPlayer::pitchCompensationAvailability
1123 \brief The pitch compensation availability of the current QtMultimedia backend.
1124 \since 6.10
1125
1126 Indicates the availability of pitch compensation of the QMediaPlayer on the current backend.
1127
1128 \note Different backends may have different behavior.
1129
1130 For more information, see \l{QMediaPlayer::PitchCompensationAvailability}.
1131*/
1132
1133/*!
1134 Returns availability of pitch compensation of the current backend.
1135 \since 6.10
1136*/
1137
1138QMediaPlayer::PitchCompensationAvailability QMediaPlayer::pitchCompensationAvailability() const
1139{
1140 Q_D(const QMediaPlayer);
1141 return d->control ? d->control->pitchCompensationAvailability()
1142 : PitchCompensationAvailability::Unavailable;
1143}
1144
1145/*!
1146 \qmlproperty PlaybackOptions MediaPlayer::playbackOptions
1147 \since 6.10
1148
1149 This property exposes the \l PlaybackOptions API that gives low-level control of media playback
1150 options. Although we strongly recommend to rely on the default settings of \l MediaPlayer,
1151 this API can be used to optimize media playback for specific use cases where the default
1152 options are not ideal.
1153
1154 Playback options take effect the next time \l MediaPlayer::source is changed.
1155*/
1156
1157/*!
1158 \property QMediaPlayer::playbackOptions
1159 \brief Advanced playback options used to configure media playback and decoding.
1160 \since 6.10
1161
1162 This property exposes the \l QPlaybackOptions API that gives low-level control of media
1163 playback options. Although we strongly recommend to rely on the default settings of
1164 \l QMediaPlayer, this API can be used to optimize media playback for specific use cases where
1165 the default options are not ideal.
1166
1167 Playback options take effect the next time \l QMediaPlayer::setSource() is called.
1168*/
1169
1170QPlaybackOptions QMediaPlayer::playbackOptions() const
1171{
1172 Q_D(const QMediaPlayer);
1173 return d->playbackOptions;
1174}
1175
1176void QMediaPlayer::setPlaybackOptions(const QPlaybackOptions &options)
1177{
1178 Q_D(QMediaPlayer);
1179 if (std::exchange(d->playbackOptions, options) != options)
1180 Q_EMIT playbackOptionsChanged();
1181}
1182
1183void QMediaPlayer::resetPlaybackOptions()
1184{
1185 Q_D(QMediaPlayer);
1186 QPlaybackOptions defaultOptions{ };
1187 if (std::exchange(d->playbackOptions, defaultOptions) != defaultOptions)
1188 Q_EMIT playbackOptionsChanged();
1189}
1190
1191// Enums
1192/*!
1193 \enum QMediaPlayer::PlaybackState
1194
1195 Defines the current state of a media player.
1196
1197 \value StoppedState The media player is not playing content, playback will begin from the start
1198 of the current track.
1199 \value PlayingState The media player is currently playing content. This indicates the same as the \l playing property.
1200 \value PausedState The media player has paused playback, playback of the current track will
1201 resume from the position the player was paused at.
1202*/
1203
1204/*!
1205 \qmlproperty enumeration QtMultimedia::MediaPlayer::playbackState
1206
1207 This property holds the state of media playback. It can be one of the following:
1208
1209 \table
1210 \header \li Property value
1211 \li Description
1212 \row \li PlayingState
1213 \li The media is currently playing. This indicates the same as the \l playing property.
1214 \row \li PausedState
1215 \li Playback of the media has been suspended.
1216 \row \li StoppedState
1217 \li Playback of the media is yet to begin.
1218 \endtable
1219*/
1220
1221/*!
1222 \qmlsignal QtMultimedia::MediaPlayer::playbackStateChanged()
1223
1224 This signal is emitted when the \l playbackState property is altered.
1225*/
1226
1227/*!
1228 \qmlsignal QtMultimedia::MediaPlayer::playingChanged()
1229
1230 This signal is emitted when the \l playing property changes.
1231*/
1232
1233/*!
1234 \enum QMediaPlayer::MediaStatus
1235
1236 Defines the status of a media player's current media.
1237
1238 \value NoMedia The is no current media. The player is in the StoppedState.
1239 \value LoadingMedia The current media is being loaded. The player may be in any state.
1240 \value LoadedMedia The current media has been loaded. The player is in the StoppedState.
1241 \value StalledMedia Playback of the current media has stalled due to insufficient buffering or
1242 some other temporary interruption. The player is in the PlayingState or PausedState.
1243 \value BufferingMedia The player is buffering data but has enough data buffered for playback to
1244 continue for the immediate future. The player is in the PlayingState or PausedState.
1245 \value BufferedMedia The player has fully buffered the current media. The player is in the
1246 PlayingState or PausedState.
1247 \value EndOfMedia Playback has reached the end of the current media. The player is in the
1248 StoppedState.
1249 \value InvalidMedia The current media cannot be played. The player is in the StoppedState.
1250*/
1251
1252/*!
1253 \qmlproperty enumeration QtMultimedia::MediaPlayer::mediaStatus
1254
1255 This property holds the status of media loading. It can be one of the following:
1256
1257 \qmlenumeratorsfrom QMediaPlayer::MediaStatus
1258*/
1259
1260/*!
1261 \qmlproperty enumeration QtMultimedia::MediaPlayer::error
1262
1263 This property holds the error state of the audio. It can be one of the following.
1264
1265 \qmlenumeratorsfrom QMediaPlayer::Error
1266*/
1267
1268/*!
1269 \enum QMediaPlayer::Error
1270
1271 Defines a media player error condition.
1272
1273 \value NoError No error has occurred.
1274 \value ResourceError A media resource couldn't be resolved.
1275 \value FormatError The format of a media resource isn't (fully) supported. Playback may still
1276 be possible, but without an audio or video component.
1277 \value NetworkError A network error occurred.
1278 \value AccessDeniedError There are not the appropriate permissions to play a media resource.
1279*/
1280
1281/*!
1282 \qmlsignal QtMultimedia::MediaPlayer::errorOccurred(error, errorString)
1283
1284 This signal is emitted when an \a error has occurred. The \a errorString
1285 parameter may contain more detailed information about the error.
1286
1287 \sa QMediaPlayer::Error
1288*/
1289
1290/*!
1291 \fn QMediaPlayer::errorOccurred(QMediaPlayer::Error error, const QString &errorString)
1292
1293 Signals that an \a error condition has occurred, with \a errorString
1294 containing a description of the error.
1295
1296 \sa errorString()
1297*/
1298
1299/*!
1300 \fn QMediaPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)
1301
1302 Signals that the \a status of the current media has changed.
1303
1304 \sa mediaStatus()
1305*/
1306
1307/*!
1308 \fn void QMediaPlayer::sourceChanged(const QUrl &media);
1309
1310 Signals that the media source has been changed to \a media.
1311*/
1312
1313/*!
1314 \fn void QMediaPlayer::playbackRateChanged(qreal rate);
1315
1316 Signals the playbackRate has changed to \a rate.
1317*/
1318
1319/*!
1320 \fn void QMediaPlayer::seekableChanged(bool seekable);
1321
1322 Signals the \a seekable status of the player object has changed.
1323*/
1324
1325// Properties
1326/*!
1327 \property QMediaPlayer::error
1328 \brief a string describing the last error condition.
1329
1330 \sa error()
1331*/
1332
1333/*!
1334 \property QMediaPlayer::source
1335 \brief the active media source being used by the player object.
1336
1337 The player object will use the QUrl for selection of the content to
1338 be played.
1339
1340 By default this property has a null QUrl.
1341
1342 Setting this property to a null QUrl will cause the player to discard all
1343 information relating to the current media source and to cease all I/O operations related
1344 to that media.
1345
1346 \sa QUrl
1347*/
1348
1349/*!
1350 \property QMediaPlayer::mediaStatus
1351 \brief the status of the current media stream.
1352
1353 The stream status describes how the playback of the current stream is
1354 progressing.
1355
1356 By default this property is QMediaPlayer::NoMedia
1357
1358*/
1359
1360/*!
1361 \qmlproperty int QtMultimedia::MediaPlayer::duration
1362
1363 This property holds the duration of the media in milliseconds.
1364
1365 If the media doesn't have a fixed duration (a live stream for example) this
1366 will be set to \c{0}.
1367*/
1368
1369/*!
1370 \property QMediaPlayer::duration
1371 \brief the duration of the current media.
1372
1373 The value is the total playback time in milliseconds of the current media.
1374 The value may change across the life time of the QMediaPlayer object and
1375 may not be available when initial playback begins, connect to the
1376 durationChanged() signal to receive status notifications.
1377*/
1378
1379/*!
1380 \qmlproperty int QtMultimedia::MediaPlayer::position
1381
1382 The value is the current playback position, expressed in milliseconds since
1383 the beginning of the media. Periodically changes in the position will be
1384 indicated with the positionChanged() signal.
1385
1386 If the \l seekable property is true, this property can be set to milliseconds.
1387*/
1388
1389/*!
1390 \property QMediaPlayer::position
1391 \brief the playback position of the current media.
1392
1393 The value is the current playback position, expressed in milliseconds since
1394 the beginning of the media. Periodically changes in the position will be
1395 indicated with the positionChanged() signal.
1396
1397 If the \l seekable property is true, this property can be set to milliseconds.
1398*/
1399
1400/*!
1401 \qmlproperty real QtMultimedia::MediaPlayer::bufferProgress
1402
1403 This property holds how much of the data buffer is currently filled,
1404 from \c 0.0 (empty) to \c 1.0 (full).
1405
1406 Playback can start or resume only when the buffer is entirely filled.
1407 When the buffer is filled, \c MediaPlayer.Buffered is true.
1408 When buffer progress is between \c 0.0 and \c 1.0, \c MediaPlayer.Buffering
1409 is set to \c{true}.
1410
1411 A value lower than \c 1.0 implies that the property \c MediaPlayer.StalledMedia
1412 is \c{true}.
1413
1414 \sa mediaStatus
1415 */
1416
1417/*!
1418 \property QMediaPlayer::bufferProgress
1419 \brief the percentage of the temporary buffer filled before playback begins or resumes, from
1420 \c 0. (empty) to \c 1. (full).
1421
1422 When the player object is buffering; this property holds the percentage of
1423 the temporary buffer that is filled. The buffer will need to reach 100%
1424 filled before playback can start or resume, at which time mediaStatus() will return
1425 BufferedMedia or BufferingMedia. If the value is anything lower than \c 100, mediaStatus() will
1426 return StalledMedia.
1427
1428 \sa mediaStatus()
1429*/
1430
1431/*!
1432 \qmlproperty bool QtMultimedia::MediaPlayer::seekable
1433
1434 This property holds whether the \l position of the media can be changed.
1435*/
1436
1437/*!
1438 \property QMediaPlayer::seekable
1439 \brief the seek-able status of the current media
1440
1441 If seeking is supported this property will be true; false otherwise. The
1442 status of this property may change across the life time of the QMediaPlayer
1443 object, use the seekableChanged signal to monitor changes.
1444*/
1445
1446/*!
1447 \qmlproperty bool QtMultimedia::MediaPlayer::playing
1448 \since 6.5
1449
1450 Indicates whether the media is currently playing.
1451
1452 \sa playbackState
1453*/
1454
1455/*!
1456 \property QMediaPlayer::playing
1457 \brief Whether the media is playing.
1458 \since 6.5
1459
1460 \sa playbackState, PlayingState
1461*/
1462
1463/*!
1464 \qmlproperty real QtMultimedia::MediaPlayer::playbackRate
1465
1466 This property holds the rate at which media is played at as a multiple of
1467 the normal rate.
1468
1469 For more information, see \l{QMediaPlayer::playbackRate}.
1470
1471 Defaults to \c{1.0}.
1472*/
1473
1474/*!
1475 \property QMediaPlayer::playbackRate
1476 \brief the playback rate of the current media.
1477
1478 This value is a multiplier applied to the media's standard playback
1479 rate. By default this value is 1.0, indicating that the media is
1480 playing at the standard speed. Values higher than 1.0 will increase
1481 the playback speed, while values between 0.0 and 1.0 results in
1482 slower playback. Negative playback rates are not supported.
1483
1484 Not all playback services support change of the playback rate. It is
1485 framework defined as to the status and quality of audio and video
1486 while fast forwarding or rewinding.
1487*/
1488
1489/*!
1490 \fn void QMediaPlayer::durationChanged(qint64 duration)
1491
1492 Signals the duration of the content has changed to \a duration, expressed in milliseconds.
1493*/
1494
1495/*!
1496 \fn void QMediaPlayer::positionChanged(qint64 position)
1497
1498 Signals the position of the content has changed to \a position, expressed in
1499 milliseconds.
1500*/
1501
1502/*!
1503 \fn void QMediaPlayer::hasVideoChanged(bool videoAvailable)
1504
1505 Signals the availability of visual content has changed to \a videoAvailable.
1506*/
1507
1508/*!
1509 \fn void QMediaPlayer::hasAudioChanged(bool available)
1510
1511 Signals the availability of audio content has changed to \a available.
1512*/
1513
1514/*!
1515 \fn void QMediaPlayer::bufferProgressChanged(float filled)
1516
1517 Signals the amount of the local buffer \a filled as a number between 0 and 1.
1518*/
1519
1520QT_END_NAMESPACE
1521
1522#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.