ukui-screensaver/screensaver/videoplayer.cpp

130 lines
4.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "videoplayer.h"
#include <QDir>
#include <QTimer>
VideoPlayer::VideoPlayer(QWidget *parent) :
QWidget(parent)
{
player = new QMediaPlayer(this);
connect(player, &QMediaPlayer::positionChanged,
this, &VideoPlayer::updatePosition);
connect(player, &QMediaPlayer::mediaStatusChanged,
this, &VideoPlayer::mediaStatusChanged);
connect(player, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>
(&QMediaPlayer::error), this, &VideoPlayer::showError);
}
VideoPlayer::~VideoPlayer()
{
if(player != nullptr)
{
player->stop();
player->deleteLater();
player = nullptr;
}
}
void VideoPlayer::setOutput(QGraphicsVideoItem *videoWidget, int duration)
{
video_Duration = duration;
player->setVideoOutput(videoWidget);
player->setMuted(true); //视频静音
player->setPosition(1); //避免首次打开视频是黑屏
player->play();
}
void VideoPlayer::setMediaFile(QString filePath)
{
m_filePath = filePath;
player->setMedia(QMediaContent(QUrl::fromLocalFile(filePath)));
}
void VideoPlayer::updatePosition(qint64 position)
{
//因为用wps制作的视频用qmediaplayer去获取视频时长有问题会在视频最后一帧停留很长时间所以用ffmpeg获取视频时长手动循环播放视频
qDebug() << "position =" << position << "player->duration() = " << player->duration() << "duration = " << video_Duration;
if (video_Duration || player->duration()) {
if (position && position > (video_Duration > 0 ? video_Duration : player->duration())) {
player->setMedia(QMediaContent(QUrl::fromLocalFile(m_filePath)));
player->setPosition(1);
player->play();
} else if (position == 0 && player->duration()) { //有些wps做的视频播放到最后一帧会自己跳到0
player->setMedia(QMediaContent(QUrl::fromLocalFile(m_filePath)));
player->setPosition(1);
player->play();
} else if (position && position == video_Duration) {//视频当前播放帧数 = 视频总时长
player->setMedia(QMediaContent(QUrl::fromLocalFile(m_filePath)));
player->setPosition(1);
player->play();
}
}
}
void VideoPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)
{
switch (status) {
case QMediaPlayer::UnknownMediaStatus:
qDebug() << "UnknownMediaStatus!!!" ;
break;
case QMediaPlayer::NoMedia:
qDebug() << "NoMedia!!!" ;
break;
case QMediaPlayer::BufferingMedia:
qDebug() << "BufferingMedia!!!" ;
break;
case QMediaPlayer::BufferedMedia:
qDebug() << "The player has fully buffered the current media!!!" ;
break;
case QMediaPlayer::LoadingMedia:
qDebug() << "LoadingMedia!!!" ;
break;
case QMediaPlayer::StalledMedia:
qDebug() << "StalledMedia!!!" ;
break;
case QMediaPlayer::EndOfMedia:
qDebug() << "EndOfMedia!!!" ;
// player->setPosition(0);
// player->play();
break;
case QMediaPlayer::LoadedMedia:
qDebug() << "LoadedMedia!!!" ;
break;
case QMediaPlayer::InvalidMedia:
qDebug() << "InvalidMedia!!!" ;
break;
default: break;
}
}
void VideoPlayer::showError(QMediaPlayer::Error error)
{
switch (error) {
case QMediaPlayer::NoError:
qDebug() << "没有错误!" ;
break;
case QMediaPlayer::ResourceError:
qDebug() << "媒体资源无法被解析!";
break;
case QMediaPlayer::FormatError:
qDebug() << "不支持该媒体格式!";
break;
case QMediaPlayer::NetworkError:
qDebug() << "发生了一个网络错误!";
break;
case QMediaPlayer::AccessDeniedError:
qDebug() << "没有播放权限!";
break;
case QMediaPlayer::ServiceMissingError:
qDebug() << "没有发现有效的播放服务!";
break;
default: break;
}
}