Ai
1 Star 0 Fork 2

第三方项目代码/Qt5Player

forked from GongBaoDD/Qt5Player 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
player.cpp 10.05 KB
一键复制 编辑 原始数据 按行查看 历史
GongBaoDD 提交于 2014-07-22 07:57 +08:00 . 20140722
#include "player.h"
#include "playercontrols.h"
#include "playlistmodel.h"
#include "connection.h"
#include <QMediaService>
#include <QMediaPlaylist>
#include <QMediaMetaData>
#include <QMessageBox>
#include <QtWidgets>
#include <QMap>
Player::Player(QWidget *parent)
: QWidget(parent)
, videoWidget(0)
, coverLabel(0)
, slider(0)
{
center = new MediaCenter();
player = new QMediaPlayer(this);
// owned by PlaylistModel
playlist = new QMediaPlaylist();
player->setPlaylist(playlist);
connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));
connect(playlist, SIGNAL(currentIndexChanged(int)), SLOT(playlistPositionChanged(int)));
connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));
connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));
connect(player, SIGNAL(videoAvailableChanged(bool)), this, SLOT(videoAvailableChanged(bool)));
connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));
connect(center,SIGNAL(sendtoplayer()),this,SLOT(getInfFromCenter()));
connect(player,SIGNAL(metaDataChanged()),center,SLOT(initial()));
videoWidget = new VideoWidget(this);
player->setVideoOutput(videoWidget);
playlistModel = new PlaylistModel(this);
playlistModel->setPlaylist(playlist);
playlistView = new QListView(this);
playlistView->setModel(playlistModel);
playlistView->setCurrentIndex(playlistModel->index(playlist->currentIndex(), 0));
connect(playlistView, SIGNAL(activated(QModelIndex)), this, SLOT(jump(QModelIndex)));
slider = new QSlider(Qt::Horizontal, this);
slider->setRange(0, player->duration() / 1000);
labelDuration = new QLabel(this);
connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
QPushButton *openButton = new QPushButton(tr("Open"), this);
connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
PlayerControls *controls = new PlayerControls(this);
controls->setState(player->state());
controls->setVolume(player->volume());
controls->setMuted(controls->isMuted());
connect(controls, SIGNAL(play()), player, SLOT(play()));
connect(controls, SIGNAL(pause()), player, SLOT(pause()));
connect(controls, SIGNAL(stop()), player, SLOT(stop()));
connect(controls, SIGNAL(next()), playlist, SLOT(next()));
connect(controls, SIGNAL(previous()), this, SLOT(previousClicked()));
connect(controls, SIGNAL(changeVolume(int)), player, SLOT(setVolume(int)));
connect(controls, SIGNAL(changeMuting(bool)), player, SLOT(setMuted(bool)));
connect(controls, SIGNAL(changeRate(qreal)), player, SLOT(setPlaybackRate(qreal)));
connect(controls, SIGNAL(stop()), videoWidget, SLOT(update()));
connect(player, SIGNAL(stateChanged(QMediaPlayer::State)),
controls, SLOT(setState(QMediaPlayer::State)));
connect(player, SIGNAL(volumeChanged(int)), controls, SLOT(setVolume(int)));
connect(player, SIGNAL(mutedChanged(bool)), controls, SLOT(setMuted(bool)));
centerButton = new QPushButton(tr("MediaCenter"), this);
QBoxLayout *displayLayout = new QHBoxLayout;
displayLayout->addWidget(videoWidget, 2);
displayLayout->addWidget(playlistView);
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(openButton);
controlLayout->addStretch(1);
controlLayout->addWidget(controls);
controlLayout->addStretch(1);
controlLayout->addWidget(centerButton);
QBoxLayout *layout = new QVBoxLayout;
layout->addLayout(displayLayout);
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(slider);
hLayout->addWidget(labelDuration);
layout->addLayout(hLayout);
layout->addLayout(controlLayout);
setLayout(layout);
if (!player->isAvailable()) {
QMessageBox::warning(this, tr("Service not available"),
tr("The QMediaPlayer object does not have a valid service.\n"\
"Please check the media service plugins are installed."));
controls->setEnabled(false);
playlistView->setEnabled(false);
openButton->setEnabled(false);
centerButton->setEnabled(false);
}
metaDataChanged();
QStringList arguments = qApp->arguments();
arguments.removeAt(0);
addToPlaylist(arguments);
connect(centerButton,SIGNAL(clicked()),this,SLOT(mediacenter()));
connect(centerButton,SIGNAL(clicked()),center,SLOT(setUnable()));
}
Player::~Player()
{
}
void Player::open()
{
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"),"",tr("Music(*.mp3 *.wav *.wma);;Video(*.mp4 *.avi *.wmv *.rmvb *.3gp *.m4v *.flv)"));
addToPlaylist(fileNames);
}
void Player::addToPlaylist(const QStringList& fileNames)
{
foreach (QString const &argument, fileNames) {
QFileInfo fileInfo(argument);
if (fileInfo.exists()) {
QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
if (fileInfo.suffix().toLower() == QLatin1String("m3u")) {
playlist->load(url);
} else
playlist->addMedia(url);
} else {
QUrl url(argument);
if (url.isValid()) {
playlist->addMedia(url);
}
}
}
}
void Player::durationChanged(qint64 duration)
{
this->duration = duration/1000;
slider->setMaximum(duration / 1000);
}
void Player::positionChanged(qint64 progress)
{
if (!slider->isSliderDown()) {
slider->setValue(progress / 1000);
}
updateDurationInfo(progress / 1000);
}
void Player::metaDataChanged()
{
if (player->isMetaDataAvailable()) {
QMap<QString,QString> map;
map["url"] = player->currentMedia().canonicalUrl().toLocalFile();
map["musicname"]=player->metaData(QMediaMetaData::Title).toString();
map["albumname"]=player->metaData(QMediaMetaData::AlbumTitle).toString();
map["composer"]=player->metaData(QMediaMetaData::AlbumArtist).toString();
map["singer"]=player->metaData(QMediaMetaData::AlbumArtist).toString();
map["lrc"]=player->metaData(QMediaMetaData::Lyrics).toString();
setTrackInfo(QString("%1 - %2")
.arg(map["composer"])
.arg(map["musicname"]));
if (coverLabel) {
map["albumpicpath"] = player->metaData(QMediaMetaData::CoverArtUrlLarge).toString();
QUrl url = player->metaData(QMediaMetaData::CoverArtUrlLarge).value<QUrl>();
coverLabel->setPixmap(!url.isEmpty()
? QPixmap(url.toString())
: QPixmap());
}
try{
Connection *con = new Connection();
con->insertMusic(map);
}catch(QString e)
{
qDebug()<<e;
}
}
}
void Player::previousClicked()
{
// Go to previous track if we are within the first 5 seconds of playback
// Otherwise, seek to the beginning.
if(player->position() <= 5000)
playlist->previous();
else
player->setPosition(0);
}
void Player::jump(const QModelIndex &index)
{
if (index.isValid()) {
playlist->setCurrentIndex(index.row());
player->play();
}
}
void Player::playlistPositionChanged(int currentItem)
{
playlistView->setCurrentIndex(playlistModel->index(currentItem, 0));
}
void Player::seek(int seconds)
{
player->setPosition(seconds * 1000);
}
void Player::statusChanged(QMediaPlayer::MediaStatus status)
{
handleCursor(status);
// handle status message
switch (status) {
case QMediaPlayer::UnknownMediaStatus:
case QMediaPlayer::NoMedia:
case QMediaPlayer::LoadedMedia:
case QMediaPlayer::BufferingMedia:
case QMediaPlayer::BufferedMedia:
setStatusInfo(QString());
break;
case QMediaPlayer::LoadingMedia:
setStatusInfo(tr("Loading..."));
break;
case QMediaPlayer::StalledMedia:
setStatusInfo(tr("Media Stalled"));
break;
case QMediaPlayer::EndOfMedia:
QApplication::alert(this);
break;
case QMediaPlayer::InvalidMedia:
displayErrorMessage();
break;
}
}
void Player::handleCursor(QMediaPlayer::MediaStatus status)
{
#ifndef QT_NO_CURSOR
if (status == QMediaPlayer::LoadingMedia ||
status == QMediaPlayer::BufferingMedia ||
status == QMediaPlayer::StalledMedia)
setCursor(QCursor(Qt::BusyCursor));
else
unsetCursor();
#endif
}
void Player::bufferingProgress(int progress)
{
setStatusInfo(tr("Buffering %4%").arg(progress));
}
void Player::videoAvailableChanged(bool available)
{
if (!available) {
videoWidget->setFullScreen(false);
}
}
void Player::setTrackInfo(const QString &info)
{
trackInfo = info;
if (!statusInfo.isEmpty())
setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
else
setWindowTitle(trackInfo);
}
void Player::setStatusInfo(const QString &info)
{
statusInfo = info;
if (!statusInfo.isEmpty())
setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
else
setWindowTitle(trackInfo);
}
void Player::displayErrorMessage()
{
setStatusInfo(player->errorString());
}
void Player::updateDurationInfo(qint64 currentInfo)
{
QString tStr;
if (currentInfo || duration) {
QTime currentTime((currentInfo/3600)%60, (currentInfo/60)%60, currentInfo%60, (currentInfo*1000)%1000);
QTime totalTime((duration/3600)%60, (duration/60)%60, duration%60, (duration*1000)%1000);
QString format = "mm:ss";
if (duration > 3600)
format = "hh:mm:ss";
tStr = currentTime.toString(format) + " / " + totalTime.toString(format);
}
labelDuration->setText(tStr);
}
void Player::mediacenter()
{
center->show();
}
void Player::getInfFromCenter()
{
addToPlaylist(center->getlist());
center->setItems();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/third_party_project_code/Qt5Player.git
git@gitee.com:third_party_project_code/Qt5Player.git
third_party_project_code
Qt5Player
Qt5Player
master

搜索帮助