QMediaPlaylist保存播放列表

QMediaPlaylist类提供了save()和load()函数供我们保存和加载播放列表。

这是在网上找到的示例片段:

地址:https://qt.gitorious.org/qt/qtmultimedia/source/51334d299520118bbbc077696ca589f972a2f2ea:src/multimedia/playback/qmediaplaylist.cpp#L574

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(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");
加载方式类似

转载于:https://www.cnblogs.com/csstudy/p/3647194.html

你可能感兴趣的:(QMediaPlaylist保存播放列表)