Guest User

Untitled

a guest
Jul 17th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.27 KB | None | 0 0
  1. #include "VideoMain.h"
  2. #include "ui_VideoMain.h"
  3.  
  4. #include "SwitchWidget.h"
  5. #include "VideoLibrary.h"
  6.  
  7. // MW
  8. #include <utils/log.h>
  9. #include <MovieLibrary.h>
  10. #include <core.h>
  11.  
  12. #include "AppUtils.h"
  13.  
  14. //show full screen after 5s
  15. #define TIMEOUT_AUTOSCREEN  5000
  16. #define TIMEOUT_AUTOPLAY    1000
  17. #define MIN_PLAY_TIME (1000 * 3)
  18.  
  19.  
  20.  
  21. VideoMain::VideoMain(QWidget *parent) :
  22.     QWidget(parent),
  23.     ui(new Ui::VideoMain),
  24.     audioOutput(Phonon::VideoCategory),
  25.     fullScreen(false),
  26.     movieId(-1)
  27. {
  28.     ui->setupUi(this);
  29.  
  30.     videoWidget = new Phonon::VideoWidget(this);
  31.  
  32.     fullRect = geometry();
  33.     normalRect = ui->bodyFrame->geometry();
  34.  
  35.     //videoWidget->setGeometry(normalRect);
  36.     videoWidget->setGeometry(fullRect);
  37.  
  38.     updateUI();
  39.  
  40.     updatePlaylist();
  41.  
  42.  
  43.     mediaObject.setTickInterval(500);
  44.  
  45.     connect(ui->backButton, SIGNAL(clicked()), g_SwitchWidget, SLOT(navigateBack()));
  46.     connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(showVideoLibrary()));
  47.  
  48.     connect(ui->playButton, SIGNAL(clicked()), this, SLOT(playPause()));
  49.     connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(playNext()));
  50.     connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(playPrev()));
  51.  
  52.     connect(&mediaObject, SIGNAL(metaDataChanged()), this, SLOT(updateInfo()));
  53.     connect(&mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(updateTime(qint64)));
  54.     connect(&mediaObject, SIGNAL(tick(qint64)), this, SLOT(updateTime(qint64)));
  55.     connect(&mediaObject, SIGNAL(finished()), this, SLOT(finished()));
  56.     connect(&mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
  57.             this, SLOT(stateChanged(Phonon::State,Phonon::State)));
  58.     connect(&mediaObject, SIGNAL(bufferStatus(int)), this, SLOT(bufferStatus(int)));
  59.     connect(&mediaObject, SIGNAL(hasVideoChanged(bool)), this, SLOT(hasVideoChanged(bool)));
  60.  
  61.     connect(&g_notifyProxy, SIGNAL(moviePlaylistChanged()), this, SLOT(updatePlaylist()));
  62.  
  63.     // auto hide timer
  64.     autoHideTimer.setSingleShot(true);
  65.     autoHideTimer.setInterval(TIMEOUT_AUTOSCREEN);
  66.     connect(&autoHideTimer, SIGNAL(timeout()), this, SLOT(autoHide()));
  67.  
  68.     // auto play next timer
  69.     autoPlayTimer.setSingleShot(true);
  70.     autoPlayTimer.setInterval(TIMEOUT_AUTOPLAY);
  71.     connect(&autoPlayTimer, SIGNAL(timeout()), this, SLOT(playNext()));
  72.  
  73.     // install event filter to video widget
  74.     videoWidget->installEventFilter(this);
  75.  
  76.     // win32 cannot run media player
  77. #ifdef _WIN32_WCE
  78.     audioPath = Phonon::createPath(&mediaObject, &audioOutput);
  79.     Phonon::createPath(&mediaObject, videoWidget);
  80. #endif
  81. }
  82.  
  83. VideoMain::~VideoMain()
  84. {
  85.     delete ui;
  86. }
  87.  
  88. void VideoMain::showVideoLibrary()
  89. {
  90.     g_SwitchWidget->navigateWidget<VideoLibrary>();
  91. }
  92.  
  93. void VideoMain::playPause()
  94. {
  95.     if (mediaObject.state() == Phonon::PlayingState)
  96.     {
  97.         mediaObject.pause();
  98.     }
  99.     else
  100.     {
  101.         if (movieId < 0)
  102.             return ;
  103.  
  104.         if (mediaObject.currentTime() == mediaObject.totalTime())
  105.             mediaObject.seek(0);
  106.  
  107.         mediaObject.play();
  108.     }
  109. }
  110.  
  111. void VideoMain::playNext()
  112. {
  113.     movieId++;
  114.  
  115.     if (movieId >= (int)movieList.size())
  116.         movieId = 0;
  117.  
  118.     if (movieId < (int)movieList.size())
  119.     {
  120.         QString fileName = QString::fromWCharArray(movieList[movieId].sFilename.c_str());
  121.  
  122.         mediaObject.stop();
  123.  
  124.         // wait for a while
  125.         QTime t;
  126.  
  127.         t.start();
  128.  
  129.         while (t.elapsed() < 500)
  130.         {
  131.             qApp->processEvents();
  132.         }
  133.  
  134.         mediaObject.setCurrentSource(Phonon::MediaSource(fileName));
  135.         if (mediaObject.currentTime() == mediaObject.totalTime())
  136.             mediaObject.seek(0);
  137.         mediaObject.play();
  138.     }
  139. }
  140.  
  141. void VideoMain::playPrev()
  142. {
  143.     if (mediaObject.currentTime() >= MIN_PLAY_TIME)
  144.     {
  145.         mediaObject.seek(0);
  146.         return ;
  147.     }
  148.  
  149.     movieId--;
  150.  
  151.     if (movieId < 0)
  152.         movieId = movieList.size() - 1;
  153.  
  154.     if (movieId >= 0)
  155.     {        
  156.         QString fileName = QString::fromWCharArray(movieList[movieId].sFilename.c_str());
  157.  
  158.         mediaObject.stop();
  159.  
  160.         // wait for a while
  161.         QTime t;
  162.  
  163.         t.start();
  164.  
  165.         while (t.elapsed() < 500)
  166.         {
  167.             qApp->processEvents();
  168.         }
  169.  
  170.         mediaObject.setCurrentSource(Phonon::MediaSource(fileName));
  171.         if (mediaObject.currentTime() == mediaObject.totalTime())
  172.             mediaObject.seek(0);
  173.         mediaObject.play();
  174.     }
  175. }
  176.  
  177. void VideoMain::updateTime(qint64)
  178. {
  179.     long len = mediaObject.totalTime();
  180.     long pos = mediaObject.currentTime();
  181.     QString timeString;
  182.     if (pos || len)
  183.     {
  184.         int sec = pos/1000;
  185.         int min = sec/60;
  186.         int hour = min/60;
  187.         int msec = pos;
  188.  
  189.         QTime playTime(hour%60, min%60, sec%60, msec%1000);
  190.         sec = len / 1000;
  191.         min = sec / 60;
  192.         hour = min / 60;
  193.         msec = len;
  194.  
  195.         QTime stopTime(hour%60, min%60, sec%60, msec%1000);
  196.         QString timeFormat = "m:ss";
  197.         if (hour > 0)
  198.             timeFormat = "h:mm:ss";
  199.         timeString = playTime.toString(timeFormat);
  200.         if (len)
  201.             timeString += " / " + stopTime.toString(timeFormat);
  202.  
  203.  
  204.         // set slider
  205.         ui->slider->setMaximum(len);
  206.         ui->slider->setSliderPosition(pos);
  207.  
  208.         // play next movie
  209.         if ((len - pos) < MIN_PLAY_TIME)
  210.         {
  211.             //playNext();
  212.         }
  213.     }
  214.     ui->timeLabel->setText(timeString);
  215. }
  216.  
  217. void VideoMain::finished()
  218. {
  219.     //mediaObject.stop();
  220.  
  221. #if 1
  222.     // must stop autoplay when video is begin to play
  223.     autoPlayTimer.start();
  224. #endif
  225. }
  226.  
  227. void VideoMain::stateChanged(Phonon::State newstate, Phonon::State oldstate)
  228. {
  229.     Q_UNUSED(oldstate);
  230.  
  231.     CLog::Log(LOGERROR, "video state %d  %d\n", newstate, oldstate);
  232.  
  233. #if 1
  234.     if (oldstate == Phonon::LoadingState)
  235.     {
  236.         //QRect videoHintRect = QRect(QPoint(0, 0), videoWidget.sizeHint());
  237.         //QRect newVideoRect = QApplication::desktop()->screenGeometry().intersected(videoHintRect);
  238.         if (mediaObject.hasVideo())
  239.         {
  240.             // Flush event que so that sizeHint takes the
  241.             // recently shown/hidden m_videoWindow into account:
  242.             qApp->processEvents();
  243.             //resize(sizeHint());
  244.         }
  245.         else
  246.         {
  247.             //resize(minimumSize());
  248.         }
  249.     }
  250. #endif
  251.  
  252.     switch (newstate)
  253.     {
  254.         case Phonon::ErrorState:
  255.             if (mediaObject.errorType() == Phonon::FatalError)
  256.             {
  257.                 ui->playButton->setEnabled(false);
  258.                 ui->prevButton->setEnabled(false);
  259.             }
  260.             else
  261.             {
  262.                 mediaObject.pause();
  263.             }
  264.             CLog::Log(LOGERROR, "Phonon Mediaplayer Eror %S\n",
  265.                       mediaObject.errorString().utf16());
  266.             break;
  267.  
  268.         case Phonon::StoppedState:
  269.             videoWidget->hide();
  270.             ui->headFrame->show();
  271.             ui->bodyFrame->show();
  272.             ui->footFrame->show();
  273.  
  274.  
  275.             // Fall through
  276.         case Phonon::PausedState:
  277.             ui->playButton->setChecked(false);
  278.             if (mediaObject.currentSource().type() != Phonon::MediaSource::Invalid)
  279.             {
  280.                 ui->playButton->setEnabled(true);
  281.                 ui->prevButton->setEnabled(true);
  282.             }
  283.             else
  284.             {
  285.                 ui->playButton->setEnabled(false);
  286.                 ui->prevButton->setEnabled(false);
  287.             }
  288.             autoHideTimer.stop();
  289.             break;
  290.         case Phonon::PlayingState:
  291.             ui->playButton->setEnabled(true);
  292.             ui->playButton->setChecked(true);
  293.             if (mediaObject.hasVideo())
  294.             {
  295.                 ui->bodyFrame->hide();
  296.                 ui->headFrame->raise();
  297.  
  298.                 videoWidget->show();
  299.  
  300.                 autoHideTimer.start();
  301.             }
  302.  
  303.             // Fall through
  304.         case Phonon::BufferingState:
  305.             ui->prevButton->setEnabled(true);
  306.             autoPlayTimer.stop();
  307.             break;
  308.         case Phonon::LoadingState:
  309.             ui->prevButton->setEnabled(false);
  310.             break;
  311.     }
  312.  
  313. }
  314.  
  315. void VideoMain::hasVideoChanged(bool bHasVideo)
  316. {
  317.     videoWidget->setVisible(bHasVideo);
  318. }
  319.  
  320. void VideoMain::updateInfo()
  321. {
  322.     int maxLength = 30;
  323.     QString font = "<font color=#ffeeaa>";
  324.     QString fontmono = "<font family=\"monospace,courier new\" color=#ffeeaa>";
  325.  
  326.     QMap <QString, QString> metaData = mediaObject.metaData();
  327.     QString trackArtist = metaData.value("ARTIST");
  328.     if (trackArtist.length() > maxLength)
  329.         trackArtist = trackArtist.left(maxLength) + "...";
  330.  
  331.     QString trackTitle = metaData.value("TITLE");
  332.     int trackBitrate = metaData.value("BITRATE").toInt();
  333.  
  334.     QString fileName;
  335.     if (mediaObject.currentSource().type() == Phonon::MediaSource::Url)
  336.     {
  337.         fileName = mediaObject.currentSource().url().toString();
  338.     }
  339.     else
  340.     {
  341.         fileName = mediaObject.currentSource().fileName();
  342.         fileName = fileName.right(fileName.length() - fileName.lastIndexOf('/') - 1);
  343.         if (fileName.length() > maxLength)
  344.             fileName = fileName.left(maxLength) + "...";
  345.     }
  346.  
  347.     QString title;    
  348.     if (!trackTitle.isEmpty())
  349.     {
  350.         if (trackTitle.length() > maxLength)
  351.             trackTitle = trackTitle.left(maxLength) + "...";
  352.         title = "Title: " + font + trackTitle + "<br></font>";
  353.     }
  354.     else if (!fileName.isEmpty())
  355.     {
  356.         if (fileName.length() > maxLength)
  357.             fileName = fileName.left(maxLength) + "...";
  358.         title = font + fileName + "</font>";
  359.     }
  360.  
  361.     QString artist;
  362.     if (!trackArtist.isEmpty())
  363.         artist = "Artist:  " + font + trackArtist + "</font>";
  364.  
  365.     QString bitrate;
  366.     if (trackBitrate != 0)
  367.         bitrate = "<br>Bitrate:  " + font + QString::number(trackBitrate/1000) + "kbit</font>";
  368.  
  369.     ui->titleLabel->setText(title + artist + bitrate);
  370. }
  371.  
  372. void VideoMain::bufferStatus(int percent)
  373. {
  374. #if 0
  375.     if (percent == 100)
  376.         progressLabel->setText(QString());
  377.     else {
  378.         QString str = QString::fromLatin1("(%1%)").arg(percent);
  379.         progressLabel->setText(str);
  380.     }
  381. #endif
  382.     CLog::Log(LOGINFO, "buffer status %d\n", percent);
  383. }
  384.  
  385. void VideoMain::hideEvent(QHideEvent *)
  386. {
  387.     mediaObject.stop();
  388. }
  389.  
  390. void VideoMain::autoHide()
  391. {
  392.  
  393.     // auto full screen
  394.     ui->headFrame->hide();
  395.     ui->footFrame->hide();
  396.     ui->bodyFrame->hide();
  397.  
  398.     //videoWidget->setGeometry(fullRect);
  399.  
  400.     fullScreen = true;
  401.  
  402. }
  403.  
  404. bool VideoMain::eventFilter(QObject *obj, QEvent *ev)
  405. {
  406.     if (obj == videoWidget)
  407.     {
  408.         if (ev->type() == QEvent::MouseButtonRelease
  409.                 || ev->type() == QEvent::KeyRelease)
  410.         {
  411.             fullScreen = !fullScreen;
  412.  
  413.             ui->headFrame->setVisible(!fullScreen);
  414.             ui->footFrame->setVisible(!fullScreen);
  415.  
  416.             if (fullScreen)
  417.             {
  418.                 //videoWidget->setGeometry(fullRect);
  419.                 videoWidget->raise();
  420.             }
  421.             else
  422.             {
  423.                 //videoWidget->setGeometry(normalRect);
  424.                 videoWidget->lower();
  425.  
  426.                 // start auto hide timer when playing
  427.                 autoHideTimer.stop();
  428.  
  429.                 if (mediaObject.state() == Phonon::PlayingState)
  430.                 {
  431.                     autoHideTimer.start();
  432.                 }
  433.             }
  434.         }
  435.     }
  436.  
  437.     return false;
  438. }
  439.  
  440. void VideoMain::updateUI()
  441. {
  442.     videoWidget->hide();
  443.  
  444.     ui->headFrame->show();
  445.     ui->bodyFrame->show();
  446.     ui->footFrame->show();
  447. }
  448.  
  449. void VideoMain::showEvent(QShowEvent *)
  450. {
  451.     //模式切换
  452.     PlayerSwitch(epiPlayerMovie);
  453.  
  454.     updateUI();
  455. }
  456.  
  457. void VideoMain::updatePlaylist()
  458. {
  459.     CMovieLibrary library;
  460.  
  461.     library.Open();
  462.     library.GetAllMovies(movieList);
  463.     library.Close();
  464.  
  465.     if (movieList.size() <= 0)
  466.         return ;
  467.  
  468.     // stop first
  469.     mediaObject.stop();
  470.  
  471.     // clear queued movie
  472.     mediaObject.clearQueue();
  473.  
  474.     movieId = 0;
  475.  
  476.     QString fileName = QString::fromWCharArray(movieList[movieId].sFilename.c_str());
  477.  
  478.     mediaObject.setCurrentSource(Phonon::MediaSource(fileName));
  479. }
Add Comment
Please, Sign In to add comment