代码拉取完成,页面将自动刷新
#include "Control.h"
#include "ui_Control.h"
Control::Control(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Control)
, playList()
{
ui->setupUi(this);
mediaPlayer = new QMediaPlayer(this);
index = 0;
progressBar = new ProgressBar();
musicOption = new MusicOption();
volumeSet = new VolumeSet();
playPattern = new PlayPattern();
songCollection = new SongCollection();
prePlay = new PrePlay();
play = new Play();
nextPlay = new NextPlay();
songCollection->isMyLove = false;
QString currentPath = QDir::currentPath();
timer = new QTimer(this);
timer->start(10);
connect(timer, &QTimer::timeout, this, &Control::on_timer_timeout);
ui->volumeSlider->setValue(100);
updateSongTime();
connect(mediaPlayer, &QMediaPlayer::stateChanged, this, [=]() {
playPattern->changePlayPattern(ui, mediaPlayer, index, playList);
if (mediaPlayer->state() == QMediaPlayer::StoppedState && mediaPlayer->position() == mediaPlayer->duration()) {
play->newSong(ui, index,mediaPlayer, playList, songNameList, songCollection);
updateSongTime();
}
});
setPauseImage();
}
Control::~Control()
{
delete ui;
delete progressBar;
delete musicOption;
delete volumeSet;
delete playPattern;
delete prePlay;
delete play;
delete nextPlay;
delete timer;
}
void Control::on_musicList_clicked()
{
QString pubPath;
auto musicList = musicOption->chooseMusic(this, pubPath);
if (musicList.length() == 0) return;
//把音乐路径保存下来
for(auto file :musicList) {
qDebug() << file;
songNameList.addSong(file);
playList.addSong(pubPath + "/" + file);
}
playList.unique();
songNameList.unique();
//把音乐放在listweidget中展示
ui->listWidget->clear();
ui->listWidget->addItems(songNameList.returnPlayList());
//如果第一次选择歌单,默认选中第一个音乐
if (mediaPlayer->media().isNull()) {
ui->listWidget->setCurrentRow(0);
play->newSong(ui, index, mediaPlayer, playList, songNameList, songCollection);
mediaPlayer->pause();
setPauseImage();
}
// 如果不是第一次选择歌单,则还是选中原来的哪个
else
ui->listWidget->setCurrentRow(index);
}
void Control::on_playMode_clicked()
{
if (mediaPlayer->media().isNull()) return;
playPattern->playPattern = ++playPattern->playPattern % 3;
if (playPattern->playPattern == 1) playPattern->randomlyPlayedSongs.clear();
playPattern->changePlayPattern(ui, mediaPlayer, index, playList);
if (mediaPlayer->state() == QMediaPlayer::StoppedState && mediaPlayer->position() == mediaPlayer->duration()) {
play->newSong(ui, index, mediaPlayer, playList, songNameList, songCollection);
updateSongTime();
}
}
void Control::on_pre_clicked()
{
int isPlaying = false;
if (mediaPlayer->state() == QMediaPlayer::PlayingState)
{
isPlaying = true;
}
prePlay->prePlay(ui, mediaPlayer, index, playList);
prePlay->newSong(ui, index, mediaPlayer, playList, songNameList, songCollection);
updateSongTime();
if (!isPlaying)
{
mediaPlayer->pause();
ui->play->setStyleSheet(" background-image: url(:/picture/8.png); ");
}
}
void Control::on_play_clicked()
{
play->play(ui, index,mediaPlayer, playList, songNameList, songCollection);
updateSongTime();
}
void Control::on_next_clicked()
{
if (mediaPlayer->media().isNull()) return;
int isPlaying = false;
if (mediaPlayer->state() == QMediaPlayer::PlayingState)
{
isPlaying = true;
}
nextPlay->nextPlay(ui, mediaPlayer, index, playList);
nextPlay->newSong(ui, index, mediaPlayer, playList, songNameList, songCollection);
updateSongTime();
if (!isPlaying)
{
mediaPlayer->pause();
ui->play->setStyleSheet(" background-image: url(:/picture/8.png);");
}
}
void Control::on_loveSong_clicked()
{
if (mediaPlayer->media().isNull()) return;
if (songCollection->isMyLove)
{
songCollection->deleteFromMyLove(songNameList[index].toString());
songCollection->isMyLove = false;
ui->loveSong->setStyleSheet("background-image: url(:/picture/12.jpg);");
}
else
{
songCollection->addToMyLove(playList[index].toString(), songNameList[index].toString());
songCollection->isMyLove = true;
ui->loveSong->setStyleSheet("background-image: url(:/picture/11.png);");
}
}
void Control::on_volume_clicked()
{
if (mediaPlayer->media().isNull()) return;
if (mediaPlayer->volume())
volumeSet->setVolume1image(ui);
else
volumeSet->setVolume2image(ui);
}
void Control::on_horizontalSlider_valueChanged(int value)
{
qint64 progress = value * 1000;
if (qAbs(progress - mediaPlayer->position()) > 100)
mediaPlayer->setPosition(ui->horizontalSlider->value() * 1000);
}
void Control::on_volumeSlider_valueChanged(int value)
{
volumeSet->setVolume(mediaPlayer, value);
}
//实现播放暂停是时的图片更改
void Control::setPauseImage() {
ui->play->setStyleSheet(" background-image: url(:/picture/8.png); ");
}
void Control::setPlayImage() {
ui->play->setStyleSheet(" background-image: url(:/picture/4.png); ");
}
//显示歌曲时长及与进度条连接
void Control::on_timer_timeout()
{
progressBar->updateProgress(ui, mediaPlayer);
}
void Control::updateSongTime() {
connect(mediaPlayer, &QMediaPlayer::durationChanged, this, [=](qint64 duration)
{
ui->horizontalSlider->setRange(0, mediaPlayer->duration() / 1000);
ui->horizontalSlider->setMaximum(mediaPlayer->duration() / 1000);
ui->horizontalSlider->setValue(0);
int minutes = duration / 1000 / 60;
int seconds = duration / 1000 % 60;
QString *totalTime = nullptr;
if (seconds >= 10) {
totalTime = new QString(QString::number(minutes) + ":" + QString::number(seconds));
}
else {
totalTime = new QString(QString::number(minutes) + ":" + "0" + QString::number(seconds));
}
ui->duration->setText(*totalTime);
});
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。