注意里面的路径是我自己的电脑路径,歌词歌曲等都需要具体的文件
#include "musicplayer.h"
#include "ui_musicplayer.h"
#include <QDebug>
#include <QTime>
#include <QFileInfoList>
#include <QDir>
MusicPlayer::MusicPlayer(QWidget *parent) :
QWidget(parent),
ui(new Ui::MusicPlayer)
{
ui->setupUi(this);
initPlaylist();
initPlayer();
initWidgetUnits();
}
MusicPlayer::~MusicPlayer()
{
delete ui;
}
void MusicPlayer::initPlaylist(void)
{
m_playList = new QMediaPlaylist;
connect(m_playList,SIGNAL(currentIndexChanged(int)),
this,SLOT(slotCurrentIndexChanged(int)));
m_playMode = 3 ;
m_playList->setPlaybackMode(QMediaPlaylist::Loop);
QDir songsDir("E:/QT/0201/歌曲歌词未加密/安装文件/UnigressMusic/songs");
QFileInfoList infos =
songsDir.entryInfoList(QStringList() << "*.mp3" ,
QDir::Files,QDir::Name);
foreach(const QFileInfo &info,infos)
{
m_songNameList.append(info.baseName());
m_playList->addMedia(QUrl::fromLocalFile(info.canonicalFilePath()));
}
}
void MusicPlayer::slotCurrentIndexChanged(int position)
{
m_songName = m_songNameList.at(position);
ui->lb_songName->setText(m_songName);
ui->lw_songNameList->setCurrentRow(position);
QString lyricFileName = "E:/QT/0201/歌曲歌词未加密/安装文件/UnigressMusic/lyrics/" + m_songName + ".lrc" ;
m_mapTimeAndIndex.clear();
lyricContent.clear();
ui->lw_lyricShow->clear();
QFile lyricFile(lyricFileName) ;
if(lyricFile.open((QIODevice::ReadOnly)))
{
QTextStream in(&lyricFile) ;
int index