QT基于Mplayer媒体播放器应用教程是本文要介绍的内容,主要是来了解并学习QT总媒体播放器的应用,本文通过代码来实现QT基于mplayer播放器的案例,具体代码的实现来看内容。
播放器全屏音量时间进度都已经实现,追加快捷键设计...
- MAINWINDOW.H
- #ifndefMAINWINDOW_H
- #defineMAINWINDOW_H
- #include
- #include
- #include
- #include
- #include
- #include
- namespaceUi{
- classMainWindow;
- }
- classMainWindow:publicQMainWindow
- {
- Q_OBJECT
- public:
- explicitMainWindow(QWidget*parent=0);
- ~MainWindow();
- private:
- Ui::MainWindow*ui;
- QProcess*mplayerProcess;
- QStringfilename;
- QTimer*poller;
- QWidget*w;
- QDockWidget*shapesDockWidget1;
- QDockWidget*shapesDockWidget2;
- QTimer*interval;
- QAction*at2;
- QAction*at3;
- QAction*at4;
- QAction*at5;
- QMenu*menu;
- QPointm_dragPosition;
- privateslots:
- voidon_textEdit_3_textChanged();
- voidon_spinBox_editingFinished();
- voidon_spinBox_valueChanged(int);
- voidwriteSettings();
- voidreadSettings();
- voidon_pushButton_10_clicked();
- voidon_pushButton_9_clicked();
- voidon_pushButton_6_clicked();
- voidon_pushButton_5_clicked();
- voidon_pushButton_3_clicked();
- voidon_pushButton_2_clicked();
- voidon_horizontalSlider_2_valueChanged(intvalue);
- voidon_pushButton_8_clicked();
- voidon_pushButton_clicked();
- voidon_pushButton_7_clicked();
- voidon_pushButton_4_clicked();
- voidtimeLineChanged(intpos);
- voidpollCurrentTime();
- voidreject();
- voidenterFullScreen();
- voidenter();
- voidcloseEvent(QCloseEvent*ex);
- voidcontextMenuEvent(QContextMenuEvent*event);
- voidmouseMoveEvent(QMouseEvent*event);
- voidmousePressEvent(QMouseEvent*event);
- booleventFilter(QObject*obj,QEvent*ev);
- };
- #endif//MAINWINDOW_H
- MAINWINDOW.cpp
- #include"mainwindow.h"
- #include"ui_mainwindow.h"
- #include"QProcess"
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- MainWindow::MainWindow(QWidget*parent):
- QMainWindow(parent),
- ui(newUi::MainWindow)
- {
- ui->setupUi(this);
- mplayerProcess=newQProcess(this);
- shapesDockWidget1=newQDockWidget(QString::fromLocal8Bit("播放界面"));
- shapesDockWidget1->setWidget(ui->widget_4);
- shapesDockWidget2=newQDockWidget(QString::fromLocal8Bit("播放时间"));
- shapesDockWidget2->setWidget(ui->widget_2);
- //QWidget*wi=newQWidget(this);
- //wi->resize(400,300);
- addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget1);
- addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget2);
- shapesDockWidget2->show();
- QVBoxLayout*vLayout=newQVBoxLayout(this);
- vLayout->addWidget(ui->widget);
- w=newQWidget(this);
- w->setLayout(vLayout);
- this->setCentralWidget(w);
- poller=newQTimer(this);
- interval=newQTimer(this);
- connect(interval,SIGNAL(timeout()),this,SLOT(reject()));
- interval->start(1000);
- connect(ui->horizontalSlider,SIGNAL(sliderMoved(int)),this,SLOT(timeLineChanged(int)));
- connect(mplayerProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(reject()));
- connect(poller,SIGNAL(timeout()),this,SLOT(pollCurrentTime()));
- //setWindowOpacity(0.95);
- QPaletteplt=palette();
- //plt.setColor(QPalette::Background,QColor("Black"));
- plt.setColor(QPalette::Background,QColor("Grey"));
- this->setPalette(plt);
- ui->label_4->setText(QString::fromLocal8Bit("不是全屏"));
- w->setAutoFillBackground(true);
- QAction*at1=newQAction("进入全屏",this);
- this->addAction(at1);
- this->setAcceptDrops(true);
- at1->setShortcuts(QList()<
- //connect(at1,SIGNAL(triggered()),this,SLOT(enterFullScreen()));
- at2=newQAction(QString::fromLocal8Bit("暂停/开始"),this);
- this->addAction(at2);
- this->setAcceptDrops(true);
- at2->setShortcuts(QList()<
- connect(at2,SIGNAL(triggered()),this,SLOT(enter()));
- at3=newQAction(QString::fromLocal8Bit("全屏"),this);
- this->addAction(at3);
- this->setAcceptDrops(true);
- at3->setShortcuts(QList()<
- connect(at3,SIGNAL(triggered()),ui->pushButton_5,SLOT(click()));
- at4=newQAction(QString::fromLocal8Bit("退出全屏"),this);
- this->addAction(at4);
- this->setAcceptDrops(true);
- at4->setShortcuts(QList()<
- connect(at4,SIGNAL(triggered()),ui->pushButton_6,SLOT(click()));
- at5=newQAction(QString::fromLocal8Bit("打开"),this);
- this->addAction(at5);
- this->setAcceptDrops(true);
- at5->setShortcuts(QList()<
- connect(at5,SIGNAL(triggered()),ui->pushButton_4,SLOT(click()));
- QAction*at6=newQAction("前进",this);
- this->addAction(at6);
- this->setAcceptDrops(true);
- at6->setShortcuts(QList()<
- connect(at6,SIGNAL(triggered()),ui->pushButton_9,SLOT(click()));
- QAction*at7=newQAction("后退",this);
- this->addAction(at7);
- this->setAcceptDrops(true);
- at7->setShortcuts(QList()<
- connect(at7,SIGNAL(triggered()),ui->pushButton_10,SLOT(click()));
- QAction*at8=newQAction("提高音量",this);
- this->addAction(at8);
- this->setAcceptDrops(true);
- at8->setShortcuts(QList()<
- connect(at8,SIGNAL(triggered()),ui->pushButton_2,SLOT(click()));
- QAction*at9=newQAction("降低",this);
- this->addAction(at9);
- this->setAcceptDrops(true);
- at9->setShortcuts(QList()<
- connect(at9,SIGNAL(triggered()),ui->pushButton_3,SLOT(click()));
- this->readSettings();
- w->installEventFilter(this);
- intx=(QApplication::desktop()->width()-this->width())/2;
- inty=(QApplication::desktop()->height()-this->height())/2;
- this->move(x,y);
- this->resize(800,600);
- //QDesktopWidget*desktop=QApplication::desktop();
- //intwidth=desktop->width();
- //intheight=desktop->height();
- }
- voidMainWindow::enterFullScreen()
- {
- mplayerProcess->write("vo_fullscreen/n");
- }
- voidMainWindow::enter()
- {
- if(ui->label_6->text()==QString::fromLocal8Bit("开始"))
- {
- mplayerProcess->write("pause/n");
- poller->stop();
- ui->label_6->setText(QString::fromLocal8Bit("暂停"));
- }
- elseif(ui->label_6->text()==QString::fromLocal8Bit("暂停"))
- {
- mplayerProcess->write("pause/n");
- mplayerProcess->write("get_time_length/n");
- poller->start(10);
- ui->label_6->setText(QString::fromLocal8Bit("开始"));
- }
- }
- MainWindow::~MainWindow()
- {
- deleteui;
- }
- voidMainWindow::pollCurrentTime()
- {
- mplayerProcess->write("get_time_pos/n");
- //qDebug()text();
- //log->append(QString(buffer));
- ui->textEdit->setText(QString::number(ui->horizontalSlider->maximum()/3600)
- +":"+QString::number((ui->horizontalSlider->maximum()/60)%60)+":"
- +QString::number(ui->horizontalSlider->maximum()%60));
- ui->textEdit_2->setText(QString::number(ui->horizontalSlider->value()/3600)
- +":"+QString::number((ui->horizontalSlider->value()/60)%60)+":"+QString::number(ui->horizontalSlider->value()%60));
- }
- voidMainWindow::reject()
- {
- this->setFocus();
- //ui->label_5->setText(QString::number(ui->horizontalSlider->maximum()/3600)+":"
- +QString::number((ui->horizontalSlider->maximum()/60)%60)+":"
- +QString::number(ui->horizontalSlider->maximum()%60));
- //ui->label_5->setText(QString::number(ui->horizontalSlider->value()/3600)+":"
- +QString::number((ui->horizontalSlider->value()/60)%60)+":"
- +QString::number(ui->horizontalSlider->value()%60));
- while(mplayerProcess->canReadLine())
- {
- QByteArraybuffer(mplayerProcess->readLine());
- ui->textEdit_3->append(QString(buffer));
- //qDebug()<
- //log->append(QString(buffer));
- //Onvérifiesionaeudesréponses
- //réponseàget_video_resolution:ANS_VIDEO_RESOLUTION='x'
- if(buffer.startsWith("ANS_VIDEO_RESOLUTION"))
- {
- buffer.remove(0,21);//vireANS_VIDEO_RESOLUTION=
- buffer.replace(QByteArray("'"),QByteArray(""));
- buffer.replace(QByteArray(""),QByteArray(""));
- buffer.replace(QByteArray("/n"),QByteArray(""));
- buffer.replace(QByteArray("/r"),QByteArray(""));
- intsepIndex=buffer.indexOf('x');
- intresX=buffer.left(sepIndex).toInt();
- intresY=buffer.mid(sepIndex+1).toInt();
- ui->widget->setMinimumSize(resX,resY);
- }
- //réponseàget_time_length:ANS_LENGTH=xx.yy
- elseif(buffer.startsWith("ANS_LENGTH"))
- {
- buffer.remove(0,11);//vireANS_LENGTH=
- buffer.replace(QByteArray("'"),QByteArray(""));
- buffer.replace(QByteArray(""),QByteArray(""));
- buffer.replace(QByteArray("/n"),QByteArray(""));
- buffer.replace(QByteArray("/r"),QByteArray(""));
- floatmaxTime=buffer.toFloat();
- ui->horizontalSlider->setMaximum(static_cast(maxTime+1));
- }
- //réponseàget_time_pos:ANS_TIME_POSITION=xx.y
- elseif(buffer.startsWith("ANS_TIME_POSITION"))
- {
- buffer.remove(0,18);//vireANS_TIME_POSITION=
- buffer.replace(QByteArray("'"),QByteArray(""));
- buffer.replace(QByteArray(""),QByteArray(""));
- buffer.replace(QByteArray("/n"),QByteArray(""));
- buffer.replace(QByteArray("/r"),QByteArray(""));
- floatcurrTime=buffer.toFloat();
- ui->horizontalSlider->setValue(static_cast(currTime+1));
- }
- }
- }
- voidMainWindow::timeLineChanged(intpos)
- {
- mplayerProcess->write(QString("seek"+QString::number(pos)+"2/n").toUtf8());
- }
- voidMainWindow::on_pushButton_4_clicked()
- {
- filename=QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("文件类型"),".",QString::fromLocal8Bit("文问类型(*.*)"));
- if(filename!=NULL)
- {
- poller->stop();
- mplayerProcess->close();
- this->setWindowTitle(filename);
- ui->pushButton_7->setEnabled(true);
- mplayerProcess=newQProcess(this);
- QStringexefile=QCoreApplication::applicationDirPath()+"/mplayer/mplayer.exe";
- qDebug()<
- //qDebug()<
- QStringListargs;
- args<<"-slave";
- //Etonveutnepasavoirtropdechoseparser:)
- args<<"-zoom";
- //args<<"-quiet";
- //args<<"-xy1024";
- //args<<"-wid"<(w->winId()))<<"-x"<<"1680"<<"-y"<<"1050";
- args<<"-wid"<(w->winId()));
- args<<"-vo"<<"direct3d";
- //args<<"-vo"<<"gl";
- //vo=gl
- //direct3d
- //mplayer-fs-zoom-vofbdev-xy1024filename
- args<
- mplayerProcess->setProcessChannelMode(QProcess::MergedChannels);
- mplayerProcess->start(exefile,args);
- if(!mplayerProcess->waitForStarted(3000))
- {
- qDebug("allez,cherchelebug:o");
- }
- //Onrécupèrelesinfosdebase
- mplayerProcess->write("get_video_resolution/n");
- mplayerProcess->write("get_time_length/n");
- mplayerProcess->write("get_time_length/n");
- //mplayerProcess->write("get_time_pos/n");
- poller->start(10);
- //mplayerProcess->write("pause/n");
- ui->label_6->text()=="开始";
- }
- }
- voidMainWindow::on_pushButton_7_clicked()
- {
- if(ui->label_6->text()==QString::fromLocal8Bit("开始"))
- {
- mplayerProcess->write("pause/n");
- poller->stop();
- ui->label_6->setText(QString::fromLocal8Bit("暂停"));
- }
- elseif(ui->label_6->text()==QString::fromLocal8Bit("暂停"))
- {
- mplayerProcess->write("pause/n");
- mplayerProcess->write("get_time_length/n");
- poller->start(10);
- ui->label_6->setText(QString::fromLocal8Bit("开始"));
- }
- }
- voidMainWindow::on_pushButton_clicked()
- {
- mplayerProcess->write("vo_fullscreen/n");
- }
- voidMainWindow::on_pushButton_8_clicked()
- {
- QMessageBox::information(this,QString::fromLocal8Bit("AboutMdieaPlayer"),
- QString::fromLocal8Bit("快捷键说明:/n打开:A/n暂停/开始:space/n全屏:D/n取消全屏:S/n快进:
- right/n后退:left/n音量提高:top/n音量降低:down/nID:Jammy基于mplayer开发版本0.06"));
- }
- voidMainWindow::on_horizontalSlider_2_valueChanged(intvalue)
- {
- inti;
- QStringstr;QByteArraystr1;
- mplayerProcess->write("volume"+str1+"/n");
- i=-(40-value);str=QString::number(i);
- strstr1=str.toAscii();
- mplayerProcess->write("volume"+str1+"/n");
- //qDebug()<
- }
- voidMainWindow::on_pushButton_2_clicked()
- {
- mplayerProcess->write("volume+1/n");
- }
- voidMainWindow::on_pushButton_3_clicked()
- {
- mplayerProcess->write("volume-1/n");
- }
- voidMainWindow::on_pushButton_5_clicked()
- {
- this->setWindowFlags(Qt::Window);
- w->setWindowFlags(Qt::FramelessWindowHint);
- this->showFullScreen();
- shapesDockWidget1->hide();
- shapesDockWidget2->hide();
- ui->label_4->setText(QString::fromLocal8Bit("全屏"));
- }
- voidMainWindow::on_pushButton_6_clicked()
- {
- this->setWindowFlags(Qt::Window);
- this->resize(800,600);
- //w->setWindowFlags(Qt::FramelessWindowHint);
- this->showNormal();
- shapesDockWidget1->show();
- shapesDockWidget2->show();
- intx=(QApplication::desktop()->width()-this->width())/2;
- inty=(QApplication::desktop()->height()-this->height())/2;
- this->move(x,y);
- ui->label_4->setText(QString::fromLocal8Bit("不是全屏"));
- }
- voidMainWindow::on_pushButton_9_clicked()
- {
- QStringstr;
- str=QString::number(ui->spinBox->value());
- mplayerProcess->write("seek+"+str.toAscii()+"/n");
- qDebug()<
- qDebug()<<"seek+"+str.toAscii()+"/n";
- }
- voidMainWindow::on_pushButton_10_clicked()
- {
- QStringstr;
- str=QString::number(ui->spinBox->value());
- mplayerProcess->write("seek-"+str.toAscii()+"/n");
- }
- voidMainWindow::on_spinBox_valueChanged(int)
- {
- w->setFocus();
- }
- voidMainWindow::on_spinBox_editingFinished()
- {
- w->setFocus();
- }
- voidMainWindow::closeEvent(QCloseEvent*ex)
- {
- //inta=QMessageBox::information(NULL,
- QString::fromLocal8Bit("对话框"),
- QString::fromLocal8Bit("是否需要退出"),
- QString::fromLocal8Bit("确定"),
- QString::fromLocal8Bit("取消"),NULL,0,-1);
- //if(a==0)
- //{
- writeSettings();
- //ex->accept();
- //}
- //elseif(a==1)
- //{
- //ex->ignore();
- //}
- }
- voidMainWindow::writeSettings()
- {
- QSettingssettings("Software","IconEditor");
- settings.beginGroup("mainWindow");
- settings.setValue("size",size());
- settings.setValue("state",saveState());
- settings.endGroup();
- }
- voidMainWindow::readSettings()
- {
- QSettingssettings("Software","IconEditor");
- settings.beginGroup("mainWindow");
- resize(settings.value("size").toSize());
- restoreState(settings.value("state").toByteArray());
- settings.endGroup();
- }
- voidMainWindow::contextMenuEvent(QContextMenuEvent*event)
- {
- shapesDockWidget1->show();
- shapesDockWidget2->show();
- //QCursorcur=this->cursor();
- menu=newQMenu(this);
- menu->addAction(at5);
- menu->addAction(at2);
- menu->addAction(at3);
- menu->addAction(at4);
- //menu->exec(cur.pos());
- menu->exec(event->globalPos());//
- //this->setContextMenuPolicy(Qt::ActionsContextMenu);
- }
- voidMainWindow::mouseMoveEvent(QMouseEvent*event)
- {
- //if(ui->label_4->text()==QString::fromLocal8Bit("不是全屏"))
- //{
- //if(event->buttons()&Qt::LeftButton){
- //move(event->globalPos()-m_dragPosition);
- ////move(event->globalPos()-m_dragPosition);
- //event->accept();
- //}
- //}
- }
- voidMainWindow::mousePressEvent(QMouseEvent*event)
- {
- //if(ui->label_4->text()==QString::fromLocal8Bit("不是全屏"))
- //{
- //if(event->button()==Qt::LeftButton){
- //m_dragPosition=event->globalPos()-frameGeometry().topLeft();
- //ui->widget->move(m_dragPosition);
- //event->accept();
- //}
- //}
- }
- boolMainWindow::eventFilter(QObject*obj,QEvent*event)
- {
- if(obj==w){
- if(event->type()==QEvent::MouseButtonDblClick){
- QMouseEvent*ev=static_cast(event);
- if(ev->buttons()&Qt::LeftButton)
- {
- if(ui->label_4->text()==QString::fromLocal8Bit("不是全屏"))
- {
- this->setWindowFlags(Qt::Window);
- w->setWindowFlags(Qt::FramelessWindowHint);
- this->showFullScreen();
- shapesDockWidget1->hide();
- shapesDockWidget2->hide();
- ui->label_4->setText(QString::fromLocal8Bit("全屏"));
- }
- elseif(ui->label_4->text()==QString::fromLocal8Bit("全屏"))
- {
- this->setWindowFlags(Qt::Window);
- this->resize(800,600);
- //w->setWindowFlags(Qt::FramelessWindowHint);
- this->showNormal();
- shapesDockWidget1->show();
- shapesDockWidget2->show();
- intx=(QApplication::desktop()->width()-this->width())/2;
- inty=(QApplication::desktop()->height()-this->height())/2;
- this->move(x,y);
- ui->label_4->setText(QString::fromLocal8Bit("不是全屏"));
- }
- //m_dragPosition=ev->globalPos()-frameGeometry().topLeft();
- //ev->accept();
- }
- returntrue;
- }
- elseif(event->type()==QEvent::MouseButtonPress){
- QMouseEvent*ev=static_cast(event);
- if(ev->buttons()&Qt::LeftButton)
- {
- if(ui->label_4->text()==QString::fromLocal8Bit("全屏"))
- {
- shapesDockWidget1->hide();
- shapesDockWidget2->hide();
- }
- elseif(ui->label_4->text()==QString::fromLocal8Bit("不是全屏"))
- {
- m_dragPosition=ev->globalPos()-frameGeometry().topLeft();
- ev->accept();
- w->setFocus();
- }
- //m_dragPosition=ev->globalPos()-frameGeometry().topLeft();
- //ev->accept();
- }
- returntrue;
- }elseif(event->type()==QEvent::MouseMove){
- QMouseEvent*ev=static_cast(event);
- if(ev->buttons()&Qt::LeftButton){
- if(ui->label_4->text()==QString::fromLocal8Bit("不是全屏"))
- {
- //move(event->globalPos()-m_dragPosition);
- move(ev->globalPos()-m_dragPosition);
- ev->accept();
- w->setFocus();
- }
- }
- }
- else{
- returnfalse;
- }
- }
- else{
- //passtheeventontotheparentclass
- returnQMainWindow::eventFilter(obj,event);
- }
- returntrue;
- }
- voidMainWindow::on_textEdit_3_textChanged()
- {
- w->setFocus();
- }
- **************************************************
- main.cpp
- #include
- #include"mainwindow.h"
- intmain(intargc,char*argv[])
- {
- QApplicationa(argc,argv);
- MainWindoww;
- w.show();
- returna.exec();
- }
小结:详解QT基于Mplayer媒体播放器应用教程的内容介绍完了,希望通过学习QT播放器的案例的实现,能对你有所帮助。更多关于QT的相关内容,请单击QT开发频道。