qt5.2并没有了phonon 模块 ,在播放音频视频的时候可以使用QMediaplayer来实现。
//创建媒体
player=new QMediaPlayer(this);
//创建播放列表,并在列表里添加两首歌曲
mediaList=new QMediaPlaylist;
mediaList->addMedia(QUrl::fromLocalFile("E:/360Downloads/Software/Qt5.2/Tools/QtCreator/bin/video/nfdsm.mp3"));
mediaList->addMedia(QUrl::fromLocalFile("E:/360Downloads/Software/Qt5.2/Tools/QtCreator/bin/video/MP3/ljf.mp3"));
//设置播放列表中的当前歌曲为第几首
mediaList->setCurrentIndex(1);
//为播放器添加播放列表
player->setPlaylist(mediaList);
player->setVolume(volumn);
//播放器开始播放列表中的歌曲
player->play();关于中文路径,目前还没有找到方法,所以只好用英文路径代替
为整个窗口设置背景色
//为整个窗口设置背景色
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/image/sz")));
this->setPalette(palette);
connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(positionChanged(qint64)));
//持续信号,使整个滑动条能够持续前进
connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
void Widget::durationChanged(qint64 duration)
{
ui->horizontalSlider->setRange(0,duration);
}
//通过滚动条控制音量为滚动条添加信号
void Widget::on_horizontalSlider_2_valueChanged(int value)
{
player->setVolume(value);
}
实现播放上一首,下一首,暂停或继续播放等功能。
//播放上一首歌曲
void Widget::on_pre_clicked()
{
mediaList->previous();
player->play();
}
//播放下一首歌曲
void Widget::on_next_clicked()
{
int currentIndex=mediaList->currentIndex();
if(++currentIndex==mediaList->mediaCount())
currentIndex=0;
mediaList->setCurrentIndex(currentIndex);
}
//暂停或是继续播放
void Widget::on_pos_clicked()
{
if(ui->pos->text()==tr("ps"))
{
player->pause();
ui->pos->setText(tr("cn"));
}
else
{
player->play();
ui->pos->setText(tr("ps"));
}
}
//当单击某行单元格时,显示改行单元格所对应的歌曲
connect(ui->tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(playTo(int))); 所调用的槽函数为playTo
//设置点击的是哪首歌曲
void Widget::playTo(int i)
{
mediaList->setCurrentIndex(i);
player->play();
}
//选择播放方式
void Widget::on_orderComboBox_currentIndexChanged(int index)
{
if(index==0)
{
mediaList->setPlaybackMode(QMediaPlaylist::Sequential);//顺序播放
}
else
{
if(index==1)
{
mediaList->setPlaybackMode(QMediaPlaylist::Random); //随机播放
}
else
{
if(index==2)
{
mediaList->setPlaybackMode(QMediaPlaylist::Loop); //列表循环
}
else
{
mediaList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop); //单曲循环
}
}
}
}
可以批量导入歌曲,具体代码如下所示:
//导入歌曲
void Widget::on_daoru_clicked()
{
QString initialName=QDir::homePath();//返回一个用户的全路径
QStringList pathList=QFileDialog::getOpenFileNames(this, codeChange("选择文件"), initialName, tr(".mp3"));
for(int i=0; i++i)
{
QString path=QDir::toNativeSeparators(pathList.at(i)); //把斜杠转化成反斜杠
if(!path.isEmpty())
{
mediaList->addMedia(QUrl::fromLocalFile(path));//加入到播放列表
QString fileName=path.split("\\").last();//用斜杠分开,取最后一个名字
int rownum=ui->tableWidget->rowCount();
ui->tableWidget->insertRow(rownum);//为整个table插入一行行号为rownum,也就是说一首歌的信息占据一行的位置
ui->tableWidget->setItem(rownum, 0, new QTableWidgetItem(fileName.split(".").front()));
ui->tableWidget->setItem(rownum, 1, new QTableWidgetItem(fileName.split(".").last()));
ui->tableWidget->setItem(rownum,2,new QTableWidgetItem(path));
ui->tableWidget->item(rownum,1)->setFlags(Qt::NoItemFlags);
ui->tableWidget->item(rownum,2)->setFlags(Qt::NoItemFlags);
}
}
}
显示歌词这部分是把歌词显示在textedit上,并没有实现桌面歌词的显示,具体代码如下所示:
//当单击某行单元格时,根据各歌曲名找到对应的歌词
connect(ui->tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(geCi(int)));
//显示歌词部分
void Widget::geCi(int z)
{
if(ui->checkBox->isChecked()==true)
{
QString str=ui->tableWidget->item(z,0)->text();
qDebug()< QFile file("E:/360Downloads/Software/Qt5.2/Tools/QtCreator/bin/video/"+str+".lrc");if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){
QMessageBox::information(this, tr("information"), codeChange("当前目录下未找到歌词文件"));}
else{
QTextStream in(&file);QString result=in.readAll();QTextEdit *edit=new QTextEdit();edit->setFixedHeight(300);edit->setFixedWidth(293);edit->setText(result);
edit->show();
}
}
}
搜索歌曲部分,可以实现全名搜索,代码如下所示://搜索歌曲
void Widget::on_go_clicked(){QStringList list; //存放的是歌曲名QStringList list1; //存放的是歌曲类型QStringList list2; //存放的是歌曲路径QStringList list3; //搜索得同样的歌曲名QStringList list4; //存放歌曲类型QStringList list5; //存放歌曲路径if(!ui->lineEdit->text().isEmpty()){
for(int i=0;i<ui->tableWidget->rowCount();i++){
QString str=ui->tableWidget->item(i,0)->text();QString str1=ui->tableWidget->item(i,1)->text();QString str2=ui->tableWidget->item(i,2)->text();list< list2< list3<}
qDebug()< QString sousuo= ui->lineEdit->text();qDebug()< for(int s=0;s{
if(list.at(s)==sousuo){
list1< list4< list5< ui->tableWidget->clearContents();for(int x=0;x{
ui->tableWidget->setItem(x,0,new QTableWidgetItem(list1.at(x)));ui->tableWidget->setItem(x,1,new QTableWidgetItem(list4.at(x)));ui->tableWidget->setItem(x,2,new QTableWidgetItem(list5.at(x)));ui->tableWidget->item(x,1)->setFlags(Qt::NoItemFlags);ui->tableWidget->item(x,2)->setFlags(Qt::NoItemFlags);QString path=QDir::toNativeSeparators(list1.at(x));//mediaList->clear();mediaList->addMedia(QUrl::fromLocalFile(path));player->setPlaylist(mediaList);player->play();mediaList->setCurrentIndex(s);}
break;}
else{
if(s==list.size()-1){
QMessageBox::about(this,codeChange("提示信息"),codeChange("未找到符合的歌曲"));}
}
}
}
}
删除歌曲部分代码如下所示://删除歌曲
void Widget::on_pushButton_clicked(){QString str=codeChange("确定要删除该首歌曲吗?");qDebug()< if (QMessageBox::Yes == QMessageBox::question(this,tr("Question"),str,
QMessageBox::Yes | QMessageBox::No,QMessageBox::Yes)) {player->EndOfMedia;ui->tableWidget->removeRow(mediaList->currentIndex());QMessageBox::information(this, tr("information"), codeChange("删除成功"));} else {QMessageBox::information(this,tr("information"),codeChange("取消删除"));}}
收藏歌曲代码如下所示://收藏歌曲
void Widget::on_pushButton_2_clicked(){int row=mediaList->currentIndex();qDebug()<<ui->tableWidget->item(row,1)->text();qDebug()<<ui->tableWidget->item(row,2)->text();int rownum=ui->shoucang->rowCount();ui->shoucang->insertRow(rownum);ui->shoucang->setItem(rownum,0,new QTableWidgetItem(ui->tableWidget->item(row,0)->text()));ui->shoucang->setItem(rownum,1,new QTableWidgetItem(ui->tableWidget->item(row,1)->text()));ui->shoucang->setItem(rownum,2,new QTableWidgetItem(ui->tableWidget->item(row,2)->text()));ui->shoucang->setEditTriggers(QAbstractItemView::NoEditTriggers);ui->shoucang->item(rownum,1)->setFlags(Qt::NoItemFlags);ui->shoucang->item(rownum,2)->setFlags(Qt::NoItemFlags);QMessageBox::information(this,tr("information"),codeChange("该歌曲成功加入收藏列表"));}
关于系统托盘,如图所示:可以这样实现。点击右键可以看到菜单,包括上一首,下一首,暂停或播放,关于酷我,关于我们等等。代码如图所示:trayicon = new QSystemTrayIcon(this);trayiconMenu=new QMenu(this);pre=new QAction(QIcon(":/image/sys"),codeChange("上一首"),this);next=new QAction(QIcon(":/image/xys"),codeChange("下一首"),this);zanting=new QAction(QIcon(":/image/zt"),codeChange("暂停"),this);guanyu=new QAction(codeChange("关于酷我"),this);quit=new QAction(QIcon(":/image/stop"),codeChange("退出"),this);wangzhi=new QAction(codeChange("关于我们"),this);trayiconMenu->addAction(pre);trayiconMenu->addAction(quit);trayiconMenu->addAction(next);trayiconMenu->addAction(zanting);trayiconMenu->addAction(guanyu);trayiconMenu->addAction(wangzhi);//点击菜单中的退出时,退出程序connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));//点击菜单中的上一首时,切换到上一首歌曲connect(pre, SIGNAL(triggered()), this, SLOT(on_pre_clicked()));//点击菜单中的下一首时,切换到下一首歌曲connect(next, SIGNAL(triggered()), this, SLOT(on_next_clicked()));//点击菜单中的关于酷我时,显示酷我音乐的信息connect(guanyu, SIGNAL(triggered()), this, SLOT(support()));//点击菜单中的暂停时,使歌曲暂停connect(zanting, SIGNAL(triggered()), this, SLOT(on_pos_clicked()));//点击菜单的关于我们时,显示网址connect(wangzhi, SIGNAL(triggered()), this, SLOT(aboutUs()));trayicon->setContextMenu(trayiconMenu);trayicon->setIcon(QIcon(":/image/icon.ico"));//显示托盘图标trayicon->setToolTip(codeChange("酷我音乐盒"));trayiconMenu = new QMenu(this);trayicon->show();//为系统托盘绑定单击信号的槽 即图标激活时connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason)));void Widget::onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason){switch(reason){
//单击case QSystemTrayIcon::Trigger://双击case QSystemTrayIcon::DoubleClick://恢复窗口显示this->setWindowState(Qt::WindowActive);this->show();break;default:break;}
}其中音乐播放器还有很多小bug,请多多指正。