You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
8.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "music.h"
#include "ui_music.h"
#include <QCoreApplication>
#include <QFileInfoList>
#include <QDir>
#include <QMatrix>
#include <QPixmap>
#include <QTimer>
music::music(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::music)
{
/* 布局初始化 */
musicLayout();
/* 媒体播放器初始化 */
mediaPlayerInit();
/* 扫描歌曲 */
scanSongs();
/* 按钮信号槽连接 */
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()));//调接音量
ui->Slider_volume->hide();//初始化因此
}
music::~music()
{
delete ui;
}
void music::musicLayout()
{
ui->setupUi(this);
}
void music::scanSongs()
{
QDir dir(QCoreApplication::applicationDirPath()
+ "/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 + "\n"
+ fileName.split("-").at(1);
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()<<info.fileName;
}
else {
qDebug()<<
mediaPlaylist->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);
break;
case QMediaPlayer::PlayingState:
ui->btn_play->setChecked(true);
break;
case QMediaPlayer::PausedState:
ui->btn_play->setChecked(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()<<ui->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="<<ui->Slider_volume->value();
}
void music::image_rotate()
{
QMatrix matrix;
matrix.rotate(music::angle);
if( music::angle ==360)
{
music::angle = 0;
}
qDebug()<<"angle"<<music::angle;
//设定图片的大小;
QImage Image = QImage(":image/images/cd.png");
QPixmap pixmap = QPixmap::fromImage(Image);
QPixmap fixpixmap = pixmap.scaled(300,300,Qt::IgnoreAspectRatio
,Qt::SmoothTransformation);
ui->label->setPixmap((fixpixmap)
.transformed(matrix,Qt::SmoothTransformation));
ui->label->setAlignment(Qt::AlignCenter);
}