可以全屏,解決布局問題,時間進度可以顯示..代碼盡量簡單,基本完美,注需要解碼包
如果需要QT 基於mplayer媒體播放器 :http://blog.csdn.net/buafnlin/archive/2011/02/04/6172415.aspx
MAINWINDOW.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QSettings>
#include <QtCore/QTimer>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QBitmap>
#include <QtGui/QLinearGradient>
#include <QtGui/QMouseEvent>
#include <QtGui/QPainter>
#include <QMessageBox>
#include <QKeyEvent>
#include <QSound>
#include "QFileDialog"
#include "qDebug"
#include "phonon"
#include "QLabel"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include "phonon/VideoWidget"
#include "QTime"
#include <QDockWidget>
#include <QAction>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widget->installEventFilter(this);
media = new Phonon::MediaObject;
//media->setCurrentSource(Phonon::MediaSource("../Puppet.mpg"));
w1=new QWidget(this);
w3=new QWidget(this);
timeLcd=new QLCDNumber(this);
vwidget = new Phonon::VideoWidget(w1);
Phonon::createPath(media, vwidget);
vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
aOutput = new Phonon::AudioOutput(Phonon::VideoCategory);
Phonon::createPath(media, aOutput);
// QLabel *l1 = new QLabel(QString::fromLocal8Bit("播放進度: "));
// QLabel *l2 = new QLabel(QString::fromLocal8Bit("音量: "));
volumeSlider = new Phonon::VolumeSlider;
volumeSlider->setAudioOutput(aOutput);
volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
Phonon::SeekSlider *seekSlider = new Phonon::SeekSlider;
seekSlider->setMediaObject(media);
timeLcd->display("00:00");
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(vwidget);
vwidget->resize(1024,768);
w1->setLayout(vLayout);
// QHBoxLayout *h1=new QHBoxLayout;
// h1->addWidget(l1);
// h1->addWidget(seekSlider);
// h1->addWidget(timeLcd);
// QHBoxLayout *h2=new QHBoxLayout;
// h2->addWidget(l2);
// h2->addWidget(volumeSlider);
// h2->addWidget(ui->widget_4);
// QVBoxLayout *vLayout1 = new QVBoxLayout;
// vLayout1->addLayout(h1);
// vLayout1->addLayout(h2);
// w3->setLayout(vLayout1);
// QHBoxLayout *hLayout1= new QHBoxLayout;
// hLayout1->addWidget(w3);
// QVBoxLayout *vLayout3= new QVBoxLayout;
// vLayout3->addWidget(w1);
//vLayout3->addLayout(hLayout1);
//vLayout3->addWidget(ui->widget_4);
// w2=new QWidget(this);
// w2->setLayout(vLayout3);
this->setCentralWidget(w1);
QDockWidget *shapesDockWidget1 = new QDockWidget(QString::fromLocal8Bit("播放進度"));
shapesDockWidget1->setWidget(seekSlider);
//shapesDockWidget1->setLayout(h2);
//shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea
// | Qt::RightDockWidgetArea);
addDockWidget(Qt::BottomDockWidgetArea, shapesDockWidget1);
QDockWidget *shapesDockWidget2 = new QDockWidget(QString::fromLocal8Bit("播放時間"));
shapesDockWidget2->setWidget(timeLcd);
shapesDockWidget2->setMaximumSize(100,100);
//shapesDockWidget1->setLayout(h2);
//shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea
// | Qt::RightDockWidgetArea);
addDockWidget(Qt::BottomDockWidgetArea, shapesDockWidget2);
QDockWidget *shapesDockWidget3 = new QDockWidget(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 = new QDockWidget(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 = new QAction("退出全屏", vwidget);
// vwidget->addAction(at);
// vwidget->setAcceptDrops(true);
// at->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
// connect(at, SIGNAL(triggered()), vwidget, SLOT(exitFullScreen()));
QAction *at1 = new QAction("退出全屏", vwidget);
vwidget->addAction(at1);
vwidget->setAcceptDrops(true);
at1->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL+Qt::Key_Return)<< QKeySequence(Qt::Key_MediaStop));
connect(at1, SIGNAL(triggered()), vwidget,SLOT(exitFullScreen()));
connect(media, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
readSettings();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resizeEvent(QResizeEvent*)
{
// QBitmap maskBitmap(width(), height());
// maskBitmap.clear();
// QPainter p(&maskBitmap);
// p.setBrush(Qt::black);
// p.drawRoundRect(0, 0, width(), height(), 2, 4);
// p.end();
// setMask(maskBitmap);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
// if (event->buttons() & Qt::LeftButton ) {
// // move(event->globalPos() - m_dragPosition);
// move(event->globalPos() - m_dragPosition);
// event->accept();
// }
}
void MainWindow::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();
//}
}
void MainWindow::on_pushButton_2_clicked()
{
this->close();
}
void MainWindow::on_pushButton_3_clicked()
{
this->showMinimized();
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->widget) {
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *ev = static_cast<QMouseEvent *>(event);
if (ev->buttons() & Qt::LeftButton)
{
m_dragPosition = ev->globalPos() - frameGeometry().topLeft();
ev->accept();
}
return true;
} else if (event->type() == QEvent::MouseMove){
QMouseEvent *ev = static_cast<QMouseEvent *>(event);
if (ev->buttons() & Qt::LeftButton ) {
// move(event->globalPos() - m_dragPosition);
move(ev->globalPos() - m_dragPosition);
ev->accept();
}
}
else {
return false;
}
}
else if (obj == w1)
{
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *ev = static_cast<QMouseEvent *>(event);
if (ev->buttons() & Qt::LeftButton)
{
vwidget = new Phonon::VideoWidget(this);
vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
vwidget->exitFullScreen();
}
}
}
else {
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
return true;
}
void MainWindow::on_pushButton_4_clicked()
{
QString filename=QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("檔案類型"),".",QString::fromLocal8Bit("文問類型(*.*)"));
qDebug()<<filename;
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);
}
void MainWindow::on_pushButton_8_clicked()
{
QMessageBox::information(this, tr("About Mdiea Player"),
tr("ID:Jammy 0.03"));
}
void MainWindow::on_pushButton_clicked()
{
vwidget->enterFullScreen();
}
void MainWindow::on_pushButton_5_clicked()
{
media->play();
ui->pushButton_5->setEnabled(false);
ui->pushButton_7->setEnabled(true);
}
void MainWindow::on_pushButton_7_clicked()
{
media->pause();
ui->pushButton_7->setEnabled(false);
ui->pushButton_5->setEnabled(true);
}
void MainWindow::on_pushButton_6_clicked()
{
// media->setCurrentSource(Phonon::MediaSource(this->ui->label->text()));
media->stop();
}
void MainWindow::tick(qint64 time)
{
QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
timeLcd->display(displayTime.toString("mm:ss"));
}
void MainWindow::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);
}
else if(ui->pushButton_5->isEnabled()==false)
{
media->play();
ui->pushButton_5->setEnabled(true);
ui->pushButton_7->setEnabled(false);
}
}
else if(k->modifiers() == Qt::AltModifier && k->key() == Qt::Key_Return)
{
vwidget->enterFullScreen();
}
// else if(k->modifiers() == Qt::ShiftModifier && k->key() == Qt::Key_A)
// {
// vwidget->enterFullScreen();
// }
}
void MainWindow::writeSettings()
{
QSettings settings("Software Inc.", "Icon Editor");
settings.beginGroup("mainWindow");
settings.setValue("size", size());
settings.setValue("state", saveState());
settings.endGroup();
}
void MainWindow::readSettings()
{
QSettings settings("Software Inc.", "Icon Editor");
settings.beginGroup("mainWindow");
resize(settings.value("size").toSize());
restoreState(settings.value("state").toByteArray());
settings.endGroup();
}
void MainWindow::closeEvent(QCloseEvent *ex)
{
int a= QMessageBox::information(NULL,QString::fromLocal8Bit("對話方塊"),QString::fromLocal8Bit("是否需要退出"),QString::fromLocal8Bit("確定"),QString::fromLocal8Bit("取消"),NULL,0,-1);
if(a==0)
{
writeSettings();
ex->accept();
}
else if(a==1)
{
ex->ignore();
}
}
****************************************************
MAINWINDOW.H
#define MAINWINDOW_H
#include <QMainWindow>
#include <phonon>
#include <QWidget>
#include <QLCDNumber>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
Phonon::MediaObject *media ;
Phonon::VideoWidget *vwidget;
Phonon::AudioOutput *aOutput;
Phonon::VolumeSlider *volumeSlider;
Phonon::SeekSlider *seekSlider;
private:
Ui::MainWindow *ui;
QPoint m_dragPosition;
protected:
void resizeEvent(QResizeEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
bool eventFilter(QObject *obj, QEvent *ev);
QWidget *w1;
QWidget *w2;
QWidget *w3;
QLCDNumber *timeLcd;
private slots:
void on_pushButton_6_clicked();
void on_pushButton_7_clicked();
void on_pushButton_5_clicked();
void on_pushButton_clicked();
void on_pushButton_8_clicked();
void on_pushButton_4_clicked();
void on_pushButton_3_clicked();
void on_pushButton_2_clicked();
void tick(qint64 time);
void keyPressEvent(QKeyEvent *k);
void writeSettings();
void readSettings();
void closeEvent(QCloseEvent *ex);
};
#endif // MAINWINDOW_H