videolabel.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "videolabel.h"
  2. #include <QUrl>
  3. //#include <QCoreApplication>
  4. //#include <QThread>
  5. VideoLabel::VideoLabel(QString path, QString filename, QWidget *parent) : QLabel(parent)
  6. {
  7. m_path = path;
  8. m_filename = filename;
  9. setScaledContents(true);
  10. setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  11. setAttribute(Qt::WA_TransparentForMouseEvents);
  12. resolution_set = false;
  13. m_medialLoaded = false;
  14. playlist.setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
  15. playlist.addMedia(QUrl::fromLocalFile(m_path + m_filename));
  16. mediaPlayer.setPlaylist(&playlist);
  17. mediaPlayer.setVideoOutput(&vfp);
  18. mediaPlayer.play();
  19. // while(!vfp.hasFrame && mediaPlayer.error() != QMediaPlayer::InvalidMedia && vfp.error() == QAbstractVideoSurface::NoError)
  20. // {
  21. // QCoreApplication::processEvents();
  22. // //QThread::msleep(4);
  23. // }
  24. // if(mediaPlayer.error() != QMediaPlayer::InvalidMedia && vfp.error() == QAbstractVideoSurface::NoError)
  25. // {
  26. connect(&vfp, SIGNAL(newFrame(QImage)), this, SLOT(OnNewFrame(QImage)));
  27. m_videoError = false;
  28. }
  29. VideoLabel::~VideoLabel()
  30. {
  31. }
  32. bool VideoLabel::videoError()
  33. {
  34. if(mediaPlayer.error() != QMediaPlayer::FormatError && vfp.error() == QAbstractVideoSurface::NoError)
  35. return false;
  36. else
  37. return true;
  38. }
  39. void VideoLabel::OnNewFrame(QImage newVideoFrame)
  40. {
  41. if(mutex.tryLock())
  42. {
  43. setPixmap(QPixmap::fromImage(newVideoFrame));
  44. if(!m_medialLoaded)
  45. {
  46. m_medialLoaded = true;
  47. emit medialLoaded();
  48. }
  49. mutex.unlock();
  50. }
  51. }