-
UID:106529
-
- 注册时间2010-10-24
- 最后登录2024-03-13
- 在线时间429小时
-
- 发帖295
- 搜Ta的帖子
- 精华1
- 金钱3552
- 威望306
- 贡献值1
- 好评度296
-
访问TA的空间加好友用道具
|
Qt5中利用mplayer播放视频,可以暂停,重播,上一个,下一个,循环播放! showvideo.cpp - #include "showvideo.h"
- ShowVideo::ShowVideo(QWidget *parent,QListWidgetItem *item,QListWidget *list,QString path) :
- QDialog(parent)
- {
- //初始化窗口
- this->resize(1024,768);
- this->move(0,0);
- this->setStyleSheet(tr("background-color: rgba(255,255,255,100%)"));
- this->setWindowFlags(Qt::FramelessWindowHint);
- //初始化状态信息
- isFinish = false;
- isPause = false;
- //当前路径
- currentPath = path;
- //初始化图片数据
- showList = list;
- rowNum = showList->currentRow();
- rowCount = showList->count();
- currentItem = item;
- //文件名
- nameLabel = new QLabel(this);
- nameLabel->move(60,720);
- nameLabel->setText(currentItem->text());
- //显示图像的QLabel
- videoLabel = new QLabel(this);
- videoLabel->setStyleSheet("background-color: rgb(0,0,0)");
- videoLabel->move(10,10);
- videoLabel->resize(1004,678);
- //初始化进度条
- bar = new QProgressBar(this);
- bar->move(10,688);
- bar->resize(1004,15);
- bar->setMinimum(0);
- bar->setMaximum(99);
- bar->setTextVisible(true);
- bar->setValue(0);
- //设置return按钮属性
- retBtn = new QPushButton(this);
- retBtn->setIconSize(QSize(32,32));
- retBtn->setIcon(QIcon(tr(":/images/return.ico")));
- retBtn->resize(32,32);
- retBtn->setStyleSheet(tr("border-radius:10px;"));
- retBtn->setFocusPolicy(Qt::NoFocus);
- retBtn->move(984,715);
- connect(retBtn,SIGNAL(clicked()),this,SLOT(closeShowVideo()));
- //设置player\pause按钮属性
- startBtn = new QPushButton(this);
- startBtn->setIconSize(QSize(32,32));
- startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
- startBtn->resize(32,32);
- startBtn->setStyleSheet("border-radius:10px;");
- startBtn->setFocusPolicy(Qt::NoFocus);
- startBtn->move(495,715);
- connect(startBtn,SIGNAL(clicked()),this,SLOT(start_pauseVideo()));
- //上一个视频按钮属性
- forwardBtn = new QPushButton(this);
- forwardBtn->setIconSize(QSize(32,32));
- forwardBtn->setIcon(QIcon(tr(":/images/forward.ico")));
- forwardBtn->resize(32,32);
- forwardBtn->setStyleSheet(tr("background-color: rgba(50%,50%,50%,0%);border-radius:10px;"));
- forwardBtn->setFocusPolicy(Qt::NoFocus);
- forwardBtn->move(440,715);
- connect(forwardBtn,SIGNAL(clicked()),this,SLOT(forwardVideo()));
- //下一个视频按钮属性
- nextBtn = new QPushButton(this);
- nextBtn->setIconSize(QSize(32,32));
- nextBtn->setIcon(QIcon(tr(":/images/next.ico")));
- nextBtn->resize(32,32);
- nextBtn->setStyleSheet(tr("background-color: rgba(50%,50%,50%,0%);border-radius:10px;"));
- nextBtn->setFocusPolicy(Qt::NoFocus);
- nextBtn->move(550,715);
- connect(nextBtn,SIGNAL(clicked()),this,SLOT(nextVideo()));
- //删除图片按钮属性
- deleteBtn = new QPushButton(this);
- deleteBtn->setIconSize(QSize(32,32));
- deleteBtn->setIcon(QIcon(tr(":/images/delete.ico")));
- deleteBtn->resize(32,32);
- deleteBtn->setStyleSheet(tr("background-color: rgba(50%,50%,50%,0%);border-radius:10px;"));
- deleteBtn->setFocusPolicy(Qt::NoFocus);
- deleteBtn->move(20,715);
- connect(deleteBtn,SIGNAL(clicked()),this,SLOT(deleteVideo()));
- //初始化进程
- play_process = new QProcess(this);
- play_process->setProcessChannelMode(QProcess::MergedChannels);
- connect(play_process,SIGNAL(readyReadStandardOutput()),this,SLOT(getVideoInfo()));
- //定时获取播放进度
- timer = new QTimer(this);
- connect(timer,SIGNAL(timeout()),this,SLOT(sendPosCmd()));
- playVideo(currentPath + currentItem->text());
- }
- //获取视频信息
- void ShowVideo::getVideoInfo()
- {
- while(play_process->canReadLine())
- {
- QString message(play_process->readLine());
- if(message.left(21) == tr("ANS_PERCENT_POSITION="))
- {
- bar->setValue(message.remove(0,21).toInt(NULL,10));
- }
- //message即为读取的信息我们可以根据需要取我们要的信息如
- //文件总时间为:ANS_LENGTH=23.00
- //当前时间为:ANS_TIME_POSITION=23.00
- }
- }
- //播放视频
- void ShowVideo::playVideo(QString fileName)
- {
- QString str;
- str = tr("mplayer -slave -quiet -fps 15 ");
- str.append(fileName);
- str.append(tr(" -wid "));
- str.append(QString::number(videoLabel->winId()));
- play_process->start(str,QIODevice::ReadWrite);
- connect(play_process,SIGNAL(finished(int)),this,SLOT(getFinishStatus(int)));
- timer->start(30);
- nameLabel->setText(fileName.right(16));
- isFinish = false;
- }
- //获取播放进度百分比的命令
- void ShowVideo::sendPosCmd()
- {
- play_process->write("get_percent_pos\n");
- }
- //获取播放完成的状态
- void ShowVideo::getFinishStatus(int status)
- {
- if(status == 0)
- {
- bar->setValue(0);
- timer->stop();
- play_process->close();
- isFinish = true;
- startBtn->setIcon(QIcon(tr(":/images/start.ico")));
- isPause = true;
- }
- }
- //开始\暂停\重播 播放
- void ShowVideo::start_pauseVideo()
- {
- if(isPause == false)
- {
- timer->stop();
- startBtn->setIcon(QIcon(tr(":/images/start.ico")));
- isPause = true;
- play_process->write("pause\n");
- }else
- {
- startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
- isPause = false;
- if(!isFinish)
- {
- timer->start(30);
- play_process->write("pause\n");
- }else
- {
- this->playVideo(currentPath + showList->item(rowNum)->text());
- }
- }
- }
- //播放上一个视频
- void ShowVideo::forwardVideo()
- {
- QListWidgetItem *item;
- bar->setValue(0);
- timer->stop();
- play_process->close();
- startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
- isPause = false;
- if(rowNum == 0)
- {
- rowNum = rowCount - 1;
- item = showList->item(rowNum);
- this->playVideo(currentPath + item->text());
- return;
- }
- rowNum--;
- item = showList->item(rowNum);
- this->playVideo(currentPath + item->text());
- return;
- }
- //播放下一个视频
- void ShowVideo::nextVideo()
- {
- QListWidgetItem *item;
- bar->setValue(0);
- timer->stop();
- play_process->close();
- startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
- isPause = false;
- if(rowNum == rowCount - 1)
- {
- rowNum = 0;
- item = showList->item(rowNum);
- this->playVideo(currentPath + item->text());
- return;
- }
- rowNum++;
- item = showList->item(rowNum);
- this->playVideo(currentPath + item->text());
- }
- //删除视频文件
- void ShowVideo::deleteVideo()
- {
- int ret;
- QListWidgetItem *item;
- ret = QMessageBox::warning(this,NULL,
- tr("是否删除").append(showList->item(rowNum)->text()).append(tr("?")),
- QMessageBox::Yes | QMessageBox::No);
- if(ret == QMessageBox::Yes)
- {
- QFile::remove(currentPath + showList->item(rowNum)->text());
- showList->removeItemWidget(showList->item(rowNum));
- //删除item
- item = showList->takeItem(rowNum);
- showList->removeItemWidget(item);
- delete item;
- rowCount--;
- if(rowCount == 0)
- {
- bar->setValue(0);
- timer->stop();
- play_process->close();
- startBtn->setIcon(QIcon(tr(":/images/start.ico")));
- isPause = true;
- videoLabel->setStyleSheet(tr("background-color: rgba(255,255,255,255)"));
- nameLabel->setText("");
- return;
- }
- if(rowNum == rowCount)
- {
- rowNum = rowCount - 1;
- }
- item = showList->item(rowNum);
- bar->setValue(0);
- timer->stop();
- play_process->close();
- startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
- isPause = false;
- playVideo(currentPath + item->text());
- }
- }
- //返回上一级
- void ShowVideo::closeShowVideo()
- {
- timer->stop();
- play_process->close();
- emit quitApp();
- this->close();
- }
|