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#include <QtQml/qqmlcontext.h>
6
8
9QQuickMediaPlayer::QQuickMediaPlayer(QObject *parent) : QMediaPlayer(parent)
10{
11 connect(this, &QMediaPlayer::mediaStatusChanged, this,
12 &QQuickMediaPlayer::onMediaStatusChanged);
13}
14
15void QQuickMediaPlayer::qmlSetSource(const QUrl &source)
16{
17 if (m_source == source)
18 return;
19 m_source = source;
20 const QQmlContext *context = qmlContext(this);
21 setSource(context ? context->resolvedUrl(source) : source);
22 m_wasMediaLoaded = false;
23 emit qmlSourceChanged(source);
24}
25
26QUrl QQuickMediaPlayer::qmlSource() const
27{
28 return m_source;
29}
30
31void QQuickMediaPlayer::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
32{
33 if (status != QMediaPlayer::LoadedMedia || std::exchange(m_wasMediaLoaded, true))
34 return;
35
36 // run with QueuedConnection to make the user able to handle the media status change
37 // by themselves, otherwise play() might change the status in the handler.
38 auto tryAutoPlay = [this]() {
39 if (m_autoPlay && mediaStatus() == QMediaPlayer::LoadedMedia)
40 play();
41 };
42
43 if (m_autoPlay)
44 QMetaObject::invokeMethod(this, tryAutoPlay, Qt::QueuedConnection);
45}
46
47/*!
48 \since 6.7
49 \qmlproperty bool QtMultimedia::MediaPlayer::autoPlay
50
51 This property controls whether the media begins to play automatically after it gets loaded.
52 Defaults to \c false.
53*/
54
55bool QQuickMediaPlayer::autoPlay() const
56{
57 return m_autoPlay;
58}
59
60void QQuickMediaPlayer::setAutoPlay(bool autoPlay)
61{
62 if (std::exchange(m_autoPlay, autoPlay) != autoPlay)
63 emit autoPlayChanged(autoPlay);
64}
65
66QT_END_NAMESPACE
67
68#include "moc_qquickmediaplayer_p.cpp"
Combined button and popup list for selecting options.