QMediaPlaylist类提供了save()和load()函数供我们保存和加载播放列表。
这是在网上找到的示例片段:
bool QMediaPlaylist::save(const QUrl &location, const char *format) { Q_D(QMediaPlaylist); d->error = NoError; d->errorString.clear(); if (d->playlist()->save(location,format)) return true; QFile file(location.toLocalFile()); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { d->error = AccessDeniedError; d->errorString = tr("The file could not be accessed."); return false; } return save(&file, format); } /*! Save playlist to QIODevice a device using format a format. Returns true if playlist was saved successfully, otherwise returns false. */ bool QMediaPlaylist::save(QIODevice * device, const char *format) { Q_D(QMediaPlaylist); d->error = NoError; d->errorString.clear(); if (d->playlist()->save(device,format)) return true; foreach (QString const& key, playlistIOLoader()->keys()) { QMediaPlaylistIOInterface* plugin = qobject_cast<QMediaPlaylistIOInterface*>(playlistIOLoader()->instance(key)); if (plugin && plugin->canWrite(device,format)) { QMediaPlaylistWriter *writer = plugin->createWriter(device,QByteArray(format)); if (writer && d->writeItems(writer)) { delete writer; return true; } delete writer; } } d->error = FormatNotSupportedError; d->errorString = tr("Playlist format is not supported."); return false; }
自己测试了一下(使用m3u格式测试成功):
QFile file("plist.m3u"); file.open(QIODevice::WriteOnly); playList->save(&file,"m3u");
加载方式类似