Qt中是否有一种方法可以强制QMediaPlayer缓冲文件而不播放它? [英] Is there a way in Qt to force QMediaPlayer to buffer a file without playing it?

查看:904
本文介绍了Qt中是否有一种方法可以强制QMediaPlayer缓冲文件而不播放它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将文件加载到QMediaPlayer实例中时,它不会自动缓冲文件.在您使用play()播放文件之前,MediaStatus保持为NoMedia,只有在此之后它才会以BufferedMedia结尾.我无法在文档中找到任何方法来强制播放器在不播放文件的情况下对其进行缓冲-有什么方法可以做到这一点?

When you load a file into a QMediaPlayer instance, it does not automatically buffer the file. The MediaStatus remains NoMedia until you play the file using play(), only after which it will end up as BufferedMedia. I can't find any way in the documentation to force the player to buffer the file without playing it - is there any way to do this?

现在,我正计划将其静音,播放文件,然后再次将其停止并取消静音,但这会使我感到肮脏.当然有更好的方法吗?

Right now I'm planning on muting it, playing the file, then stopping it again and unmuting it, but it makes me feel dirty. Surely there is a better way to do this?

顺便说一句,这是必要的,因为在文件被缓冲之前我无法检索duration,并且我需要duration在曲目中选择position才能开始播放.

This is necessary, by the way, because I can't retrieve the duration until the file has been buffered, and I need the duration to select a position in the track to start playing from.

谢谢

推荐答案

在您使用play()播放文件之前,MediaStatus仍为NoMedia

The MediaStatus remains NoMedia until you play the file using play()

不在Qt 5中,这也不是您不知道持续时间"和设置位置"的原因. 当您使用setMedia 设置媒体时,它不会等待媒体完成加载(并且不会检查错误).加载介质时会发出mediaStatusChanged()信号,因此请听完该信号,并在介质加载完成时通知error()信号.

Not in Qt 5, and its not the reason you can't know 'duration' and set 'position'. When you set the media with setMedia it does not wait for the media to finish loading (and does not check for errors). a signal mediaStatusChanged() is emitted when media is loaded, so listen to that signal and error() signal to be notified when the media loading is finished.

connect(player, &QMediaPlayer::mediaStatusChanged, this, [=]() {
    qDebug() << "Media Status:" << player->mediaStatus();

我需要时间来选择曲目中的位置以开始播放 来自.

I need the duration to select a position in the track to start playing from.

加载媒体文件并播放前,您可以检查持续时间,并将播放器设置到所需位置,但这最好在持续时间从0更改为媒体时执行加载后的持续时间,因此请连接到信号durationChanged():

Once the media file is loaded and before play, you can check the duration and you can set the player to desired position, but this is best to do once the duration changes from 0 to the media duration after loading, so connect to signal durationChanged():

connect(player, &QMediaPlayer::durationChanged, this, [&](qint64 duration) {
    qDebug() << "Media duration = " << duration;
    player->setPosition(duration/2);
    qDebug() << "Set position:" << player->position();
});

我在文档中找不到任何强制播放器执行以下操作的方法: 缓冲文件而不播放文件-有什么方法吗?

I can't find any way in the documentation to force the player to buffer the file without playing it - is there any way to do this?

是的,从文件创建一个缓冲区,然后将mediacontent设置为您的缓冲区(但这不是上面的步骤所必需的,它只是提供了一种更快的媒体查找方式):

Yes, create a buffer from file then set mediacontent to your buffer (but this is not required to do the above, it just provides a faster way to seek in media):

QString fileName=QFileDialog::getOpenFileName(this,"Select:","","( *.mp3)");
QFile mediafile(fileName);
mediafile.open(QIODevice::ReadOnly);
QByteArray *ba = new QByteArray();
ba->append(mediafile.readAll());
QBuffer *buffer = new QBuffer(ba);
buffer->open(QIODevice::ReadOnly);
buffer->reset(); //seek 0
player->setMedia(QMediaContent(), buffer);

这篇关于Qt中是否有一种方法可以强制QMediaPlayer缓冲文件而不播放它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆