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
qquickplaylist.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
7
8/*!
9 \qmltype PlaylistItem
10 \nativetype QQuickPlaylistItem
11 \since 5.6
12
13 \inqmlmodule QtMultimedia
14 \ingroup multimedia_qml
15 \ingroup multimedia_audio_qml
16 \ingroup multimedia_video_qml
17 \brief Defines an item in a Playlist.
18 \internal
19
20 \sa Playlist
21*/
22
23/*!
24 \qmlproperty url QtMultimedia::PlaylistItem::source
25
26 This property holds the source URL of the item.
27
28 \sa Playlist
29*/
30QQuickPlaylistItem::QQuickPlaylistItem(QObject *parent)
32{
33}
34
35QUrl QQuickPlaylistItem::source() const
36{
37 return m_source;
38}
39
40void QQuickPlaylistItem::setSource(const QUrl &source)
41{
42 m_source = source;
43}
44
45/*!
46 \qmltype Playlist
47 \nativetype QQuickPlaylist
48 \since 5.6
49 \brief For specifying a list of media to be played.
50 \internal
51
52 \inqmlmodule QtMultimedia
53 \ingroup multimedia_qml
54 \ingroup multimedia_audio_qml
55 \ingroup multimedia_video_qml
56
57 The Playlist type provides a way to play a list of media with the MediaPlayer, Audio and Video
58 types. It can be used as a data source for view elements (such as ListView) and other elements
59 that interact with model data (such as Repeater). When used as a data model, each playlist
60 item's source URL can be accessed using the \c source role.
61
62 \qml
63 Item {
64 width: 400;
65 height: 300;
66
67 Audio {
68 id: player;
69 playlist: Playlist {
70 id: playlist
71 PlaylistItem { source: "song1.ogg"; }
72 PlaylistItem { source: "song2.ogg"; }
73 PlaylistItem { source: "song3.ogg"; }
74 }
75 }
76
77 ListView {
78 model: playlist;
79 delegate: Text {
80 font.pixelSize: 16;
81 text: source;
82 }
83 }
84
85 MouseArea {
86 anchors.fill: parent;
87 onPressed: {
88 if (player.playbackState != Audio.PlayingState) {
89 player.play();
90 } else {
91 player.pause();
92 }
93 }
94 }
95 }
96 \endqml
97
98 \sa MediaPlayer, Audio, Video
99*/
100
101void QQuickPlaylist::_q_mediaAboutToBeInserted(int start, int end)
102{
103 emit itemAboutToBeInserted(start, end);
104
105 beginInsertRows(QModelIndex(), start, end);
106}
107
108void QQuickPlaylist::_q_mediaInserted(int start, int end)
109{
110 endInsertRows();
111
112 emit itemCountChanged();
113 emit itemInserted(start, end);
114}
115
116void QQuickPlaylist::_q_mediaAboutToBeRemoved(int start, int end)
117{
118 emit itemAboutToBeRemoved(start, end);
119
120 beginRemoveRows(QModelIndex(), start, end);
121}
122
123void QQuickPlaylist::_q_mediaRemoved(int start, int end)
124{
125 endRemoveRows();
126
127 emit itemCountChanged();
128 emit itemRemoved(start, end);
129}
130
131void QQuickPlaylist::_q_mediaChanged(int start, int end)
132{
133 emit dataChanged(createIndex(start, 0), createIndex(end, 0));
134 emit itemChanged(start, end);
135}
136
137void QQuickPlaylist::_q_loadFailed()
138{
139 m_error = m_playlist->error();
140 m_errorString = m_playlist->errorString();
141
142 emit error(Error(m_error), m_errorString);
143 emit errorChanged();
144 emit loadFailed();
145}
146
149 , m_playlist(nullptr)
151{
152}
153
155{
156 delete m_playlist;
157}
158
159/*!
160 \qmlproperty enumeration QtMultimedia::Playlist::playbackMode
161
162 This property holds the order in which items in the playlist are played.
163
164 \table
165 \header \li Value \li Description
166 \row \li CurrentItemOnce
167 \li The current item is played only once.
168 \row \li CurrentItemInLoop
169 \li The current item is played repeatedly in a loop.
170 \row \li Sequential
171 \li Playback starts from the current and moves through each successive item until the last
172 is reached and then stops. The next item is a null item when the last one is currently
173 playing.
174 \row \li Loop
175 \li Playback restarts at the first item after the last has finished playing.
176 \row \li Random
177 \li Play items in random order.
178 \endtable
179 */
181{
182 return PlaybackMode(m_playlist->playbackMode());
183}
184
185void QQuickPlaylist::setPlaybackMode(PlaybackMode mode)
186{
187 if (playbackMode() == mode)
188 return;
189
190 m_playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode(mode));
191}
192
193/*!
194 \qmlproperty url QtMultimedia::Playlist::currentItemsource
195
196 This property holds the source URL of the current item in the playlist.
197 */
199{
200 return m_playlist->currentMedia();
201}
202
203/*!
204 \qmlproperty int QtMultimedia::Playlist::currentIndex
205
206 This property holds the position of the current item in the playlist.
207 */
209{
210 return m_playlist->currentIndex();
211}
212
214{
215 if (currentIndex() == index)
216 return;
217
218 m_playlist->setCurrentIndex(index);
219}
220
221/*!
222 \qmlproperty int QtMultimedia::Playlist::itemCount
223
224 This property holds the number of items in the playlist.
225 */
227{
228 return m_playlist->mediaCount();
229}
230
231/*!
232 \qmlproperty enumeration QtMultimedia::Playlist::error
233
234 This property holds the error condition of the playlist.
235
236 \table
237 \header \li Value \li Description
238 \row \li NoError
239 \li No errors
240 \row \li FormatError
241 \li Format error.
242 \row \li FormatNotSupportedError
243 \li Format not supported.
244 \row \li NetworkError
245 \li Network error.
246 \row \li AccessDeniedError
247 \li Access denied error.
248 \endtable
249 */
251{
252 return Error(m_error);
253}
254
255/*!
256 \qmlproperty string QtMultimedia::Playlist::errorString
257
258 This property holds a string describing the current error condition of the playlist.
259*/
261{
262 return m_errorString;
263}
264
265/*!
266 \qmlmethod url QtMultimedia::Playlist::itemSource(index)
267
268 Returns the source URL of the item at the given \a index in the playlist.
269*/
270QUrl QQuickPlaylist::itemSource(int index)
271{
272 return m_playlist->media(index);
273}
274
275/*!
276 \qmlmethod int QtMultimedia::Playlist::nextIndex(steps)
277
278 Returns the index of the item in the playlist which would be current after calling next()
279 \a steps times.
280
281 Returned value depends on the size of the playlist, the current position and the playback mode.
282
283 \sa playbackMode, previousIndex()
284*/
286{
287 return m_playlist->nextIndex(steps);
288}
289
290/*!
291 \qmlmethod int QtMultimedia::Playlist::previousIndex(steps)
292
293 Returns the index of the item in the playlist which would be current after calling previous()
294 \a steps times.
295
296 Returned value depends on the size of the playlist, the current position and the playback mode.
297
298 \sa playbackMode, nextIndex()
299*/
301{
302 return m_playlist->previousIndex(steps);
303}
304
305/*!
306 \qmlmethod QtMultimedia::Playlist::next()
307
308 Advances to the next item in the playlist.
309*/
311{
312 m_playlist->next();
313}
314
315/*!
316 \qmlmethod QtMultimedia::Playlist::previous()
317
318 Returns to the previous item in the playlist.
319*/
321{
322 m_playlist->previous();
323}
324
325/*!
326 \qmlmethod QtMultimedia::Playlist::shuffle()
327
328 Shuffles items in the playlist.
329*/
331{
332 m_playlist->shuffle();
333}
334
335/*!
336 \qmlmethod QtMultimedia::Playlist::load(location, format)
337
338 Loads a playlist from the given \a location. If \a format is specified, it is used, otherwise
339 the format is guessed from the location name and the data.
340
341 New items are appended to the playlist.
342
343 \c onloaded() is emitted if the playlist loads successfully, otherwise \c onLoadFailed() is
344 emitted with \l error and \l errorString defined accordingly.
345*/
346void QQuickPlaylist::load(const QUrl &location, const QString &format)
347{
348 m_error = QMediaPlaylist::NoError;
349 m_errorString = QString();
350 emit errorChanged();
351 m_playlist->load(location, format.toLatin1().constData());
352}
353
354/*!
355 \qmlmethod bool QtMultimedia::Playlist::save(location, format)
356
357 Saves the playlist to the given \a location. If \a format is specified, it is used, otherwise
358 the format is guessed from the location name.
359
360 Returns true if the playlist is saved successfully.
361*/
362bool QQuickPlaylist::save(const QUrl &location, const QString &format)
363{
364 return m_playlist->save(location, format.toLatin1().constData());
365}
366
367/*!
368 \qmlmethod bool QtMultimedia::Playlist::addItem(source)
369
370 Appends the \a source URL to the playlist.
371
372 Returns true if the \a source is added successfully.
373*/
374void QQuickPlaylist::addItem(const QUrl &source)
375{
376 m_playlist->addMedia(QUrl(source));
377}
378
379/*!
380 \qmlmethod bool QtMultimedia::Playlist::addItems(sources)
381
382 Appends the list of URLs in \a sources to the playlist.
383
384 Returns true if the \a sources are added successfully.
385
386 \since 5.7
387*/
388void QQuickPlaylist::addItems(const QList<QUrl> &sources)
389{
390 if (sources.isEmpty())
391 return;
392
393 QList<QUrl> contents;
394 QList<QUrl>::const_iterator it = sources.constBegin();
395 while (it != sources.constEnd()) {
396 contents.push_back(QUrl(*it));
397 ++it;
398 }
399 m_playlist->addMedia(contents);
400}
401
402/*!
403 \qmlmethod bool QtMultimedia::Playlist::insertItem(index, source)
404
405 Inserts the \a source URL to the playlist at the given \a index.
406
407 Returns true if the \a source is added successfully.
408*/
409bool QQuickPlaylist::insertItem(int index, const QUrl &source)
410{
411 return m_playlist->insertMedia(index, QUrl(source));
412}
413
414/*!
415 \qmlmethod bool QtMultimedia::Playlist::insertItems(index, sources)
416
417 Inserts the list of URLs in \a sources to the playlist at the given \a index.
418
419 Returns true if the \a sources are added successfully.
420
421 \since 5.7
422*/
423bool QQuickPlaylist::insertItems(int index, const QList<QUrl> &sources)
424{
425 if (sources.empty())
426 return false;
427
428 QList<QUrl> contents;
429 QList<QUrl>::const_iterator it = sources.constBegin();
430 while (it != sources.constEnd()) {
431 contents.push_back(QUrl(*it));
432 ++it;
433 }
434 return m_playlist->insertMedia(index, contents);
435}
436
437/*!
438 \qmlmethod bool QtMultimedia::Playlist::moveItem(from, to)
439
440 Moves the item at index position \a from to index position \a to.
441
442 Returns \c true if the item is moved successfully.
443
444 \since 5.7
445*/
446bool QQuickPlaylist::moveItem(int from, int to)
447{
448 return m_playlist->moveMedia(from, to);
449}
450
451/*!
452 \qmlmethod bool QtMultimedia::Playlist::removeItem(index)
453
454 Removes the item at the given \a index from the playlist.
455
456 Returns \c true if the item is removed successfully.
457*/
459{
460 return m_playlist->removeMedia(index);
461}
462
463/*!
464 \qmlmethod bool QtMultimedia::Playlist::removeItems(int start, int end)
465
466 Removes items in the playlist from \a start to \a end inclusive.
467
468 Returns \c true if the items are removed successfully.
469
470 \since 5.7
471*/
472bool QQuickPlaylist::removeItems(int start, int end)
473{
474 return m_playlist->removeMedia(start, end);
475}
476
477/*!
478 \qmlmethod bool QtMultimedia::Playlist::clear()
479
480 Removes all the items from the playlist.
481
482 Returns \c true if the operation is successful.
483*/
485{
486 m_playlist->clear();
487}
488
489int QQuickPlaylist::rowCount(const QModelIndex &parent) const
490{
491 if (parent.isValid())
492 return 0;
493
494 return m_playlist->mediaCount();
495}
496
497QVariant QQuickPlaylist::data(const QModelIndex &index, int role) const
498{
499 Q_UNUSED(role);
500
501 if (!index.isValid())
502 return QVariant();
503
504 return m_playlist->media(index.row());
505}
506
508{
509 QHash<int, QByteArray> roleNames;
510 roleNames[SourceRole] = "source";
511 return roleNames;
512}
513
515{
516 m_playlist = new QMediaPlaylist(this);
517
518 connect(m_playlist, SIGNAL(currentIndexChanged(int)),
519 this, SIGNAL(currentIndexChanged()));
520 connect(m_playlist, SIGNAL(playbackModeChanged(QMediaPlaylist::PlaybackMode)),
521 this, SIGNAL(playbackModeChanged()));
522 connect(m_playlist, SIGNAL(currentMediaChanged(QUrl)),
523 this, SIGNAL(currentItemSourceChanged()));
524 connect(m_playlist, SIGNAL(mediaAboutToBeInserted(int,int)),
525 this, SLOT(_q_mediaAboutToBeInserted(int,int)));
526 connect(m_playlist, SIGNAL(mediaInserted(int,int)),
527 this, SLOT(_q_mediaInserted(int,int)));
528 connect(m_playlist, SIGNAL(mediaAboutToBeRemoved(int,int)),
529 this, SLOT(_q_mediaAboutToBeRemoved(int,int)));
530 connect(m_playlist, SIGNAL(mediaRemoved(int,int)),
531 this, SLOT(_q_mediaRemoved(int,int)));
532 connect(m_playlist, SIGNAL(mediaChanged(int,int)),
533 this, SLOT(_q_mediaChanged(int,int)));
534 connect(m_playlist, SIGNAL(loaded()),
535 this, SIGNAL(loaded()));
536 connect(m_playlist, SIGNAL(loadFailed()),
537 this, SLOT(_q_loadFailed()));
538}
539
543
544/*!
545 \qmlsignal QtMultimedia::Playlist::itemAboutToBeInserted(start, end)
546
547 This signal is emitted when items are to be inserted into the playlist at \a start and ending at
548 \a end.
549*/
550
551/*!
552 \qmlsignal QtMultimedia::Playlist::itemInserted(start, end)
553
554 This signal is emitted after items have been inserted into the playlist. The new items are those
555 between \a start and \a end inclusive.
556*/
557
558/*!
559 \qmlsignal QtMultimedia::Playlist::itemAboutToBeRemoved(start, end)
560
561 This signal emitted when items are to be deleted from the playlist at \a start and ending at
562 \a end.
563*/
564
565/*!
566 \qmlsignal QtMultimedia::Playlist::itemRemoved(start, end)
567
568 This signal is emitted after items have been removed from the playlist. The removed items are
569 those between \a start and \a end inclusive.
570*/
571
572/*!
573 \qmlsignal QtMultimedia::Playlist::itemChanged(start, end)
574
575 This signal is emitted after items have been changed in the playlist between \a start and
576 \a end positions inclusive.
577*/
578
579/*!
580 \qmlsignal QtMultimedia::Playlist::loaded()
581
582 This signal is emitted when the playlist loading succeeded.
583*/
584
585/*!
586 \qmlsignal QtMultimedia::Playlist::loadFailed()
587
588 This signal is emitted when the playlist loading failed. \l error and \l errorString can be
589 checked for more information on the failure.
590*/
591
592QT_END_NAMESPACE
593
594#include "moc_qquickplaylist_p.cpp"
QObject * parent
Definition qobject.h:73
\inmodule QtCore
Definition qobject.h:103
Error error() const
\qmlproperty enumeration QtMultimedia::Playlist::error
void shuffle()
\qmlmethod QtMultimedia::Playlist::shuffle()
QHash< int, QByteArray > roleNames() const override
int itemCount() const
\qmlproperty int QtMultimedia::Playlist::itemCount
bool insertItem(int index, const QUrl &source)
\qmlmethod bool QtMultimedia::Playlist::insertItem(index, source)
void setPlaybackMode(PlaybackMode playbackMode)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
QQuickPlaylist(QObject *parent=0)
PlaybackMode playbackMode() const
\qmlproperty enumeration QtMultimedia::Playlist::playbackMode
void next()
\qmlmethod QtMultimedia::Playlist::next()
QString errorString() const
\qmlproperty string QtMultimedia::Playlist::errorString
bool removeItem(int index)
\qmlmethod bool QtMultimedia::Playlist::removeItem(index)
bool save(const QUrl &location, const QString &format=QString())
\qmlmethod bool QtMultimedia::Playlist::save(location, format)
int nextIndex(int steps=1)
\qmlmethod int QtMultimedia::Playlist::nextIndex(steps)
QUrl currentItemSource() const
\qmlproperty url QtMultimedia::Playlist::currentItemsource
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void addItem(const QUrl &source)
\qmlmethod bool QtMultimedia::Playlist::addItem(source)
void previous()
\qmlmethod QtMultimedia::Playlist::previous()
void classBegin() override
Invoked after class creation, but before any properties have been set.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
void clear()
\qmlmethod bool QtMultimedia::Playlist::clear()
void load(const QUrl &location, const QString &format=QString())
\qmlmethod QtMultimedia::Playlist::load(location, format)
int previousIndex(int steps=1)
\qmlmethod int QtMultimedia::Playlist::previousIndex(steps)
void setCurrentIndex(int currentIndex)
int currentIndex() const
\qmlproperty int QtMultimedia::Playlist::currentIndex
Combined button and popup list for selecting options.