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
4#ifndef QMEDIAPLAYER_H
5#define QMEDIAPLAYER_H
6
7#include <QtCore/qobject.h>
8#include <QtCore/qurl.h>
9#include <QtMultimedia/qtmultimediaglobal.h>
10#include <QtMultimedia/qmediaenumdebug.h>
11#include <QtMultimedia/qaudio.h>
12
13QT_BEGIN_NAMESPACE
14
15class QVideoSink;
16class QAudioOutput;
17class QAudioDevice;
18class QMediaMetaData;
19class QMediaTimeRange;
20
21class QMediaPlayerPrivate;
22class Q_MULTIMEDIA_EXPORT QMediaPlayer : public QObject
23{
24 Q_OBJECT
25 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
26 Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged)
27 Q_PROPERTY(qint64 position READ position WRITE setPosition NOTIFY positionChanged)
28 Q_PROPERTY(float bufferProgress READ bufferProgress NOTIFY bufferProgressChanged)
29 Q_PROPERTY(bool hasAudio READ hasAudio NOTIFY hasAudioChanged)
30 Q_PROPERTY(bool hasVideo READ hasVideo NOTIFY hasVideoChanged)
31 Q_PROPERTY(bool seekable READ isSeekable NOTIFY seekableChanged)
32 Q_PROPERTY(bool playing READ isPlaying NOTIFY playingChanged)
33 Q_PROPERTY(qreal playbackRate READ playbackRate WRITE setPlaybackRate NOTIFY playbackRateChanged)
34 Q_PROPERTY(int loops READ loops WRITE setLoops NOTIFY loopsChanged)
35 Q_PROPERTY(PlaybackState playbackState READ playbackState NOTIFY playbackStateChanged)
36 Q_PROPERTY(MediaStatus mediaStatus READ mediaStatus NOTIFY mediaStatusChanged)
37 Q_PROPERTY(QMediaMetaData metaData READ metaData NOTIFY metaDataChanged)
38 Q_PROPERTY(Error error READ error NOTIFY errorChanged)
39 Q_PROPERTY(QString errorString READ errorString NOTIFY errorChanged)
40 Q_PROPERTY(QObject *videoOutput READ videoOutput WRITE setVideoOutput NOTIFY videoOutputChanged)
41 Q_PROPERTY(QAudioOutput *audioOutput READ audioOutput WRITE setAudioOutput NOTIFY
42 audioOutputChanged)
43
44 Q_PROPERTY(QList<QMediaMetaData> audioTracks READ audioTracks NOTIFY tracksChanged)
45 Q_PROPERTY(QList<QMediaMetaData> videoTracks READ videoTracks NOTIFY tracksChanged)
46 Q_PROPERTY(QList<QMediaMetaData> subtitleTracks READ subtitleTracks NOTIFY tracksChanged)
47
48 Q_PROPERTY(int activeAudioTrack READ activeAudioTrack WRITE setActiveAudioTrack NOTIFY
49 activeTracksChanged)
50 Q_PROPERTY(int activeVideoTrack READ activeVideoTrack WRITE setActiveVideoTrack NOTIFY
51 activeTracksChanged)
52 Q_PROPERTY(int activeSubtitleTrack READ activeSubtitleTrack WRITE setActiveSubtitleTrack NOTIFY
53 activeTracksChanged)
54
55public:
56 enum PlaybackState
57 {
58 StoppedState,
59 PlayingState,
60 PausedState
61 };
62 Q_ENUM(PlaybackState)
63
64 enum MediaStatus
65 {
66 NoMedia,
67 LoadingMedia,
68 LoadedMedia,
69 StalledMedia,
70 BufferingMedia,
71 BufferedMedia,
72 EndOfMedia,
73 InvalidMedia
74 };
75 Q_ENUM(MediaStatus)
76
77 enum Error
78 {
79 NoError,
80 ResourceError,
81 FormatError,
82 NetworkError,
83 AccessDeniedError
84 };
85 Q_ENUM(Error)
86
87 enum Loops
88 {
89 Infinite = -1,
90 Once = 1
91 };
92 Q_ENUM(Loops)
93
94 explicit QMediaPlayer(QObject *parent = nullptr);
95 ~QMediaPlayer();
96
97 QList<QMediaMetaData> audioTracks() const;
98 QList<QMediaMetaData> videoTracks() const;
99 QList<QMediaMetaData> subtitleTracks() const;
100
101 int activeAudioTrack() const;
102 int activeVideoTrack() const;
103 int activeSubtitleTrack() const;
104
105 void setActiveAudioTrack(int index);
106 void setActiveVideoTrack(int index);
107 void setActiveSubtitleTrack(int index);
108
109 void setAudioOutput(QAudioOutput *output);
110 QAudioOutput *audioOutput() const;
111
112 void setVideoOutput(QObject *);
113 QObject *videoOutput() const;
114
115 void setVideoSink(QVideoSink *sink);
116 QVideoSink *videoSink() const;
117
118 QUrl source() const;
119 const QIODevice *sourceDevice() const;
120
121 PlaybackState playbackState() const;
122 MediaStatus mediaStatus() const;
123
124 qint64 duration() const;
125 qint64 position() const;
126
127 bool hasAudio() const;
128 bool hasVideo() const;
129
130 float bufferProgress() const;
131 QMediaTimeRange bufferedTimeRange() const;
132
133 bool isSeekable() const;
134 qreal playbackRate() const;
135
136 bool isPlaying() const;
137
138 int loops() const;
139 void setLoops(int loops);
140
141 Error error() const;
142 QString errorString() const;
143
144 bool isAvailable() const;
145 QMediaMetaData metaData() const;
146
147public Q_SLOTS:
148 void play();
149 void pause();
150 void stop();
151
152 void setPosition(qint64 position);
153
154 void setPlaybackRate(qreal rate);
155
156 void setSource(const QUrl &source);
157 void setSourceDevice(QIODevice *device, const QUrl &sourceUrl = QUrl());
158
159Q_SIGNALS:
160 void sourceChanged(const QUrl &media);
161 void playbackStateChanged(QMediaPlayer::PlaybackState newState);
162 void mediaStatusChanged(QMediaPlayer::MediaStatus status);
163
164 void durationChanged(qint64 duration);
165 void positionChanged(qint64 position);
166
167 void hasAudioChanged(bool available);
168 void hasVideoChanged(bool videoAvailable);
169
170 void bufferProgressChanged(float progress);
171
172 void seekableChanged(bool seekable);
173 void playingChanged(bool playing);
174 void playbackRateChanged(qreal rate);
175 void loopsChanged();
176
177 void metaDataChanged();
178 void videoOutputChanged();
179 void audioOutputChanged();
180
181 void tracksChanged();
182 void activeTracksChanged();
183
184 void errorChanged();
185 void errorOccurred(QMediaPlayer::Error error, const QString &errorString);
186
187private:
188 Q_DISABLE_COPY(QMediaPlayer)
189 Q_DECLARE_PRIVATE(QMediaPlayer)
190 friend class QPlatformMediaPlayer;
191};
192
193QT_END_NAMESPACE
194
195Q_MEDIA_ENUM_DEBUG(QMediaPlayer, PlaybackState)
196Q_MEDIA_ENUM_DEBUG(QMediaPlayer, MediaStatus)
197Q_MEDIA_ENUM_DEBUG(QMediaPlayer, Error)
198
199#endif // QMEDIAPLAYER_H
200

source code of qtmultimedia/src/multimedia/playback/qmediaplayer.h