QT媒体播放器代码案例实现是本文要介绍的内容,主要是来了解并学习QT中关于播放器的应用,本文通过代码来实现QT中播放器的应用,具体内容来看详细代码。
可以全屏,解决布局问题,时间进度可以显示..代码尽量简单,基本***,注需要解码包
如果需要QT基于mplayer媒体播放器:,请参考查看。
- MAINWINDOW.cpp
- #include"mainwindow.h"
- #include"ui_mainwindow.h"
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include"QFileDialog"
- #include"qDebug"
- #include"phonon"
- #include"QLabel"
- #include
- #include
- #include
- #include"phonon/VideoWidget"
- #include"QTime"
- #include
- #include
- MainWindow::MainWindow(QWidget*parent):
- QMainWindow(parent),
- ui(newUi::MainWindow)
- {
- ui->setupUi(this);
- ui->widget->installEventFilter(this);
- media=newPhonon::MediaObject;
- //media->setCurrentSource(Phonon::MediaSource("../Puppet.mpg"));
- w1=newQWidget(this);
- w3=newQWidget(this);
- timeLcd=newQLCDNumber(this);
- vwidget=newPhonon::VideoWidget(w1);
- Phonon::createPath(media,vwidget);
- vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
- aOutput=newPhonon::AudioOutput(Phonon::VideoCategory);
- Phonon::createPath(media,aOutput);
- //QLabel*l1=newQLabel(QString::fromLocal8Bit("播放进度:"));
- //QLabel*l2=newQLabel(QString::fromLocal8Bit("音量:"));
- volumeSlider=newPhonon::VolumeSlider;
- volumeSlider->setAudioOutput(aOutput);
- volumeSlider->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
- Phonon::SeekSlider*seekSlider=newPhonon::SeekSlider;
- seekSlider->setMediaObject(media);
- timeLcd->display("00:00");
- QVBoxLayout*vLayout=newQVBoxLayout;
- vLayout->addWidget(vwidget);
- vwidget->resize(1024,768);
- w1->setLayout(vLayout);
- //QHBoxLayout*h1=newQHBoxLayout;
- //h1->addWidget(l1);
- //h1->addWidget(seekSlider);
- //h1->addWidget(timeLcd);
- //QHBoxLayout*h2=newQHBoxLayout;
- //h2->addWidget(l2);
- //h2->addWidget(volumeSlider);
- //h2->addWidget(ui->widget_4);
- //QVBoxLayout*vLayout1=newQVBoxLayout;
- //vLayout1->addLayout(h1);
- //vLayout1->addLayout(h2);
- //w3->setLayout(vLayout1);
- //QHBoxLayout*hLayout1=newQHBoxLayout;
- //hLayout1->addWidget(w3);
- //QVBoxLayout*vLayout3=newQVBoxLayout;
- //vLayout3->addWidget(w1);
- //vLayout3->addLayout(hLayout1);
- //vLayout3->addWidget(ui->widget_4);
- //w2=newQWidget(this);
- //w2->setLayout(vLayout3);
- this->setCentralWidget(w1);
- QDockWidget*shapesDockWidget1=newQDockWidget(QString::fromLocal8Bit("播放进度"));
- shapesDockWidget1->setWidget(seekSlider);
- //shapesDockWidget1->setLayout(h2);
- //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea
- //|Qt::RightDockWidgetArea);
- addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget1);
- QDockWidget*shapesDockWidget2=newQDockWidget(QString::fromLocal8Bit("播放时间"));
- shapesDockWidget2->setWidget(timeLcd);
- shapesDockWidget2->setMaximumSize(100,100);
- //shapesDockWidget1->setLayout(h2);
- //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea
- //|Qt::RightDockWidgetArea);
- addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget2);
- QDockWidget*shapesDockWidget3=newQDockWidget(QString::fromLocal8Bit("播放界面"));
- shapesDockWidget3->setWidget(ui->widget_4);
- //shapesDockWidget1->setLayout(h2);
- //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea
- //|Qt::RightDockWidgetArea);
- //addDockWidget(Qt::RightDockWidgetArea,shapesDockWidget3);
- addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget3);
- QDockWidget*shapesDockWidget=newQDockWidget(QString::fromLocal8Bit("音量"));
- shapesDockWidget->setWidget(volumeSlider);
- //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea
- //|Qt::RightDockWidgetArea);
- addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget);
- ui->pushButton_5->setEnabled(false);
- ui->pushButton_7->setEnabled(false);
- //QAction*at=newQAction("退出全屏",vwidget);
- //vwidget->addAction(at);
- //vwidget->setAcceptDrops(true);
- //at->setShortcuts(QList()<
- //connect(at,SIGNAL(triggered()),vwidget,SLOT(exitFullScreen()));
- QAction*at1=newQAction("退出全屏",vwidget);
- vwidget->addAction(at1);
- vwidget->setAcceptDrops(true);
- at1->setShortcuts(QList()<
- connect(at1,SIGNAL(triggered()),vwidget,SLOT(exitFullScreen()));
- connect(media,SIGNAL(tick(qint64)),this,SLOT(tick(qint64)));
- readSettings();
- }
- MainWindow::~MainWindow()
- {
- deleteui;
- }
- voidMainWindow::resizeEvent(QResizeEvent*)
- {
- //QBitmapmaskBitmap(width(),height());
- //maskBitmap.clear();
- //QPainterp(&maskBitmap);
- //p.setBrush(Qt::black);
- //p.drawRoundRect(0,0,width(),height(),2,4);
- //p.end();
- //setMask(maskBitmap);
- }
- voidMainWindow::mouseMoveEvent(QMouseEvent*event)
- {
- //if(event->buttons()&Qt::LeftButton){
- ////move(event->globalPos()-m_dragPosition);
- //move(event->globalPos()-m_dragPosition);
- //event->accept();
- //}
- }
- voidMainWindow::mousePressEvent(QMouseEvent*event)
- {
- //if(event->button()==Qt::LeftButton){
- //m_dragPosition=event->globalPos()-frameGeometry().topLeft();
- ////m_dragPosition=event->globalPos()-frameGeometry().topLeft();
- //////ui->widget->move(m_dragPosition);
- //event->accept();
- //}
- }
- voidMainWindow::on_pushButton_2_clicked()
- {
- this->close();
- }
- voidMainWindow::on_pushButton_3_clicked()
- {
- this->showMinimized();
- }
- boolMainWindow::eventFilter(QObject*obj,QEvent*event)
- {
- if(obj==ui->widget){
- if(event->type()==QEvent::MouseButtonPress){
- QMouseEvent*ev=static_cast(event);
- if(ev->buttons()&Qt::LeftButton)
- {
- m_dragPosition=ev->globalPos()-frameGeometry().topLeft();
- ev->accept();
- }
- returntrue;
- }elseif(event->type()==QEvent::MouseMove){
- QMouseEvent*ev=static_cast(event);
- if(ev->buttons()&Qt::LeftButton){
- //move(event->globalPos()-m_dragPosition);
- move(ev->globalPos()-m_dragPosition);
- ev->accept();
- }
- }
- else{
- returnfalse;
- }
- }
- elseif(obj==w1)
- {
- if(event->type()==QEvent::MouseButtonPress){
- QMouseEvent*ev=static_cast(event);
- if(ev->buttons()&Qt::LeftButton)
- {
- vwidget=newPhonon::VideoWidget(this);
- vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
- vwidget->exitFullScreen();
- }
- }
- }
- else{
- //passtheeventontotheparentclass
- returnQMainWindow::eventFilter(obj,event);
- }
- returntrue;
- }
- voidMainWindow::on_pushButton_4_clicked()
- {
- QStringfilename=QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("文件类型"),".",
- QString::fromLocal8Bit("文问类型(*.*)"));
- qDebug()<
- media->setCurrentSource(Phonon::MediaSource(filename));
- media->play();
- this->setWindowTitle(filename);
- ui->pushButton_5->setEnabled(false);
- ui->pushButton_7->setEnabled(true);
- w1->setFocus();
- //this->ui->label->setText(filename);
- }
- voidMainWindow::on_pushButton_8_clicked()
- {
- QMessageBox::information(this,tr("AboutMdieaPlayer"),
- tr("ID:Jammy0.03"));
- }
- voidMainWindow::on_pushButton_clicked()
- {
- vwidget->enterFullScreen();
- }
- voidMainWindow::on_pushButton_5_clicked()
- {
- media->play();
- ui->pushButton_5->setEnabled(false);
- ui->pushButton_7->setEnabled(true);
- }
- voidMainWindow::on_pushButton_7_clicked()
- {
- media->pause();
- ui->pushButton_7->setEnabled(false);
- ui->pushButton_5->setEnabled(true);
- }
- voidMainWindow::on_pushButton_6_clicked()
- {
- //media->setCurrentSource(Phonon::MediaSource(this->ui->label->text()));
- media->stop();
- }
- voidMainWindow::tick(qint64time)
- {
- QTimedisplayTime(0,(time/60000)%60,(time/1000)%60);
- timeLcd->display(displayTime.toString("mm:ss"));
- }
- voidMainWindow::keyPressEvent(QKeyEvent*k)
- {
- if(k->key()==Qt::Key_Space)
- {
- if(ui->pushButton_5->isEnabled()==true)
- {
- media->pause();
- ui->pushButton_5->setEnabled(false);
- ui->pushButton_7->setEnabled(true);
- }
- elseif(ui->pushButton_5->isEnabled()==false)
- {
- media->play();
- ui->pushButton_5->setEnabled(true);
- ui->pushButton_7->setEnabled(false);
- }
- }
- elseif(k->modifiers()==Qt::AltModifier&&k->key()==Qt::Key_Return)
- {
- vwidget->enterFullScreen();
- }
- //elseif(k->modifiers()==Qt::ShiftModifier&&k->key()==Qt::Key_A)
- //{
- //vwidget->enterFullScreen();
- //}
- }
- voidMainWindow::writeSettings()
- {
- QSettingssettings("SoftwareInc.","IconEditor");
- settings.beginGroup("mainWindow");
- settings.setValue("size",size());
- settings.setValue("state",saveState());
- settings.endGroup();
- }
- voidMainWindow::readSettings()
- {
- QSettingssettings("SoftwareInc.","IconEditor");
- settings.beginGroup("mainWindow");
- resize(settings.value("size").toSize());
- restoreState(settings.value("state").toByteArray());
- settings.endGroup();
- }
- 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();
- }
- }
- ****************************************************
- MAINWINDOW.H
- #defineMAINWINDOW_H
- #include
- #include
- #include
- #include
- namespaceUi{
- classMainWindow;
- }
- classMainWindow:publicQMainWindow
- {
- Q_OBJECT
- public:
- explicitMainWindow(QWidget*parent=0);
- ~MainWindow();
- protected:
- Phonon::MediaObject*media;
- Phonon::VideoWidget*vwidget;
- Phonon::AudioOutput*aOutput;
- Phonon::VolumeSlider*volumeSlider;
- Phonon::SeekSlider*seekSlider;
- private:
- Ui::MainWindow*ui;
- QPointm_dragPosition;
- protected:
- voidresizeEvent(QResizeEvent*event);
- voidmouseMoveEvent(QMouseEvent*event);
- voidmousePressEvent(QMouseEvent*event);
- booleventFilter(QObject*obj,QEvent*ev);
- QWidget*w1;
- QWidget*w2;
- QWidget*w3;
- QLCDNumber*timeLcd;
- privateslots:
- voidon_pushButton_6_clicked();
- voidon_pushButton_7_clicked();
- voidon_pushButton_5_clicked();
- voidon_pushButton_clicked();
- voidon_pushButton_8_clicked();
- voidon_pushButton_4_clicked();
- voidon_pushButton_3_clicked();
- voidon_pushButton_2_clicked();
- voidtick(qint64time);
- voidkeyPressEvent(QKeyEvent*k);
- voidwriteSettings();
- voidreadSettings();
- voidcloseEvent(QCloseEvent*ex);
- };
- #endif//MAINWINDOW_H
小结:详解QT媒体播放器代码案例实现的内容介绍完了,通过QT中播放器的代码实现内容,希望本篇QT播放器的内容能对你有所帮助!