文档说QMediaPlaylist::addMedia如果失败则返回false:



但是,即使文件不存在,此代码也将显示true:

QMediaPlayer *player = new QMediaPlayer;
QMediaPlaylist *playlist = new QMediaPlaylist(player);
qDebug() << playlist->addMedia(QUrl("this file doesn't exist.mp4"));

如果文件不存在,如何将操作视为成功?

最佳答案

进入Qt源代码后,我看到QMediaPlaylist::addMedia()调用QMediaNetworkPlaylistProvider::addMedia(),它始终返回true:

bool QMediaPlaylist::addMedia(const QMediaContent &content)
{
    return d_func()->control->playlistProvider()->addMedia(content);
}

bool QMediaNetworkPlaylistProvider::addMedia(const QMediaContent &content)
{
    Q_D(QMediaNetworkPlaylistProvider);

    int pos = d->resources.count();

    emit mediaAboutToBeInserted(pos, pos);
    d->resources.append(content);
    emit mediaInserted(pos, pos);

    return true;
}

尽管为什么它需要返回始终为真的 bool(boolean) 值对我来说还是一个谜。

08-16 08:47