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
qquickmediaplayer.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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/private/qmediaplayer_p.h>
7#include <QtMultimediaQuick/private/qqmlcontext_source_resolver_p.h>
8
10
11QQuickMediaPlayer::QQuickMediaPlayer(QObject *parent) : QMediaPlayer(parent)
12{
13 connect(this, &QMediaPlayer::mediaStatusChanged, this,
14 &QQuickMediaPlayer::onMediaStatusChanged);
15
16 auto *playerPrivate = QMediaPlayerPrivate::get(this);
17 playerPrivate->m_sourceResolver =
18 std::make_unique<QMultimediaPrivate::QQmlContextSourceResolver>(this);
19}
20
21void QQuickMediaPlayer::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
22{
23 if (status != QMediaPlayer::LoadedMedia || std::exchange(m_wasMediaLoaded, true))
24 return;
25
26 // run with QueuedConnection to make the user able to handle the media status change
27 // by themselves, otherwise play() might change the status in the handler.
28 auto tryAutoPlay = [this]() {
29 if (m_autoPlay && mediaStatus() == QMediaPlayer::LoadedMedia)
30 play();
31 };
32
33 if (m_autoPlay)
34 QMetaObject::invokeMethod(this, tryAutoPlay, Qt::QueuedConnection);
35}
36
37/*!
38 \since 6.7
39 \qmlproperty bool QtMultimedia::MediaPlayer::autoPlay
40
41 This property controls whether the media begins to play automatically after it gets loaded.
42 Defaults to \c false.
43*/
44
45bool QQuickMediaPlayer::autoPlay() const
46{
47 return m_autoPlay;
48}
49
50void QQuickMediaPlayer::setAutoPlay(bool autoPlay)
51{
52 if (std::exchange(m_autoPlay, autoPlay) != autoPlay)
53 emit autoPlayChanged(autoPlay);
54}
55
56QT_END_NAMESPACE
57
58#include "moc_qquickmediaplayer_p.cpp"
Combined button and popup list for selecting options.