• 9719阅读
  • 4回复

QT5杂谈(四)------Qt5中利用mplayer播放视频 [复制链接]

上一主题 下一主题
离线青春岁月
 

只看楼主 倒序阅读 楼主  发表于: 2014-04-28
Qt5中利用mplayer播放视频,可以暂停,重播,上一个,下一个,循环播放!
showvideo.cpp
  1. #include "showvideo.h"
  2. ShowVideo::ShowVideo(QWidget *parent,QListWidgetItem *item,QListWidget *list,QString path) :
  3.     QDialog(parent)
  4. {
  5.     //初始化窗口
  6.     this->resize(1024,768);
  7.     this->move(0,0);
  8.     this->setStyleSheet(tr("background-color: rgba(255,255,255,100%)"));
  9.     this->setWindowFlags(Qt::FramelessWindowHint);
  10.     //初始化状态信息
  11.     isFinish = false;
  12.     isPause = false;
  13.     //当前路径
  14.     currentPath = path;
  15.     //初始化图片数据
  16.     showList = list;
  17.     rowNum = showList->currentRow();
  18.     rowCount = showList->count();
  19.     currentItem = item;
  20.     //文件名
  21.     nameLabel = new QLabel(this);
  22.     nameLabel->move(60,720);
  23.     nameLabel->setText(currentItem->text());
  24.     //显示图像的QLabel
  25.     videoLabel = new QLabel(this);
  26.     videoLabel->setStyleSheet("background-color: rgb(0,0,0)");
  27.     videoLabel->move(10,10);
  28.     videoLabel->resize(1004,678);
  29.     //初始化进度条
  30.     bar = new QProgressBar(this);
  31.     bar->move(10,688);
  32.     bar->resize(1004,15);
  33.     bar->setMinimum(0);
  34.     bar->setMaximum(99);
  35.     bar->setTextVisible(true);
  36.     bar->setValue(0);
  37.     //设置return按钮属性
  38.     retBtn = new QPushButton(this);
  39.     retBtn->setIconSize(QSize(32,32));
  40.     retBtn->setIcon(QIcon(tr(":/images/return.ico")));
  41.     retBtn->resize(32,32);
  42.     retBtn->setStyleSheet(tr("border-radius:10px;"));
  43.     retBtn->setFocusPolicy(Qt::NoFocus);
  44.     retBtn->move(984,715);
  45.     connect(retBtn,SIGNAL(clicked()),this,SLOT(closeShowVideo()));
  46.     //设置player\pause按钮属性
  47.     startBtn = new QPushButton(this);
  48.     startBtn->setIconSize(QSize(32,32));
  49.     startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
  50.     startBtn->resize(32,32);
  51.     startBtn->setStyleSheet("border-radius:10px;");
  52.     startBtn->setFocusPolicy(Qt::NoFocus);
  53.     startBtn->move(495,715);
  54.     connect(startBtn,SIGNAL(clicked()),this,SLOT(start_pauseVideo()));
  55.     //上一个视频按钮属性
  56.     forwardBtn = new QPushButton(this);
  57.     forwardBtn->setIconSize(QSize(32,32));
  58.     forwardBtn->setIcon(QIcon(tr(":/images/forward.ico")));
  59.     forwardBtn->resize(32,32);
  60.     forwardBtn->setStyleSheet(tr("background-color: rgba(50%,50%,50%,0%);border-radius:10px;"));
  61.     forwardBtn->setFocusPolicy(Qt::NoFocus);
  62.     forwardBtn->move(440,715);
  63.     connect(forwardBtn,SIGNAL(clicked()),this,SLOT(forwardVideo()));
  64.     //下一个视频按钮属性
  65.     nextBtn = new QPushButton(this);
  66.     nextBtn->setIconSize(QSize(32,32));
  67.     nextBtn->setIcon(QIcon(tr(":/images/next.ico")));
  68.     nextBtn->resize(32,32);
  69.     nextBtn->setStyleSheet(tr("background-color: rgba(50%,50%,50%,0%);border-radius:10px;"));
  70.     nextBtn->setFocusPolicy(Qt::NoFocus);
  71.     nextBtn->move(550,715);
  72.     connect(nextBtn,SIGNAL(clicked()),this,SLOT(nextVideo()));
  73.     //删除图片按钮属性
  74.     deleteBtn = new QPushButton(this);
  75.     deleteBtn->setIconSize(QSize(32,32));
  76.     deleteBtn->setIcon(QIcon(tr(":/images/delete.ico")));
  77.     deleteBtn->resize(32,32);
  78.     deleteBtn->setStyleSheet(tr("background-color: rgba(50%,50%,50%,0%);border-radius:10px;"));
  79.     deleteBtn->setFocusPolicy(Qt::NoFocus);
  80.     deleteBtn->move(20,715);
  81.     connect(deleteBtn,SIGNAL(clicked()),this,SLOT(deleteVideo()));
  82.     //初始化进程
  83.     play_process = new QProcess(this);
  84.     play_process->setProcessChannelMode(QProcess::MergedChannels);
  85.     connect(play_process,SIGNAL(readyReadStandardOutput()),this,SLOT(getVideoInfo()));
  86.     //定时获取播放进度
  87.     timer = new QTimer(this);
  88.     connect(timer,SIGNAL(timeout()),this,SLOT(sendPosCmd()));
  89.     playVideo(currentPath + currentItem->text());
  90. }
  91. //获取视频信息
  92. void ShowVideo::getVideoInfo()
  93. {
  94.     while(play_process->canReadLine())
  95.     {
  96.         QString message(play_process->readLine());
  97.         if(message.left(21) == tr("ANS_PERCENT_POSITION="))
  98.         {
  99.             bar->setValue(message.remove(0,21).toInt(NULL,10));
  100.         }
  101.         //message即为读取的信息我们可以根据需要取我们要的信息如
  102.         //文件总时间为:ANS_LENGTH=23.00
  103.         //当前时间为:ANS_TIME_POSITION=23.00
  104.     }
  105. }
  106. //播放视频
  107. void ShowVideo::playVideo(QString fileName)
  108. {
  109.     QString str;
  110.     str = tr("mplayer -slave -quiet -fps 15 ");
  111.     str.append(fileName);
  112.     str.append(tr(" -wid "));
  113.     str.append(QString::number(videoLabel->winId()));
  114.     play_process->start(str,QIODevice::ReadWrite);
  115.     connect(play_process,SIGNAL(finished(int)),this,SLOT(getFinishStatus(int)));
  116.     timer->start(30);
  117.     nameLabel->setText(fileName.right(16));
  118.     isFinish = false;
  119. }
  120. //获取播放进度百分比的命令
  121. void ShowVideo::sendPosCmd()
  122. {
  123.     play_process->write("get_percent_pos\n");
  124. }
  125. //获取播放完成的状态
  126. void ShowVideo::getFinishStatus(int status)
  127. {
  128.     if(status == 0)
  129.     {
  130.         bar->setValue(0);
  131.         timer->stop();
  132.         play_process->close();
  133.         isFinish = true;
  134.         startBtn->setIcon(QIcon(tr(":/images/start.ico")));
  135.         isPause = true;
  136.     }
  137. }
  138. //开始\暂停\重播 播放
  139. void ShowVideo::start_pauseVideo()
  140. {
  141.     if(isPause == false)
  142.     {
  143.         timer->stop();
  144.         startBtn->setIcon(QIcon(tr(":/images/start.ico")));
  145.         isPause = true;
  146.         play_process->write("pause\n");
  147.     }else
  148.     {
  149.         startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
  150.         isPause = false;
  151.         if(!isFinish)
  152.         {
  153.             timer->start(30);
  154.             play_process->write("pause\n");
  155.         }else
  156.         {
  157.             this->playVideo(currentPath + showList->item(rowNum)->text());
  158.         }
  159.     }
  160. }
  161. //播放上一个视频
  162. void ShowVideo::forwardVideo()
  163. {
  164.     QListWidgetItem *item;
  165.     bar->setValue(0);
  166.     timer->stop();
  167.     play_process->close();
  168.     startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
  169.     isPause = false;
  170.     if(rowNum == 0)
  171.     {
  172.         rowNum = rowCount - 1;
  173.         item = showList->item(rowNum);
  174.         this->playVideo(currentPath + item->text());
  175.         return;
  176.     }
  177.     rowNum--;
  178.     item = showList->item(rowNum);
  179.     this->playVideo(currentPath + item->text());
  180.     return;
  181. }
  182. //播放下一个视频
  183. void ShowVideo::nextVideo()
  184. {
  185.     QListWidgetItem *item;
  186.     bar->setValue(0);
  187.     timer->stop();
  188.     play_process->close();
  189.     startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
  190.     isPause = false;
  191.     if(rowNum == rowCount - 1)
  192.     {
  193.         rowNum = 0;
  194.         item = showList->item(rowNum);
  195.         this->playVideo(currentPath + item->text());
  196.         return;
  197.     }
  198.     rowNum++;
  199.     item = showList->item(rowNum);
  200.     this->playVideo(currentPath + item->text());
  201. }
  202. //删除视频文件
  203. void ShowVideo::deleteVideo()
  204. {
  205.     int ret;
  206.     QListWidgetItem *item;
  207.     ret = QMessageBox::warning(this,NULL,
  208.                                tr("是否删除").append(showList->item(rowNum)->text()).append(tr("?")),
  209.                                QMessageBox::Yes | QMessageBox::No);
  210.     if(ret == QMessageBox::Yes)
  211.     {
  212.         QFile::remove(currentPath + showList->item(rowNum)->text());
  213.         showList->removeItemWidget(showList->item(rowNum));
  214.         //删除item
  215.         item = showList->takeItem(rowNum);
  216.         showList->removeItemWidget(item);
  217.         delete item;
  218.         rowCount--;
  219.         if(rowCount == 0)
  220.         {
  221.             bar->setValue(0);
  222.             timer->stop();
  223.             play_process->close();
  224.             startBtn->setIcon(QIcon(tr(":/images/start.ico")));
  225.             isPause = true;
  226.             videoLabel->setStyleSheet(tr("background-color: rgba(255,255,255,255)"));
  227.             nameLabel->setText("");
  228.             return;
  229.         }
  230.         if(rowNum == rowCount)
  231.         {
  232.             rowNum = rowCount - 1;
  233.         }
  234.         item = showList->item(rowNum);
  235.         bar->setValue(0);
  236.         timer->stop();
  237.         play_process->close();
  238.         startBtn->setIcon(QIcon(tr(":/images/pause.ico")));
  239.         isPause = false;
  240.         playVideo(currentPath + item->text());
  241.     }
  242. }
  243. //返回上一级
  244. void ShowVideo::closeShowVideo()
  245. {
  246.     timer->stop();
  247.     play_process->close();
  248.     emit quitApp();
  249.     this->close();
  250. }

go!   go!  go!  go!
离线自强不吸

只看该作者 1楼 发表于: 2014-05-06
楼主上个效果图瞅瞅啊
自强不吸!
离线玩意儿

只看该作者 2楼 发表于: 2014-11-03
我想问一下怎样双击视频文件时自动打开视频播放程序并播放???
离线slf0205

只看该作者 3楼 发表于: 2014-11-05
离线bb1994525

只看该作者 4楼 发表于: 2014-11-15
最近在学习视频播放器,楼主能不能把这个文件的完整工程上传一下?交流学习?或者发我邮箱也可: bb1994525@126.com  希望您能及时看到,谢谢!
快速回复
限100 字节
 
上一个 下一个