1、qt5.14.2 plugins\mediaservice拷贝到exe目录下
2、x86、x64、debug、release分清,拷贝错了就会报以下错误
const QMediaPlayer::Error media_error = pMediaPlayer_->error();
//ServiceMissingError 服务丢失错误(找不到有效的播放服务,无法继续播放。)
3、去LAVFilters官网下载最新版x86、x64安装程序,安装时别搞混了版本。
以管理员权限运行install_splitter.bat等三个bat,直接运行会报错。
可能只运行install_splitter.bat就行了。
有些网站说K-Lite更好更全面,实际播放视频时黑屏了,原因不明,卸载K-Lite后重新使用install_splitter.bat安装LAVFilters就好了。
QMediaPlayer相关错误信息获得方式
//https://blog.csdn.net/u010037542/article/details/100075368 状态说明
const QMediaPlayer::State media_state = pMediaPlayer_->state();//StoppedState
const QMediaPlayer::MediaStatus media_state2 = pMediaPlayer_->mediaStatus();//UnknownMediaStatus
const QMediaPlayer::Error media_error = pMediaPlayer_->error();//ServiceMissingError 服务丢失错误(找不到有效的播放服务,无法继续播放。)
const QString media_error_str = pMediaPlayer_->errorString();//
//https://www.cnblogs.com/zhuchunlin/p/16485929.html
//http://t.zoukankan.com/lxuechao-p-12677357.html
const QMediaPlaylist::Error media_list_error = pMediaList_->error();//NoError
const QString media_list_error_str = pMediaList_->errorString();//
//https://blog.csdn.net/u010796650/article/details/107066957/ 类似问题,处理方式不明,原因也不是很明确
//mediaPlayer.error = 5 即ServiceMissing
//The audio cannot be played because the media service could not be instantiated(无法播放音频,因为无法实例化媒体服务。)
//QMediaPlayer::ServiceMissingError 5 A valid playback service was not found, playback cannot proceed.
4、最后附上几种测试代码
void MyQMediaPlayerTest::slotOnPushButton_play()
{
const QString mp3_file = ui_.lineEdit_file->text();
if (mp3_file.length() == 0)
{
return;
}
if (!QFile::exists(mp3_file))
{
return;
}
if (nullptr == pMediaList_)
{
pMediaList_ = new QMediaPlaylist(this);
}
if (nullptr == pMediaPlayer_)
{
pMediaPlayer_ = new QMediaPlayer(this);
QObject::connect(pMediaPlayer_, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(OnState(QMediaPlayer::State)));
}
//
if (mp3_file.endsWith("mp3"))
{
//PlayMp3(pMediaPlayer_, mp3_file);
//PlayMp3InBuffer(pMediaPlayer_, mp3_file);
PlayMp3ByList(pMediaPlayer_, pMediaList_, mp3_file);
}
else if (mp3_file.endsWith("wav"))
{
//PlayWav(mp3_file);//C:/code/git/game/Win32/Debug/wav/Ring01.wav
PlayWavEffect(mp3_file);
}
else
{
//
}
const QMediaPlayer::State media_state = pMediaPlayer_->state();//StoppedState
const QMediaPlayer::MediaStatus media_state2 = pMediaPlayer_->mediaStatus();//UnknownMediaStatus
const QMediaPlayer::Error media_error = pMediaPlayer_->error();//ServiceMissingError 服务丢失错误(找不到有效的播放服务,无法继续播放。)
const QString media_error_str = pMediaPlayer_->errorString();//
const QMediaPlaylist::Error media_list_error = pMediaList_->error();//NoError
const QString media_list_error_str = pMediaList_->errorString();//
}
void MyQMediaPlayerTest::PlayMp3InBuffer(QMediaPlayer * pMediaPlayer, const QString &mp3_file)
{
if (nullptr == pMediaPlayer)
{
return;
}
QFile mp3_file_temp(mp3_file);
if (!mp3_file_temp.open(QIODevice::ReadOnly))
{
qDebug() << "文件打开失败,请重试~";
}
//下面用的对象,必须动态申请,不然构造函数执行完毕,局部变量内存会被释放
QByteArray *data = new QByteArray(mp3_file_temp.readAll());
mp3_file_temp.close();
QBuffer *buffer = new QBuffer(data, this);
if (!buffer->open(QIODevice::ReadWrite))
{
qDebug() << "buffer error";
}
pMediaPlayer->setMedia(QMediaContent(), buffer);
pMediaPlayer_->setVolume(80); //音量
pMediaPlayer->play();
}
void MyQMediaPlayerTest::PlayMp3(QMediaPlayer * pMediaPlayer, const QString & mp3_file)
{
if (nullptr == pMediaPlayer)
{
return;
}
if (!QFile::exists(mp3_file))
{
return;
}
pMediaPlayer_->setMedia(QUrl::fromLocalFile(mp3_file));
pMediaPlayer_->setVolume(80); //音量
pMediaPlayer_->play();
}
void MyQMediaPlayerTest::PlayMp3ByList(QMediaPlayer * pMediaPlayer,
QMediaPlaylist * pMediaList, const QString & mp3_file)
{
if (nullptr == pMediaPlayer || nullptr == pMediaList)
{
return;
}
if (!QFile::exists(mp3_file))
{
return;
}
pMediaPlayer->setPlaylist(pMediaList);
pMediaPlayer->stop();
pMediaList->clear();
pMediaList->addMedia(QMediaContent(QUrl::fromLocalFile(mp3_file)));
pMediaList->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);
//pMediaList->setCurrentIndex(0);
//pMediaList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
pMediaPlayer->setVolume(80); //音量
pMediaPlayer->play();
}
void MyQMediaPlayerTest::PlayWav(const QString & wav_file)
{
if (!QFile::exists(wav_file))
{
return;
}
QSound::play(wav_file);
}
void MyQMediaPlayerTest::PlayWavEffect(const QString & wav_file)
{
if (!QFile::exists(wav_file))
{
return;
}
QSoundEffect effect;
effect.setSource(QUrl::fromLocalFile(wav_file));
effect.setLoopCount(QSoundEffect::Infinite);
//effect.setVolume(0.8f);
effect.setVolume(80);
effect.play();
}