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 | |
4 | #include "qquickmediaplayer_p.h" |
5 | #include <QtQml/qqmlcontext.h> |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | QQuickMediaPlayer::QQuickMediaPlayer(QObject *parent) : QMediaPlayer(parent) |
10 | { |
11 | connect(sender: this, signal: &QMediaPlayer::positionChanged, context: this, slot: &QQuickMediaPlayer::onPositionChanged); |
12 | connect(sender: this, signal: &QMediaPlayer::durationChanged, context: this, slot: &QQuickMediaPlayer::onDurationChanged); |
13 | connect(sender: this, signal: &QMediaPlayer::mediaStatusChanged, context: this, |
14 | slot: &QQuickMediaPlayer::onMediaStatusChanged); |
15 | } |
16 | |
17 | void QQuickMediaPlayer::qmlSetSource(const QUrl &source) |
18 | { |
19 | if (m_source == source) |
20 | return; |
21 | m_source = source; |
22 | m_wasMediaLoaded = false; |
23 | const QQmlContext *context = qmlContext(this); |
24 | setSource(context ? context->resolvedUrl(source) : source); |
25 | emit qmlSourceChanged(source); |
26 | } |
27 | |
28 | QUrl QQuickMediaPlayer::qmlSource() const |
29 | { |
30 | return m_source; |
31 | } |
32 | |
33 | void QQuickMediaPlayer::setQmlPosition(int position) |
34 | { |
35 | setPosition(static_cast<qint64>(position)); |
36 | } |
37 | |
38 | int QQuickMediaPlayer::qmlPosition() const |
39 | { |
40 | return static_cast<int>(position()); |
41 | } |
42 | |
43 | int QQuickMediaPlayer::qmlDuration() const |
44 | { |
45 | return static_cast<int>(duration()); |
46 | } |
47 | |
48 | void QQuickMediaPlayer::onPositionChanged(qint64 position) |
49 | { |
50 | emit qmlPositionChanged(position: static_cast<int>(position)); |
51 | } |
52 | |
53 | void QQuickMediaPlayer::onDurationChanged(qint64 duration) |
54 | { |
55 | emit qmlDurationChanged(duration: static_cast<int>(duration)); |
56 | } |
57 | |
58 | void QQuickMediaPlayer::onMediaStatusChanged(QMediaPlayer::MediaStatus status) |
59 | { |
60 | if (status != QMediaPlayer::LoadedMedia || std::exchange(obj&: m_wasMediaLoaded, new_val: true)) |
61 | return; |
62 | |
63 | // run with QueuedConnection to make the user able to handle the media status change |
64 | // by themselves, otherwise play() might change the status in the handler. |
65 | auto tryAutoPlay = [this]() { |
66 | if (m_autoPlay && mediaStatus() == QMediaPlayer::LoadedMedia) |
67 | play(); |
68 | }; |
69 | |
70 | if (m_autoPlay) |
71 | QMetaObject::invokeMethod(object: this, function&: tryAutoPlay, type: Qt::QueuedConnection); |
72 | } |
73 | |
74 | /*! |
75 | \since 6.7 |
76 | \qmlproperty bool QtMultimedia::MediaPlayer::autoPlay |
77 | |
78 | This property controls whether the media begins to play automatically after it gets loaded. |
79 | Defaults to \c false. |
80 | */ |
81 | |
82 | bool QQuickMediaPlayer::autoPlay() const |
83 | { |
84 | return m_autoPlay; |
85 | } |
86 | |
87 | void QQuickMediaPlayer::setAutoPlay(bool autoPlay) |
88 | { |
89 | if (std::exchange(obj&: m_autoPlay, new_val&: autoPlay) != autoPlay) |
90 | emit autoPlayChanged(autoPlay); |
91 | } |
92 | |
93 | QT_END_NAMESPACE |
94 | |
95 | #include "moc_qquickmediaplayer_p.cpp" |
96 | |