#include "player.h"
#include <QtGui/QFileDialog>
#include <QtGui/QStyle>
#include <QtGui/QDesktopWidget>
#include <QTime>
#include <QtGui/QMessageBox>
//
Player::Player( QWidget * parent, Qt::WFlags f)
: QDialog(parent, f)
{
path = QCoreApplication::applicationDirPath ();
if (path.data()[path.size() - 1] != '/') path += "/";
qDebug() << "path = " << path;;
setupUi(this);
setWindowFlags(Qt::WindowMinimizeButtonHint);
audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
mediaObject = new Phonon::MediaObject(this);
Phonon::createPath(mediaObject, audioOutput);;
videoWidget = new Phonon::VideoWidget(this);
Phonon::createPath(mediaObject, videoWidget);
QHBoxLayout *videoLayout = new QHBoxLayout;
videoLayout->addWidget(videoWidget);
videoWindow->setLayout(videoLayout);
mediaObject->setTickInterval(1000);
setUI();
setActions();
timeLabel->installEventFilter(this);
volumeLabel->installEventFilter(this);
timeLeft = false;
init();
adjustWindow();
}
void Player::init()
{
index = 0;
playing = 0;
stopped = false;
playButton->setEnabled(false);
prevButton->setEnabled(false);
nextButton->setEnabled(false);
stopButton->setEnabled(false);
reloadButton->setEnabled(false);
addButton->setEnabled(false);
newLoad = true;
}
// adjust window position to center of the screen
void Player::adjustWindow()
{
QDesktopWidget *desktop = QApplication::desktop();
int screenWidth, width;
int screenHeight, height;
int x, y;
QSize windowSize;
screenWidth = desktop->width();
screenHeight = desktop->height();
windowSize = size();
width = windowSize.width();
height = windowSize.height();
x = (screenWidth - width) / 2;
y = (screenHeight - height) / 2;
y -= 50;
move (x, y);
}
// set player label text
void Player::setLabel(QString track)
{
QString filename = mediaObject->currentSource().fileName();
filename = filename.right(filename.length() - filename.lastIndexOf('/') - 1);
this->setWindowTitle("Player ~ "+filename);
}
// simulate the play button clicked
void Player::autoPlay()
{
playButton->animateClick(10);
}
// get the audio source file
Phonon::MediaSource Player::getAudio()
{
if (playing == 1)
index++;
if (playing == 2)
index--;
if ( sources.size() > 1){
if (index == 0){
prevButton->setEnabled(false);
nextButton->setEnabled(true);
}
else if (index > 0 && index < sources.size() - 1){
prevButton->setEnabled(true);
nextButton->setEnabled(true);
}
else if (index == sources.size() - 1){
prevButton->setEnabled(true);
nextButton->setEnabled(false);
}
}
return sources.at(index);
}
// actions
void Player::setActions(){
connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));
connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
connect(prevButton, SIGNAL(clicked()), this, SLOT(prev()));
connect(loadButton, SIGNAL(clicked()), this, SLOT(load()));
connect(addButton, SIGNAL(clicked()), this, SLOT(add()));
connect(reloadButton, SIGNAL(clicked()), this, SLOT(reload()));
connect(mediaObject, SIGNAL(finished()), this, SLOT(finished()));
connect(audioOutput, SIGNAL(volumeChanged ( qreal )), this, SLOT(setVolume()));
connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(updateTime()));
connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(updateTime())); ;
connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
this, SLOT(stateChanged(Phonon::State, Phonon::State)));
connect(mediaObject, SIGNAL(metaDataChanged()), this, SLOT(updateInfo()));
}
// set the player UI : buttons icons and sliders
void Player::setUI(){
#if 0
QColor *c = new QColor ( 238, 238, 157, 255 );
QPalette * p = new QPalette();
p->setColor(QPalette::ToolTipBase,*c);
QFont *f = new QFont ( "sans", 10, -1, false );
QToolTip::setFont ( *f );
QToolTip::setPalette(*p);
setWindowIcon(QIcon(path+"images/audio.png"));
#endif
playButton->setToolTip("Play");
stopButton->setToolTip("Stop");
nextButton->setToolTip("Skip forward");
prevButton->setToolTip("Skip backward");
loadButton->setToolTip("Load files");
reloadButton->setToolTip("Rewind");
seekSlider = new Phonon::SeekSlider(this);
seekSlider->setMediaObject(mediaObject);
QHBoxLayout *seekerLayout = new QHBoxLayout;
seekerLayout->addWidget(seekSlider);
playerFrame->setLayout(seekerLayout);
volumeSlider = new Phonon::VolumeSlider(this);
volumeSlider->setAudioOutput(audioOutput);
volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
QHBoxLayout *volumeLayout = new QHBoxLayout;
volumeLayout->addWidget(volumeSlider);
volumeFrame->setLayout(volumeLayout);
}
// play audio file
void Player::play()
{
if (mediaObject->state() == Phonon::PlayingState)
mediaObject->pause();
else if (mediaObject->state() == Phonon::PausedState || mediaObject->state() == Phonon::LoadingState || mediaObject->state() == Phonon::StoppedState)
mediaObject->play();
}
// show playing time (left or elapsed just click on the label to switch)
void Player::updateTime()
{
QString sign = "";
long len = mediaObject->totalTime();
long pos = mediaObject->currentTime();
if (timeLeft == true) {
pos = len -pos;
sign = "-";
}
QString timeString;
if (pos || len)
{
int sec = pos/1000;
int min = sec/60;
int hour = min/60;
int msec = pos;
QTime playTime(hour%60, min%60, sec%60, msec%1000);
sec = len / 1000;
min = sec / 60;
hour = min / 60;
msec = len;
QTime stopTime(hour%60, min%60, sec%60, msec%1000);
QString timeFormat = "hh:mm:ss";
//if (hour > 0)
//timeFormat = "hh:mm:ss";
timeString = playTime.toString(timeFormat);
if (len)
sign += timeString;
}
timeLabel->setText(sign);
}
// stop playing
void Player::stop()
{
mediaObject->stop();
updateTime();
}
// change the volume
void Player::setVolume()
{
if (audioOutput->volume () == 0.0) volumeLabel->setPixmap(QPixmap::fromImage(QImage(path+"images/mute.png")));
else volumeLabel->setPixmap(QPixmap::fromImage(QImage(path+"images/volume.png")));
}
// when the audio source is finished
void Player::finished()
{
stop();
if (index < sources.size() - 1)
next();
}
// switch between time left or elapsed one
void Player::changeSign()
{
timeLeft = !timeLeft;
}
// detect click on the time label
bool Player::eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::MouseButtonPress)
{
if (o == timeLabel)
{
changeSign();
return TRUE;
}
if (o == volumeLabel)
{
if (audioOutput->volume () == 0.0) {
audioOutput->setVolume(1.0);
volumeLabel->setPixmap(QPixmap::fromImage(QImage(path+"images/volume.png")));
}
else {
audioOutput->setVolume(0.0);
volumeLabel->setPixmap(QPixmap::fromImage(QImage(path+"images/mute.png")));
}
}
}
return QWidget::eventFilter(o, e);
}
// forward
void Player::next()
{
playing = 1;
mediaObject->setCurrentSource(getAudio());
play();
}
// backward
void Player::prev()
{
playing = 2;
mediaObject->setCurrentSource(getAudio());
play();
}
// load media files
void Player::load()
{
sources.clear();
QStringList list = QFileDialog::getOpenFileNames(this,tr("Open one or more files"),".",
tr("audios (*.mp3 *.wma *.ogg *.wave *.midi *.avi *.mpeg *.mpg *.wmv *.divx *.xvid *.mp4 *.flv *.ogv)"));
if (list.size() > 0)
{
init();
for (int i = 0; i < list.size(); i++)
{
Phonon::MediaSource source(list.at(i));
sources.insert(i,source);
}
playButton->setEnabled(true);
stopButton->setEnabled(true);
reloadButton->setEnabled(true);
addButton->setEnabled(true);
mediaObject->setCurrentSource(getAudio());
pl
- 1
- 2
- 3
- 4
- 5
- 6
前往页