#include "music.h" #include "ui_music.h" #include #include #include #include #include music::music(QWidget *parent) : QDialog(parent) , ui(new Ui::music) { /* 布局初始化 */ musicLayout(); /* 媒体播放器初始化 */ mediaPlayerInit(); /* 扫描歌曲 */ scanSongs(); rotationTimer = new QTimer(this); /* 按钮信号槽连接 */ connect(ui->btn_previous, SIGNAL(clicked()),this, SLOT(btn_previous_clicked())); connect(ui->btn_play, SIGNAL(clicked()),this, SLOT(btn_play_clicked())); connect(ui->btn_next, SIGNAL(clicked()),this, SLOT(btn_next_clicked())); /* 列表信号槽连接 */ connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(listWidgetCliked(QListWidgetItem*)));//item 参数指的就是被用户点击的列表项 /* 媒体信号槽连接 */ connect(musicPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(mediaPlayerStateChanged(QMediaPlayer::State)));//播放的状态一旦改变 connect(mediaPlaylist, SIGNAL(currentIndexChanged(int)), this, SLOT(mediaPlaylistCurrentIndexChanged(int)));//播放列表改变 connect(musicPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(musicPlayerDurationChanged(qint64)));//获取音乐的信息 connect(musicPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(mediaPlayerPositionChanged(qint64)));//获取音乐播放的进度 /* slider信号槽连接 */ connect(ui->durationSlider, SIGNAL(sliderReleased()), this, SLOT(durationSliderReleased()));//播放进度条滑动,修改音乐的进度 connect(ui->Slider_volume, SIGNAL(sliderReleased()), this, SLOT(volume_change()));//调接音量 //Lambda 捕获 this 11新特性 旋转功能 connect(this, SIGNAL(Image_rate(bool)), this, SLOT(ImageRate(bool))); connect(rotationTimer, &QTimer::timeout, this, [=]() { image_rotate(2); // 每次超时旋转10度 }); ui->Slider_volume->hide();//初始化因此 } music::~music() { delete ui; delete musicPlayer; delete mediaPlaylist; delete listWidget; delete listMask; } void music::musicLayout() { ui->setupUi(this); } void music::scanSongs() { // QDir dir(QCoreApplication::applicationDirPath() // + "/myMusic"); QDir dir("F:/coderead/QT/demo/music/myMusic"); QDir dirbsolutePath(dir.absolutePath()); /* 如果目录存在 */ if (dirbsolutePath.exists()) { /* 定义过滤器 */ QStringList filter; /* 包含所有.mp3后缀的文件 */ filter << "*.mp3"; /* 获取该目录下的所有文件 */ QFileInfoList files = dirbsolutePath.entryInfoList(filter, QDir::Files); /* 遍历 */ for (int i = 0; i < files.count(); i++) { MediaObjectInfo info; /* 使用utf-8编码 */ QString fileName = QString::fromUtf8(files.at(i) .fileName() .replace(".mp3", "") .toUtf8() .data()); info.fileName = fileName; QStringList parts = fileName.split("-"); if (parts.size() > 1) { info.fileName = fileName + "\n" + parts.at(1); } else { info.fileName = fileName; // 或其他默认处理 } info.filePath = QString::fromUtf8(files.at(i) .filePath() .toUtf8() .data()); /* 媒体列表添加歌曲 */ if (mediaPlaylist->addMedia(QUrl::fromLocalFile(info.filePath))) { /* 添加到容器数组里储存 */ mediaObjectInfo.append(info); /* 添加歌曲名字至列表 */ ui->listWidget->addItem(info.fileName); qDebug()<errorString() .toUtf8().data() << endl; qDebug()<< " Error number:" << mediaPlaylist->error() << endl; } } } } void music::btn_play_clicked() { int state = musicPlayer->state();//获取播放器的状态 switch (state) { case QMediaPlayer::StoppedState: /* 媒体播放 */ musicPlayer->play(); break; case QMediaPlayer::PlayingState: /* 媒体暂停 */ musicPlayer->pause(); break; case QMediaPlayer::PausedState: musicPlayer->play(); break; } } void music::btn_next_clicked() { musicPlayer->stop(); int count = mediaPlaylist->mediaCount(); if (0 == count) return; /* 列表下一个 */ mediaPlaylist->next(); musicPlayer->play(); } void music::btn_previous_clicked() { musicPlayer->stop(); int count = mediaPlaylist->mediaCount(); if (0 == count) return; /* 列表上一个 */ mediaPlaylist->previous(); musicPlayer->play(); } void music::listWidgetCliked(QListWidgetItem *item) { musicPlayer->stop(); mediaPlaylist->setCurrentIndex(ui->listWidget->row(item));//获取行号 musicPlayer->play(); } void music::mediaPlayerStateChanged(QMediaPlayer::State state) { switch (state) { case QMediaPlayer::StoppedState: ui->btn_play->setChecked(false); emit Image_rate(false); break; case QMediaPlayer::PlayingState: ui->btn_play->setChecked(true); emit Image_rate(true); break; case QMediaPlayer::PausedState: ui->btn_play->setChecked(false); emit Image_rate(false); break; } } void music::mediaPlaylistCurrentIndexChanged(int index) { if (-1 == index) return; /* 设置列表正在播放的项 */ ui->listWidget->setCurrentRow(index);//设置显示列表指向相应的行 } void music::musicPlayerDurationChanged(qint64 duration)//duration 音乐的时间(wei秒为单位) { ui->durationSlider->setRange(0, duration / 1000);//设置范围 int second = duration / 1000; int minute = second / 60;//分钟 second %= 60;//秒 QString mediaDuration; mediaDuration.clear(); if (minute >= 10) mediaDuration = QString::number(minute, 10);// else mediaDuration = "0" + QString::number(minute, 10); if (second >= 10) mediaDuration = mediaDuration + ":" + QString::number(second, 10); else mediaDuration = mediaDuration + ":0" + QString::number(second, 10); /* 显示媒体总长度时间 */ ui->label_time_out->setText(mediaDuration); } void music::mediaPlayerPositionChanged(qint64 position)//position音乐现在播放的时间 { if (!ui->durationSlider->isSliderDown())//没桦到最后 { ui->durationSlider->setValue(position/1000); } int second = position / 1000; int minute = second / 60; second %= 60; QString mediaPosition; mediaPosition.clear(); if (minute >= 10) mediaPosition = QString::number(minute, 10); else mediaPosition = "0" + QString::number(minute, 10); if (second >= 10) mediaPosition = mediaPosition + ":" + QString::number(second, 10); else mediaPosition = mediaPosition + ":0" + QString::number(second, 10); /* 显示现在播放的时间 */ ui->label_time_start->setText(mediaPosition); } void music::durationSliderReleased() { /* 设置媒体播放的位置 */ musicPlayer->setPosition(ui->durationSlider->value() * 1000); qDebug()<durationSlider->value(); } void music::mediaPlayerInit() { musicPlayer = new QMediaPlayer(this); mediaPlaylist = new QMediaPlaylist(this); /* 确保列表是空的 */ mediaPlaylist->clear(); /* 设置音乐播放器的列表为mediaPlaylist */ musicPlayer->setPlaylist(mediaPlaylist); /* 设置播放模式,Loop是列循环 */ mediaPlaylist->setPlaybackMode(QMediaPlaylist::Loop); musicPlayer->setVolume(20); } void music::on_btn_volume_toggled(bool checked) { if(checked==true)//按第二遍 { qDebug()<<"released"; ui->Slider_volume->hide(); } else { qDebug()<<"released"; ui->Slider_volume->show(); } } void music::volume_change() { ui->Slider_volume->setRange(0, 150);//设置范围 musicPlayer->setVolume(ui->Slider_volume->value()); qDebug()<<"volume="<Slider_volume->value(); } void music::image_rotate(int angleIncrement) { if (angleIncrement == 0) { return; // 如果增量为0,不进行旋转 } music::angle += angleIncrement; if (music::angle >= 360) { music::angle -= 360; } QTransform transform; transform.rotate(music::angle); QImage image(":image/images/cd.png"); QPixmap pixmap = QPixmap::fromImage(image); QPixmap fixedPixmap = pixmap.scaled(300, 300, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap rotatedPixmap = fixedPixmap.transformed(transform, Qt::SmoothTransformation); ui->label->setPixmap(rotatedPixmap); ui->label->setAlignment(Qt::AlignCenter); } // 在 music 类中添加停止播放音乐的方法 void music::stopmusic() { if (musicPlayer->state() != QMediaPlayer::StoppedState) { musicPlayer->stop(); } } void music::on_pushButton_clicked() { emit Music_close(false); qDebug() << "关闭music!"; } void music::on_pushButton_2_clicked() { emit Music_Minisize(true); } void music::ImageRate(bool t) { if(t) { rotationTimer->start(60); // 每100毫秒旋转一次 } else { rotationTimer->stop(); //image_rotate(0); } }