Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "VideoMain.h"
- #include "ui_VideoMain.h"
- #include "SwitchWidget.h"
- #include "VideoLibrary.h"
- // MW
- #include <utils/log.h>
- #include <MovieLibrary.h>
- #include <core.h>
- #include "AppUtils.h"
- //show full screen after 5s
- #define TIMEOUT_AUTOSCREEN 5000
- #define TIMEOUT_AUTOPLAY 1000
- #define MIN_PLAY_TIME (1000 * 3)
- VideoMain::VideoMain(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::VideoMain),
- audioOutput(Phonon::VideoCategory),
- fullScreen(false),
- movieId(-1)
- {
- ui->setupUi(this);
- videoWidget = new Phonon::VideoWidget(this);
- fullRect = geometry();
- normalRect = ui->bodyFrame->geometry();
- //videoWidget->setGeometry(normalRect);
- videoWidget->setGeometry(fullRect);
- updateUI();
- updatePlaylist();
- mediaObject.setTickInterval(500);
- connect(ui->backButton, SIGNAL(clicked()), g_SwitchWidget, SLOT(navigateBack()));
- connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(showVideoLibrary()));
- connect(ui->playButton, SIGNAL(clicked()), this, SLOT(playPause()));
- connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(playNext()));
- connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(playPrev()));
- connect(&mediaObject, SIGNAL(metaDataChanged()), this, SLOT(updateInfo()));
- connect(&mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(updateTime(qint64)));
- connect(&mediaObject, SIGNAL(tick(qint64)), this, SLOT(updateTime(qint64)));
- connect(&mediaObject, SIGNAL(finished()), this, SLOT(finished()));
- connect(&mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
- this, SLOT(stateChanged(Phonon::State,Phonon::State)));
- connect(&mediaObject, SIGNAL(bufferStatus(int)), this, SLOT(bufferStatus(int)));
- connect(&mediaObject, SIGNAL(hasVideoChanged(bool)), this, SLOT(hasVideoChanged(bool)));
- connect(&g_notifyProxy, SIGNAL(moviePlaylistChanged()), this, SLOT(updatePlaylist()));
- // auto hide timer
- autoHideTimer.setSingleShot(true);
- autoHideTimer.setInterval(TIMEOUT_AUTOSCREEN);
- connect(&autoHideTimer, SIGNAL(timeout()), this, SLOT(autoHide()));
- // auto play next timer
- autoPlayTimer.setSingleShot(true);
- autoPlayTimer.setInterval(TIMEOUT_AUTOPLAY);
- connect(&autoPlayTimer, SIGNAL(timeout()), this, SLOT(playNext()));
- // install event filter to video widget
- videoWidget->installEventFilter(this);
- // win32 cannot run media player
- #ifdef _WIN32_WCE
- audioPath = Phonon::createPath(&mediaObject, &audioOutput);
- Phonon::createPath(&mediaObject, videoWidget);
- #endif
- }
- VideoMain::~VideoMain()
- {
- delete ui;
- }
- void VideoMain::showVideoLibrary()
- {
- g_SwitchWidget->navigateWidget<VideoLibrary>();
- }
- void VideoMain::playPause()
- {
- if (mediaObject.state() == Phonon::PlayingState)
- {
- mediaObject.pause();
- }
- else
- {
- if (movieId < 0)
- return ;
- if (mediaObject.currentTime() == mediaObject.totalTime())
- mediaObject.seek(0);
- mediaObject.play();
- }
- }
- void VideoMain::playNext()
- {
- movieId++;
- if (movieId >= (int)movieList.size())
- movieId = 0;
- if (movieId < (int)movieList.size())
- {
- QString fileName = QString::fromWCharArray(movieList[movieId].sFilename.c_str());
- mediaObject.stop();
- // wait for a while
- QTime t;
- t.start();
- while (t.elapsed() < 500)
- {
- qApp->processEvents();
- }
- mediaObject.setCurrentSource(Phonon::MediaSource(fileName));
- if (mediaObject.currentTime() == mediaObject.totalTime())
- mediaObject.seek(0);
- mediaObject.play();
- }
- }
- void VideoMain::playPrev()
- {
- if (mediaObject.currentTime() >= MIN_PLAY_TIME)
- {
- mediaObject.seek(0);
- return ;
- }
- movieId--;
- if (movieId < 0)
- movieId = movieList.size() - 1;
- if (movieId >= 0)
- {
- QString fileName = QString::fromWCharArray(movieList[movieId].sFilename.c_str());
- mediaObject.stop();
- // wait for a while
- QTime t;
- t.start();
- while (t.elapsed() < 500)
- {
- qApp->processEvents();
- }
- mediaObject.setCurrentSource(Phonon::MediaSource(fileName));
- if (mediaObject.currentTime() == mediaObject.totalTime())
- mediaObject.seek(0);
- mediaObject.play();
- }
- }
- void VideoMain::updateTime(qint64)
- {
- long len = mediaObject.totalTime();
- long pos = mediaObject.currentTime();
- 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 = "m:ss";
- if (hour > 0)
- timeFormat = "h:mm:ss";
- timeString = playTime.toString(timeFormat);
- if (len)
- timeString += " / " + stopTime.toString(timeFormat);
- // set slider
- ui->slider->setMaximum(len);
- ui->slider->setSliderPosition(pos);
- // play next movie
- if ((len - pos) < MIN_PLAY_TIME)
- {
- //playNext();
- }
- }
- ui->timeLabel->setText(timeString);
- }
- void VideoMain::finished()
- {
- //mediaObject.stop();
- #if 1
- // must stop autoplay when video is begin to play
- autoPlayTimer.start();
- #endif
- }
- void VideoMain::stateChanged(Phonon::State newstate, Phonon::State oldstate)
- {
- Q_UNUSED(oldstate);
- CLog::Log(LOGERROR, "video state %d %d\n", newstate, oldstate);
- #if 1
- if (oldstate == Phonon::LoadingState)
- {
- //QRect videoHintRect = QRect(QPoint(0, 0), videoWidget.sizeHint());
- //QRect newVideoRect = QApplication::desktop()->screenGeometry().intersected(videoHintRect);
- if (mediaObject.hasVideo())
- {
- // Flush event que so that sizeHint takes the
- // recently shown/hidden m_videoWindow into account:
- qApp->processEvents();
- //resize(sizeHint());
- }
- else
- {
- //resize(minimumSize());
- }
- }
- #endif
- switch (newstate)
- {
- case Phonon::ErrorState:
- if (mediaObject.errorType() == Phonon::FatalError)
- {
- ui->playButton->setEnabled(false);
- ui->prevButton->setEnabled(false);
- }
- else
- {
- mediaObject.pause();
- }
- CLog::Log(LOGERROR, "Phonon Mediaplayer Eror %S\n",
- mediaObject.errorString().utf16());
- break;
- case Phonon::StoppedState:
- videoWidget->hide();
- ui->headFrame->show();
- ui->bodyFrame->show();
- ui->footFrame->show();
- // Fall through
- case Phonon::PausedState:
- ui->playButton->setChecked(false);
- if (mediaObject.currentSource().type() != Phonon::MediaSource::Invalid)
- {
- ui->playButton->setEnabled(true);
- ui->prevButton->setEnabled(true);
- }
- else
- {
- ui->playButton->setEnabled(false);
- ui->prevButton->setEnabled(false);
- }
- autoHideTimer.stop();
- break;
- case Phonon::PlayingState:
- ui->playButton->setEnabled(true);
- ui->playButton->setChecked(true);
- if (mediaObject.hasVideo())
- {
- ui->bodyFrame->hide();
- ui->headFrame->raise();
- videoWidget->show();
- autoHideTimer.start();
- }
- // Fall through
- case Phonon::BufferingState:
- ui->prevButton->setEnabled(true);
- autoPlayTimer.stop();
- break;
- case Phonon::LoadingState:
- ui->prevButton->setEnabled(false);
- break;
- }
- }
- void VideoMain::hasVideoChanged(bool bHasVideo)
- {
- videoWidget->setVisible(bHasVideo);
- }
- void VideoMain::updateInfo()
- {
- int maxLength = 30;
- QString font = "<font color=#ffeeaa>";
- QString fontmono = "<font family=\"monospace,courier new\" color=#ffeeaa>";
- QMap <QString, QString> metaData = mediaObject.metaData();
- QString trackArtist = metaData.value("ARTIST");
- if (trackArtist.length() > maxLength)
- trackArtist = trackArtist.left(maxLength) + "...";
- QString trackTitle = metaData.value("TITLE");
- int trackBitrate = metaData.value("BITRATE").toInt();
- QString fileName;
- if (mediaObject.currentSource().type() == Phonon::MediaSource::Url)
- {
- fileName = mediaObject.currentSource().url().toString();
- }
- else
- {
- fileName = mediaObject.currentSource().fileName();
- fileName = fileName.right(fileName.length() - fileName.lastIndexOf('/') - 1);
- if (fileName.length() > maxLength)
- fileName = fileName.left(maxLength) + "...";
- }
- QString title;
- if (!trackTitle.isEmpty())
- {
- if (trackTitle.length() > maxLength)
- trackTitle = trackTitle.left(maxLength) + "...";
- title = "Title: " + font + trackTitle + "<br></font>";
- }
- else if (!fileName.isEmpty())
- {
- if (fileName.length() > maxLength)
- fileName = fileName.left(maxLength) + "...";
- title = font + fileName + "</font>";
- }
- QString artist;
- if (!trackArtist.isEmpty())
- artist = "Artist: " + font + trackArtist + "</font>";
- QString bitrate;
- if (trackBitrate != 0)
- bitrate = "<br>Bitrate: " + font + QString::number(trackBitrate/1000) + "kbit</font>";
- ui->titleLabel->setText(title + artist + bitrate);
- }
- void VideoMain::bufferStatus(int percent)
- {
- #if 0
- if (percent == 100)
- progressLabel->setText(QString());
- else {
- QString str = QString::fromLatin1("(%1%)").arg(percent);
- progressLabel->setText(str);
- }
- #endif
- CLog::Log(LOGINFO, "buffer status %d\n", percent);
- }
- void VideoMain::hideEvent(QHideEvent *)
- {
- mediaObject.stop();
- }
- void VideoMain::autoHide()
- {
- // auto full screen
- ui->headFrame->hide();
- ui->footFrame->hide();
- ui->bodyFrame->hide();
- //videoWidget->setGeometry(fullRect);
- fullScreen = true;
- }
- bool VideoMain::eventFilter(QObject *obj, QEvent *ev)
- {
- if (obj == videoWidget)
- {
- if (ev->type() == QEvent::MouseButtonRelease
- || ev->type() == QEvent::KeyRelease)
- {
- fullScreen = !fullScreen;
- ui->headFrame->setVisible(!fullScreen);
- ui->footFrame->setVisible(!fullScreen);
- if (fullScreen)
- {
- //videoWidget->setGeometry(fullRect);
- videoWidget->raise();
- }
- else
- {
- //videoWidget->setGeometry(normalRect);
- videoWidget->lower();
- // start auto hide timer when playing
- autoHideTimer.stop();
- if (mediaObject.state() == Phonon::PlayingState)
- {
- autoHideTimer.start();
- }
- }
- }
- }
- return false;
- }
- void VideoMain::updateUI()
- {
- videoWidget->hide();
- ui->headFrame->show();
- ui->bodyFrame->show();
- ui->footFrame->show();
- }
- void VideoMain::showEvent(QShowEvent *)
- {
- //模式切换
- PlayerSwitch(epiPlayerMovie);
- updateUI();
- }
- void VideoMain::updatePlaylist()
- {
- CMovieLibrary library;
- library.Open();
- library.GetAllMovies(movieList);
- library.Close();
- if (movieList.size() <= 0)
- return ;
- // stop first
- mediaObject.stop();
- // clear queued movie
- mediaObject.clearQueue();
- movieId = 0;
- QString fileName = QString::fromWCharArray(movieList[movieId].sFilename.c_str());
- mediaObject.setCurrentSource(Phonon::MediaSource(fileName));
- }
Add Comment
Please, Sign In to add comment