PDA

View Full Version : ساخت یک media player پیشرفته در qt نسخه ی 4.5.2



QTdesigner
جمعه 27 شهریور 1388, 14:10 عصر
این برنامه رو خودم نوشتم که به یه source در درایو c احتیاج داره البته می تونید تغییرش بدید تو برنامه کاملا
واضح

.h اول

#ifndef form_h__
#define form_h__
#include"GeneralHeader.h"
#include "ui_settings.h"
#include"ui_Ui.h"
namespace Ui
{
class Form ;
class settings;

} //end namespace



//////////////////////////////////////////////////////////////////////////

class MyVideoPlayer;

class form : public QWidget
{
Q_OBJECT
public :
form() ;
~form() ;
Ui::Form *p ;
QTimer *myTimer;
QMenu *fileMenu;
MyVideoPlayer* videoPlayer;

void Load(void);
void ShowTime(void);
void Connects(void);
void CreateMenuBar(void);
void CreateButton(void);
void shortcutKey(void);
protected:
void contextMenuEvent(QContextMenuEvent *event);



private:
QMenu *aspectMenu;
QActionGroup *aspectGroup;
QAction *aspectActionAuto;
QAction *aspectActionScale;
QAction *aspectAction16_9 ;
QAction *aspectAction4_3;
QMenu *scaleMenu;
QActionGroup *scaleGroup;
QAction *scaleActionFit;
QAction *scaleActionCrop;
QAction *settingsAction;


public slots :
void Play(void);
void Stop(void);
void Forward(void);
void Backward(void);
void updateTime(void);


void Repeat (void);
void scaleChanged(QAction *);
void aspectChanged(QAction *);
void setBrightness(int val);
void setSaturation(int val);
void setHue(int val);
void setAspect(int val);
void setScale(int val);
void setContrast(int val);
void showSettingsDialog(void);




};


//-----------------------------------------------------------------------------------------

class MyVideoPlayer : public Phonon::VideoWidget
{
public:
Phonon::MediaObject *mediaObject;
Phonon::AudioOutput *audioOutput;
Phonon::Path path;

MyVideoPlayer(QString File,form *player, QWidget *parent = 0) : Phonon::VideoWidget(parent), m_player(player)
{
m_action = new QAction(this);
m_action->setCheckable(true);
m_action->setChecked(false);
m_action->setShortcut(QKeySequence( Qt::AltModifier + Qt::Key_Return));
m_action->setShortcutContext(Qt::WindowShortcut);
connect(m_action, SIGNAL(toggled(bool)), SLOT(setFullScreen(bool)));
addAction(m_action);
setAcceptDrops(true);
mediaObject = new Phonon::MediaObject(parent);
mediaObject->setCurrentSource(Phonon::MediaSource(File));
audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
path = Phonon::createPath(mediaObject, audioOutput);
path = Phonon::createPath(mediaObject, this);
}

~MyVideoPlayer()
{
delete mediaObject;
delete audioOutput;
}

void mouseDoubleClickEvent(QMouseEvent *e)
{
Phonon::VideoWidget::mouseDoubleClickEvent(e);
setFullScreen(!isFullScreen());
}

void keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Space && !e->modifiers())
{
if (mediaObject->state() == Phonon::PlayingState)
mediaObject->pause();
else {
mediaObject->play();
}
e->accept();
}
else if (e->key() == Qt::Key_Escape && !e->modifiers()) {
setFullScreen(false);
e->accept();
}
Phonon::VideoWidget::keyPressEvent(e);
}

//////////////////////////////////////////////////////////////////////////new

bool event(QEvent *e)
{
switch(e->type())
{
case QEvent::Close:
//we just ignore the cose events on the video widget
//this prevents ALT+F4 from having an effect in fullscreen mode
e->ignore();
return true;
case QEvent::MouseMove:

unsetCursor();

//fall through
case QEvent::WindowStateChange:
{
//we just update the state of the checkbox, in case it wasn't already
m_action->setChecked(windowState() & Qt::WindowFullScreen);
//const Qt::WindowFlags flags = m_player->windowFlags();
if (windowState() & Qt::WindowFullScreen) {
m_timer.start(1000, this);
} else {
m_timer.stop();

unsetCursor();

}
}
break;
default:
break;
}

return Phonon::VideoWidget::event(e);
}

void timerEvent(QTimerEvent *e)
{

if (e->timerId() == m_timer.timerId()) {
//let's store the cursor shape

setCursor(Qt::BlankCursor);
}

Phonon::VideoWidget::timerEvent(e);
}

//////////////////////////////////////////////////////////////////////////new

MyVideoPlayer(QWidget* Parent){setParent(Parent);}


private: // new added

form *m_player;
QBasicTimer m_timer;
QAction *m_action;
};
#endif //form_h;

.h دوم

#ifndef setting_h__
#define setting_h__
#pragma once
#include "ui_settings.h"
#include "GeneralHeader.h"

class Ui::settings ;

class settingDialog : public QDialog
{

public:
settingDialog (void);
~settingDialog (void);
Ui::settings *p2 ;
};
#endif


. cpp اول

#include"form.h"
#include "setting.h"
#define SLIDER_RANGE 8

//////////////////////////////////////////////////////////////////////////

form::form()
{
p = new Ui::Form ;
myTimer = new QTimer(this);
videoPlayer = new MyVideoPlayer("c:\\Sarboland.wmv",this);
videoPlayer->setObjectName(QString::fromUtf8("videoPlayer"));
videoPlayer->setMaximumSize(QSize(16777215, 16777215));

p->setupUi(this) ;
p->verticalLayout_2->removeWidget(p->videoPlayer);
delete p->videoPlayer;

p->verticalLayout_2->insertWidget(0,videoPlayer);

p->lcdNumber->display("00:00:00");

Connects(); //connect`s class
shortcutKey(); //key`s button
//CreateButton(); //icon`s button
Load() ;
CreateMenuBar(); //new state function

p->settingbtn->setIcon(style()->standardIcon(QStyle::SP_FileDialogDetailedView));
p->settingbtn->setIconSize(QSize(25,25));


}

//////////////////////////////////////////////////////////////////////////

form::~form()
{
delete videoPlayer;
delete p ;
}

//////////////////////////////////////////////////////////////////////////

void form::Repeat()
{
videoPlayer->mediaObject->seek(0);
videoPlayer->mediaObject->play();
}

//////////////////////////////////////////////////////////////////////////

void form::Play()
{
if(!p->play->isChecked())
{
videoPlayer->mediaObject->pause();
QIcon Play ;
Play.addFile(QString::fromUtf8("play-128x128.png"), QSize(), QIcon::Normal, QIcon::Off);
p->play->setIcon(Play);
p->play->setIconSize(QSize(36,36));

}
else
{
videoPlayer->mediaObject->play();
QIcon Puse ;
Puse.addFile(QString::fromUtf8("pause-128x128.png"), QSize(), QIcon::Normal, QIcon::Off);
p->play->setIcon(Puse);
p->play->setIconSize(QSize(36,36));
}
p->volumeSlider->setAudioOutput(videoPlayer->audioOutput);
myTimer->start(1000);
}

//////////////////////////////////////////////////////////////////////////

void form::Load(void)
{
p->seekSlider->setMediaObject(videoPlayer->mediaObject);
}

//////////////////////////////////////////////////////////////////////////

void form::Stop(void)
{
myTimer->stop();
videoPlayer->mediaObject->stop();
if(p->play->isChecked())
videoPlayer->mediaObject->play();
}

//////////////////////////////////////////////////////////////////////////

void form::Forward()
{
videoPlayer->mediaObject->seek(videoPlayer->mediaObject->currentTime()+5000);

}

//////////////////////////////////////////////////////////////////////////

void form::Backward()
{

videoPlayer->mediaObject->seek(videoPlayer->mediaObject->currentTime()-5000);

}

//////////////////////////////////////////////////////////////////////////

void form::updateTime()
{

qint64 time = videoPlayer->mediaObject->currentTime();
QTime TIME(time/3600000,(time/1000)/60,(time/1000)%60);
p->lcdNumber->display(TIME.toString("hh:mm:ss"));

}

//////////////////////////////////////////////////////////////////////////

void form::shortcutKey()
{

connect(p->forward, SIGNAL(triggered()), this, SLOT(Forward()));
p->forward->setShortcut(tr("alt+right"));
connect(p->backward, SIGNAL(triggered()), this, SLOT(Backward()));
p->backward->setShortcut(tr("alt+left"));
connect(p->play ,SIGNAL(triggered()) , this , SLOT(Play()));
p->play->setShortcut(tr("space"));
connect(p->stop , SIGNAL(triggered()),this , SLOT(Stop()));
p->stop->setShortcut(tr("ctrl+s"));
connect(p->Reapet , SIGNAL(triggered()),this , SLOT(Full()));
p->Reapet->setShortcut(tr("ctrl+f"));

}

//////////////////////////////////////////////////////////////////////////


void form::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu ;
menu.addAction(aspectActionAuto);
menu.addAction(aspectActionScale);
menu.addAction(aspectAction16_9);
menu.addAction(aspectAction4_3);
menu.addSeparator();
menu.addAction(scaleActionFit);
menu.addAction(scaleActionCrop);
menu.addSeparator();
menu.addAction(settingsAction);
menu.exec(event->globalPos());



}

//////////////////////////////////////////////////////////////////////////

void form::Connects(void)
{

connect(p->play , SIGNAL(clicked()) , this , SLOT(Play()));
connect(p->stop , SIGNAL(clicked()) , this , SLOT(Stop()));
connect(p->forward , SIGNAL(clicked()) , this , SLOT(Forward()));
connect(p->backward , SIGNAL(clicked()) , this , SLOT(Backward()));
connect( p->Reapet , SIGNAL(clicked()) , this , SLOT(Repeat()) );
connect(myTimer , SIGNAL(timeout()), this, SLOT(updateTime()));

}

//////////////////////////////////////////////////////////////////////////

void form::CreateMenuBar()
{


//Create menu bar:
fileMenu = new QMenu(this);
fileMenu->addSeparator();
aspectMenu = fileMenu->addMenu(tr("&Aspect ratio"));
aspectGroup = new QActionGroup(aspectMenu);
connect(aspectGroup, SIGNAL(triggered(QAction *)), this, SLOT(aspectChanged(QAction *)));
aspectGroup->setExclusive(true);
aspectActionAuto = aspectMenu->addAction(tr("Auto"));
aspectActionAuto->setCheckable(true);
aspectActionAuto->setChecked(true);
aspectGroup->addAction(aspectActionAuto);
aspectActionScale = aspectMenu->addAction(tr("Scale"));
aspectActionScale->setCheckable(true);
aspectGroup->addAction(aspectActionScale);
aspectAction16_9 = aspectMenu->addAction(tr("16/9"));
aspectAction16_9->setCheckable(true);
aspectGroup->addAction(aspectAction16_9);
aspectAction4_3 = aspectMenu->addAction(tr("4/3"));
aspectAction4_3->setCheckable(true);
aspectGroup->addAction(aspectAction4_3);

scaleMenu = fileMenu->addMenu(tr("&Scale mode"));
scaleGroup = new QActionGroup(scaleMenu);
connect(scaleGroup, SIGNAL(triggered(QAction *)), this, SLOT(scaleChanged(QAction *)));
scaleGroup->setExclusive(true);
scaleActionFit = scaleMenu->addAction(tr("Fit in view"));
scaleActionFit->setCheckable(true);
scaleActionFit->setChecked(true);
scaleGroup->addAction(scaleActionFit);
scaleActionCrop = scaleMenu->addAction(tr("Scale and crop"));
scaleActionCrop->setCheckable(true);
scaleGroup->addAction(scaleActionCrop);

fileMenu->addSeparator();
settingsAction = fileMenu->addAction(tr("&Settings..."));
p->settingbtn->setMenu(fileMenu);
connect(settingsAction, SIGNAL(triggered(bool)), this , SLOT(showSettingsDialog()));
}

//////////////////////////////////////////////////////////////////////////

void form::showSettingsDialog()
{

settingDialog * set = new settingDialog ;
set->show();

connect(set->p2->brightnessSlider, SIGNAL(valueChanged(int)), this, SLOT(setBrightness(int)));
connect(set->p2->hueSlider, SIGNAL(valueChanged(int)), this, SLOT(setHue(int)));
connect(set->p2->saturationSlider, SIGNAL(valueChanged(int)), this, SLOT(setSaturation(int)));
connect(set->p2->contrastSlider , SIGNAL(valueChanged(int)), this, SLOT(setContrast(int)));
connect(set->p2->aspectCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setAspect(int)));
connect(set->p2->scalemodeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setScale(int)));



set->p2->brightnessSlider->setValue( int(videoPlayer->brightness() * SLIDER_RANGE ) );
set->p2->hueSlider->setValue(int(videoPlayer->hue() * SLIDER_RANGE));
set->p2->saturationSlider->setValue(int(videoPlayer->saturation() * SLIDER_RANGE));
set->p2->contrastSlider->setValue(int(videoPlayer->contrast() * SLIDER_RANGE));
set->p2->aspectCombo->setCurrentIndex(videoPlayer->aspectRatio());
set->p2->scalemodeCombo->setCurrentIndex(videoPlayer->scaleMode());

}


//////////////////////////////////////////////////////////////////////////

void form::scaleChanged(QAction *act)
{
if (act->text() == tr("Scale and crop"))
videoPlayer->setScaleMode(Phonon::VideoWidget::ScaleAndCrop);
else
videoPlayer->setScaleMode(Phonon::VideoWidget::FitInView);
}

//////////////////////////////////////////////////////////////////////////

void form::aspectChanged(QAction *act)
{
if (act->text() == tr("16/9"))
videoPlayer->setAspectRatio(Phonon::VideoWidget::AspectRatio16_ 9);
else if (act->text() == tr("Scale"))
videoPlayer->setAspectRatio(Phonon::VideoWidget::AspectRatioWid get);
else if (act->text() == tr("4/3"))
videoPlayer->setAspectRatio(Phonon::VideoWidget::AspectRatio4_3 );
else
videoPlayer->setAspectRatio(Phonon::VideoWidget::AspectRatioAut o);
}
//////////////////////////////////////////////////////////////////////////


void form::setSaturation(int val)
{
videoPlayer->setSaturation(val / qreal(SLIDER_RANGE));
}

//////////////////////////////////////////////////////////////////////////

void form::setHue(int val)
{
videoPlayer->setHue(val / qreal(SLIDER_RANGE));
}

//////////////////////////////////////////////////////////////////////////

void form::setAspect(int val)
{
videoPlayer->setAspectRatio(Phonon::VideoWidget::AspectRatio(va l));
}

//////////////////////////////////////////////////////////////////////////

void form::setScale(int val)
{
videoPlayer->setScaleMode(Phonon::VideoWidget::ScaleMode(val));
}

//////////////////////////////////////////////////////////////////////////

void form::setBrightness(int val)
{
videoPlayer->setBrightness(val / qreal(SLIDER_RANGE));
}

//////////////////////////////////////////////////////////////////////////


void form::setContrast(int val)
{
videoPlayer->setContrast(val / qreal(SLIDER_RANGE));
}

//////////////////////////////////////////////////////////////////////////

cpp. دوم

#include"setting.h"
settingDialog::settingDialog()
{
p2 = new Ui::settings ;
p2->setupUi(this);
}
settingDialog::~settingDialog()
{
delete p2 ;
}