第16篇 Qt实现简单音乐播放器(三)

第16篇 Qt实现简单音乐播放器(三)

1.音乐文件读取

路径是本地文件路径,找个地方把音乐存起来。从目录中读取后缀为.mp3的文件,保存到QStringList,然后把内容加入到列表中。

void MusicPlaying::readMusicfile()
{
    musicpath = "D:/Qt/MyQt4/mymusic";
    QDir dir(musicpath);

    QStringList nameFileters;
    nameFileters << "*.mp3";

    QStringList files = dir.entryList(nameFileters, QDir::Files|QDir::Readable,QDir::Name);
    this->musiclistWidget->addItems(files);
    this->musiclistWidget->sortItems();

    this->currentmusic->setText(this->musiclistWidget->item(0)->text());
    player->setMedia(QUrl::fromLocalFile(musicpath + '/' + this->musiclistWidget->item(0)->text()));
    player->setVolume(50);

    this->currentvolume->setText("50");
    this->volumeprogress->setSliderPosition(50);
    player->stop();
}

2.一小点美化

构造函数里加入一小点美化,让界面好看那么一丢丢。

MusicPlaying::MusicPlaying(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowIcon(QIcon(":/new/picture/music_icon.jpg"));
    this->setWindowTitle("音乐播放");
    this->setMinimumSize(500,400);
    this->setMaximumSize(500,400);
    QPalette pale = this->palette();
    QImage image(":/new/picture/music_back.webp");
    QImage fitimgpic=image.scaled(this->width(),this->height(), Qt::IgnoreAspectRatio);
    pale.setBrush(QPalette::Window, QBrush(fitimgpic));
    this->setPalette(pale);//设置窗口背景图片
    this->interfaceLayout();//窗口布局

    this->player = new QMediaPlayer;
    QObject::connect(this->player,SIGNAL(positionChanged(qint64)),
                     this,SLOT(on_player_positionChanged(qint64)));//歌曲的当前播放位置改变
    QObject::connect(this->player,SIGNAL(durationChanged(qint64)),
                     this,SLOT(on_player_durationChanged(qint64)));//整首歌的持续时间
    this->readMusicfile();

}

3.效果

效果
这就是最后的效果了。

4.代码

最后在统一的给出代码。源文件。

4.1.源文件

#include "musicplaying.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QDir>

MusicPlaying::MusicPlaying(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowIcon(QIcon(":/new/picture/music_icon.jpg"));
    this->setWindowTitle("音乐播放");
    this->setMinimumSize(500,400);
    this->setMaximumSize(500,400);
    QPalette pale = this->palette();
    QImage image(":/new/picture/music_back.webp");
    QImage fitimgpic=image.scaled(this->width(),this->height(), Qt::IgnoreAspectRatio);
    pale.setBrush(QPalette::Window, QBrush(fitimgpic));
    this->setPalette(pale);//设置窗口背景图片
    this->interfaceLayout();//窗口布局

    this->player = new QMediaPlayer;
    QObject::connect(this->player,SIGNAL(positionChanged(qint64)),
                     this,SLOT(on_player_positionChanged(qint64)));//歌曲的当前播放位置改变
    QObject::connect(this->player,SIGNAL(durationChanged(qint64)),
                     this,SLOT(on_player_durationChanged(qint64)));//整首歌的持续时间
    this->readMusicfile();

}

void MusicPlaying::interfaceLayout()
{
    QVBoxLayout* vlayout = new QVBoxLayout;//总的布局为垂直布局
    QHBoxLayout* hlayout1 = new QHBoxLayout;
    QHBoxLayout* hlayout2 = new QHBoxLayout;//中间有两个水平布局
    QVBoxLayout* vlayout2 = new QVBoxLayout;//再穿插一个小的垂直布局

    this->currentmusic = new QLabel("当前音乐");

    this->musiclistWidget = new QListWidget;//所有音乐列表显示
    QObject::connect(this->musiclistWidget,SIGNAL(currentRowChanged(int)),
                     this,SLOT(on_musiclistWidget_currentRowChanged(int)));
    this->musiclistWidget->setStyleSheet("background-color:transparent");//设置背景透明

    this->currenttime = new QLabel("00:00");//音乐当前进度时间
    this->timeprogress = new QSlider;//时间进度
    QObject::connect(this->timeprogress,SIGNAL(sliderMoved(int)),
                     this,SLOT(on_timeprogress_sliderMoved(int)));
    this->timeprogress->setOrientation(Qt::Horizontal);//设置进度条为水平
    this->durationstime = new QLabel("00:00");//整首歌的持续时间

    this->lastsong = new QPushButton("上一首");
    QObject::connect(this->lastsong,SIGNAL(clicked()),this,SLOT(on_lastsong_clicked()));

    this->playpause = new QPushButton("暂停/播放");
    QObject::connect(this->playpause,SIGNAL(clicked()),this,SLOT(on_playpause_clicked()));

    this->nextsong = new QPushButton("下一首");
    QObject::connect(this->nextsong,SIGNAL(clicked()),this,SLOT(on_nextsong_clicked()));

    this->currentvolume = new QLabel("99");//音量
    this->volumeprogress = new QSlider;//音量进度
    QObject::connect(this->volumeprogress,SIGNAL(valueChanged(int)),
                     this,SLOT(on_volumeprogress_valueChanged(int)));
    
    vlayout->addWidget(this->currentmusic);//把当前播放的音乐放到最上面
    vlayout->addWidget(this->musiclistWidget);//然后是音乐列表
    //先把当前进度时间   时间进度   整首歌持续时间放在一个水平布局里
    hlayout1->addWidget(this->currenttime);
    hlayout1->addWidget(this->timeprogress);
    hlayout1->addWidget(this->durationstime);
    vlayout->addLayout(hlayout1);//把他们加到整体布局
    //把上一首   暂停/播放   下一首   音量   音量进度加到水平布局了
    hlayout2->addWidget(this->lastsong);
    hlayout2->addWidget(this->playpause);
    hlayout2->addWidget(this->nextsong);
    vlayout2->addWidget(this->currentvolume);
    vlayout2->addWidget(this->volumeprogress);
    hlayout2->addLayout(vlayout2);
    vlayout->addLayout(hlayout2);

    this->setLayout(vlayout);//设置整体布局
}

void MusicPlaying::readMusicfile()
{
    musicpath = "D:/Qt/MyQt4/mymusic";
    QDir dir(musicpath);

    QStringList nameFileters;
    nameFileters << "*.mp3";

    QStringList files = dir.entryList(nameFileters, QDir::Files|QDir::Readable,QDir::Name);
    this->musiclistWidget->addItems(files);
    this->musiclistWidget->sortItems();

    this->currentmusic->setText(this->musiclistWidget->item(0)->text());
    player->setMedia(QUrl::fromLocalFile(musicpath + '/' + this->musiclistWidget->item(0)->text()));
    player->setVolume(50);

    this->currentvolume->setText("50");
    this->volumeprogress->setSliderPosition(50);
    player->stop();
}

MusicPlaying::~MusicPlaying()
{
}

void MusicPlaying::on_player_positionChanged(qint64 position)
{
    if(this->timeprogress->isSliderDown()){
        return ;
    }
    this->timeprogress->setSliderPosition(position);
    int secs = position / 1000;
    int mins = secs / 60;
    secs = secs % 60;
    this->currenttime->setText(QString::asprintf("%02d:%02d",mins,secs));
    if(this->player->position() == this->player->duration() &&
            this->player->position() != 0 && this->player->duration() != 0){
        on_nextsong_clicked();
    }
}

void MusicPlaying::on_player_durationChanged(qint64 duration)
{
    this->timeprogress->setMaximum(duration);//设置进度条最大值为当前音乐总进度
    int secs = duration/1000;
    int mins = secs/60;
    secs = secs % 60;
    this->durationstime->setText(QString::asprintf("%02d:%02d",mins,secs));
}

void MusicPlaying::on_musiclistWidget_currentRowChanged(int currentrow)
{
    this->currentmusic->setText(this->musiclistWidget->item(currentrow)->text());
    player->setMedia(QUrl::fromLocalFile(musicpath+'/'+this->musiclistWidget->item(currentrow)->text()));
    player->setVolume(this->volumeprogress->value());
    player->play();
    this->isplay = true;
}

void MusicPlaying::on_timeprogress_sliderMoved(int position)
{
    player->setPosition(position);//如果滑块改变位置,则改变音乐进度
    int secs = position / 1000;
    int mins = secs / 60;
    secs = secs % 60;
    this->currenttime->setText(QString::asprintf("%02d:%02d",mins,secs));
    if(this->player->position() == this->player->duration() &&
            this->player->position() != 0 && this->player->duration() != 0){
        on_nextsong_clicked();
    }
}

void MusicPlaying::on_lastsong_clicked()
{
    int currentrow = this->musiclistWidget->currentRow();
    int rowcount = this->musiclistWidget->count();
    this->musiclistWidget->setCurrentRow((currentrow - 1 + rowcount) % rowcount);
}

void MusicPlaying::on_playpause_clicked()
{
    if(isplay){
        this->player->pause();
        this->isplay = false;
    }
    else{
        this->player->play();
        this->isplay = true;
    }
}

void MusicPlaying::on_nextsong_clicked()
{
    int currentrow = this->musiclistWidget->currentRow();
    int rowcount = this->musiclistWidget->count();
    this->musiclistWidget->setCurrentRow((currentrow + 1) % rowcount);
}

void MusicPlaying::on_volumeprogress_valueChanged(int value)
{
    player->setVolume(value);
    this->currentvolume->setText(QString::asprintf("%d",value));
}

ok了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大唐不良猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值