简介
使用QT的QMediaPlayer类播放MP4格式的视频。
代码示例
头文件
Ui::MainWindow *ui;
QMediaPlaylist *playlist;
QMediaPlayer *player;
QVideoWidget *videoWidget;
方式一
应用窗体外播放视频。
.cpp文件
playlist = new QMediaPlaylist;
playlist->addMedia(QUrl("qrc:/video/sample5s.mp4"));
playlist->addMedia(QUrl("qrc:/video/SampleVideo1mb.mp4"));
playlist->setCurrentIndex(0);
player = new QMediaPlayer;
player->setPlaylist(playlist);
videoWidget = new QVideoWidget;
videoWidget->setFixedSize(1366,768); //设置video窗体大小
videoWidget->setWindowFlag(Qt::FramelessWindowHint); //设置无边框
player->setVideoOutput(videoWidget);
videoWidget->show();
player->play();
效果如下
方式二
UI设计器界面拖拽一个Widget,右键提升为QVideoWidget,窗体内播放视频;
.cpp文件
player = new QMediaPlayer;
playlist = new QMediaPlaylist(player);
playlist->addMedia(QUrl("qrc:/video/sample5s.mp4"));
playlist->addMedia(QUrl("qrc:/video/SampleVideo1mb.mp4"));
playlist->setCurrentIndex(0);
playlist->setPlaybackMode(QMediaPlaylist::Loop);//循环播放
player->setPlaylist(playlist);
ui->videowidget->setAspectRatioMode(Qt::IgnoreAspectRatio);//自适应铺满界面
//ui->videowidget->setFullScreen(true); //全屏
player->setVideoOutput(ui->videowidget);
player->play();
效果如下**
QMediaPlayer播放视频常见信号处理
常用信号及其绑定
//播放进度
connect(player,&QMediaPlayer::positionChanged,this,&MainWindow::updatePosition);
//媒体状态
connect(player, &QMediaPlayer::mediaStatusChanged, this, &MainWindow::mediaStatusChanged);
//播放错误状态捕获
connect(player, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>
(&QMediaPlayer::error), this, &MainWindow::showError);
//播放状态
connect(player, &QMediaPlayer::stateChanged, this, &MainWindow::stateChanged);
//获取元数据
connect(player, &QMediaPlayer::metaDataAvailableChanged,
this, &MainWindow::metaDataAvailableChanged);
对应槽函数处理方法
//position 为当前播放时长,不播放则为0
//player->duration() 为视频的总时长,但当视频还没有加载完成时返回为0
void MainWindow::updatePosition(qint64 position)
{
if(position > 0)
{
ui->videowidget->setVisible(true);
}
//不使用QMediaPlaylist时,实现循环播放
if(position == player->duration())
{
if(player->duration() != 0)
{
player->play();
}
}
}
void MainWindow::mediaStatusChanged(QMediaPlayer::MediaStatus status)
{
switch (status) {
case QMediaPlayer::UnknownMediaStatus:
ui->label->setText(tr("媒体未知状况!"));
break;
case QMediaPlayer::NoMedia:
ui->label->setText(tr("没有媒体文件!"));
break;
case QMediaPlayer::BufferingMedia:
ui->label->setText(tr("正在缓冲媒体文件!"));
break;
case QMediaPlayer::BufferedMedia:
ui->label->setText(tr("媒体文件缓冲完成!"));
break;
case QMediaPlayer::LoadingMedia:
ui->label->setText(tr("正在加载媒体!"));
break;
case QMediaPlayer::StalledMedia:
ui->label->setText(tr("播放停滞!"));
break;
case QMediaPlayer::EndOfMedia:
ui->label->setText(tr("播放结束!"));
break;
case QMediaPlayer::LoadedMedia:
ui->label->setText(tr("媒体加载完成!"));
break;
case QMediaPlayer::InvalidMedia:
ui->label->setText(tr("不可用的媒体文件!"));
break;
default: break;
}
}
void MainWindow::stateChanged(QMediaPlayer::State state)
{
switch (state) {
case QMediaPlayer::StoppedState:
ui->label_2->setText(tr("停止状态!"));
break;
case QMediaPlayer::PlayingState:
ui->label_2->setText(tr("播放状态!"));
break;
case QMediaPlayer::PausedState:
ui->label_2->setText(tr("暂停状态!"));
break;
default: break;
}
}
void MainWindow::showError(QMediaPlayer::Error error)
{
switch (error) {
case QMediaPlayer::NoError:
ui->label_3->setText(tr("没有错误!"));
break;
case QMediaPlayer::ResourceError:
ui->label_3->setText(tr("媒体资源无法被解析!"));
break;
case QMediaPlayer::FormatError:
ui->label_3->setText(tr("不支持该媒体格式!"));
break;
case QMediaPlayer::NetworkError:
ui->label_3->setText(tr("发生了一个网络错误!"));
break;
case QMediaPlayer::AccessDeniedError:
ui->label_3->setText(tr("没有播放权限!"));
break;
case QMediaPlayer::ServiceMissingError:
ui->label_3->setText(tr("没有发现有效的播放服务!"));
break;
default:
ui->label_3->setText(tr("未知错误码!"));
break;
}
}
// 获取元数据
void MainWindow::metaDataAvailableChanged(bool available)
{
if(available)
{
QString title = player->metaData("Title").toString();
QString author = player->metaData("Author").toString();
setWindowTitle(title + "-" + author);
}
}
状态处理,引用部分内容
https://blog.csdn.net/zhaoyun_zzz/article/details/90290552