1 Star 3 Fork 0

wujiamin2023/Qt-mscPlayer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 10.08 KB
一键复制 编辑 原始数据 按行查看 历史
Ollivander 提交于 2022-02-26 16:30 . ver2
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <QDebug>
#include <QSlider>
#include <QMediaPlaylist>
#include <QFileDialog>
#include <QPainter>
#include <QMenu>
#include <QDesktopServices>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QSystemTrayIcon>
#include "lyrsub.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initUI();
player=new QMediaPlayer(this);
player->setVolume(50);
ui->volumnSlider->setValue(player->volume());
ui->volumnSlider->setMaximum(100);
playlist=new QMediaPlaylist(this);
playlist->setPlaybackMode(QMediaPlaylist::Loop);
playMode=loopSingle;
lrcSub=new lyrSub(this);
//获取歌曲元数据,加载歌词文件
connect(player,&QMediaPlayer::metaDataAvailableChanged,
this,&MainWindow::on_metaDataAvailableChanged);
//播放进度
connect(player,&QMediaPlayer::positionChanged,this,
&MainWindow::on_positionChanged);
//音量调节
connect(ui->volumnSlider,&QSlider::valueChanged,
[this](int value){player->setVolume(value);});
connect(player,&QMediaPlayer::volumeChanged,
[this](int val){ui->volumnSlider->setValue(val);});
//桌面歌词显示
//connect(ui->widget,&LyrcWind::lyricString,lrcSub,&lyrSub::on_lyricString);
connect(ui->widget,&LyricList::lyricString,lrcSub,&lyrSub::on_lyricString);
//系统托盘图标被点击
connect(trayIcon,&QSystemTrayIcon::activated,[this](QSystemTrayIcon::ActivationReason reason){
if(reason==QSystemTrayIcon::Trigger)
this->show();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
//显示&隐藏播放列表
void MainWindow::on_actPlayList_triggered(bool checked)
{
if(checked){
this->setFixedHeight(800);
ui->tabWidget->setVisible(true);
}
else{
ui->tabWidget->setVisible(false);
this->setFixedHeight(160);
}
}
//打开文件加载列表
void MainWindow::on_actOpen_triggered()
{
QStringList fileNames=QFileDialog::getOpenFileNames(this,"select",".","music(*.mp3 *.wav)");
int fileCount=fileNames.count();
if(fileCount==0) return;
for(int i=0;i<fileCount;i++){
playlist->addMedia(QUrl::fromLocalFile(fileNames.at(i)));
QString path=fileNames.at(i);
//路径截取文件名
auto item=new QListWidgetItem(path.mid(path.lastIndexOf('/')+1));
item->setToolTip(path);
ui->listWidget->addItem(item);
}
player->setPlaylist(playlist);
//先加载最后一个文件
playlist->setCurrentIndex(playlist->mediaCount()-1);
}
void MainWindow::initUI()
{
setWindowTitle("Qt-MscPlayer");
setAcceptDrops(true);
trayIcon=new QSystemTrayIcon(QIcon(":/images/images/icon.png"),this);
trayIcon->setToolTip("Qt-MscPlayer");
QMenu *trayMenu=new QMenu;
trayMenu->addAction("quit",this,&MainWindow::close);
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
ui->widget_2->setFixedHeight(150);
this->setFixedHeight(160);
ui->tabWidget->hide();
bgPath="./images/back2.jpg";
// setPalette(QColor(250,250,250));
// setAutoFillBackground(true);
ui->tbPlay->setIcon(QIcon(":/images/images/play.png"));
ui->tbVolum->setIcon(QIcon(":/images/images/maxvolume.png"));
ui->tbplayModel->setIcon(QIcon(":/images/images/order.png"));
ui->tbBack->setDefaultAction(ui->actBack);
ui->tbNext->setDefaultAction(ui->actNext);
ui->tbOpen->setDefaultAction(ui->actOpen);
ui->tbPrev->setDefaultAction(ui->actPrev);
ui->tbPlayList->setDefaultAction(ui->actPlayList);
//设置不可编辑
ui->listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
//设置只能选一行
ui->listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->listWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.drawPixmap(this->rect(),QPixmap(bgPath));
painter.setBrush(QColor(255,255,255,130));
painter.drawRect(this->rect());
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if(event->mimeData()->hasUrls())
event->acceptProposedAction();
else event->ignore();
}
void MainWindow::dropEvent(QDropEvent *event)
{
if(event->mimeData()->hasUrls()){
auto urls=event->mimeData()->urls();
int fileCount=urls.count();
if(fileCount==0) return;
for(int i=0;i<fileCount;i++){
playlist->addMedia(urls.at(i));
//路径截取文件名
QString path=urls.at(i).toLocalFile();
auto item=new QListWidgetItem(path.mid(path.lastIndexOf('/')+1));
item->setToolTip(path);
ui->listWidget->addItem(item);
}
player->setPlaylist(playlist);
playlist->setCurrentIndex(playlist->mediaCount()-1);
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(this->isVisible()){
this->hide();
trayIcon->showMessage("Qt-MscPlayer","click me show again");
event->ignore();
}
}
void MainWindow::on_positionChanged(qint64 position)
{
ui->slider->setMaximum(player->duration()/1000);//总共时长多少秒
ui->slider->setValue(position/1000);//当前是第几秒
ui->labPlayDurat->setText(timeDurat(position));
//显示歌词
ui->widget->showLyric(position);
}
//获取歌曲元数据
void MainWindow::on_metaDataAvailableChanged(bool available)
{
ui->widget->clearLyric();
if(!available) return;
ui->listWidget->setCurrentRow(playlist->currentIndex()+1);
QString title=player->metaData("Title").toString();
QString author=player->metaData("Author").toString();
ui->labMusicInfo->setText(QStringLiteral("歌曲名:")+
title+QStringLiteral("\t艺术家:")+author);
ui->labDurat->setText(timeDurat(player->duration()));
//加载歌词文件
QString lrcPath=player->currentMedia().canonicalUrl().toLocalFile();
QString lrcName=lrcPath.remove(lrcPath.right(3))+"lrc";
//qDebug()<<"lyricHandle";
ui->widget->LoadLyric(lrcName);
}
QString MainWindow::timeDurat(const qint64 &minsec)
{
int minuit=minsec/1000/60;
int sec=minsec/1000%60;
return QString("%1:%2").arg(minuit,2,10,QLatin1Char('0')).
arg(sec,2,10,QLatin1Char('0'));
}
//slider调进度 不能用valueChanged
void MainWindow::on_slider_sliderMoved(int position)
{
player->setPosition(position*1000);
}
//播放暂停
void MainWindow::on_tbPlay_toggled(bool checked)
{
if(checked){
ui->tbPlay->setIcon(QIcon(":/images/images/pause.png"));
player->play();}
else{ui->tbPlay->setIcon(QIcon(":/images/images/play.png"));
player->pause();}
}
//下一曲
void MainWindow::on_actNext_triggered()
{
playlist->next();
}
//上一曲
void MainWindow::on_actPrev_triggered()
{
playlist->previous();
}
//playlist右键菜单
void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
Q_UNUSED(pos);
QMenu menu;
QAction *actOpen=menu.addAction("open path");
QAction *actRemoveList=menu.addAction("remove from playlist");
QAction *actRemoveFile=menu.addAction("remove from disk");
QAction *actClear=menu.addAction("clear playlist");
QAction *actRes=menu.exec(QCursor::pos());
if(actRes){
int currentRow=ui->listWidget->currentRow();
auto selectFirst=ui->listWidget->currentItem();
QString path=selectFirst->toolTip();//选中music的路径
if(selectFirst==nullptr) return;
if(actRes==actOpen){
//fromLocalFile可以解决路径中文
QDesktopServices::openUrl(QUrl::fromLocalFile(
path.left(path.lastIndexOf('/'))));
}else if(actRes==actRemoveList){
if(playlist->removeMedia(currentRow)){//删除成功
auto removeItem=ui->listWidget->takeItem(currentRow);
delete removeItem;
}
}else if(actRes==actRemoveFile){
QFile fe(path);
if(fe.remove()){//删除成功
playlist->removeMedia(currentRow);
auto removeItem=ui->listWidget->takeItem(currentRow);
delete removeItem;
}
}else if(actRes==actClear){
playlist->clear();
ui->listWidget->clear();
ui->labDurat->setText("00:00");
ui->labPlayDurat->setText("00:00");
ui->labMusicInfo->setText("Music Info");
}
}
}
//双击更换歌曲
void MainWindow::on_listWidget_doubleClicked(const QModelIndex &index)
{
Q_UNUSED(index);
auto selects=ui->listWidget->selectedItems();
if(selects.isEmpty()) return;
//qDebug()<<selectFirst->row();
playlist->setCurrentIndex(ui->listWidget->currentRow()-1);
}
//音量&静音
void MainWindow::on_tbVolum_toggled(bool checked)
{
if(checked)
{
ui->tbVolum->setIcon(QIcon(":/images/images/minvolume.png"));
player->setVolume(0);
}else{
ui->tbVolum->setIcon(QIcon(":/images/images/maxvolume.png"));
player->setVolume(50);
} ;
}
void MainWindow::on_pbLrc_clicked()
{
lrcSub->isHidden()?lrcSub->show():lrcSub->hide();
}
void MainWindow::on_actBack_triggered()
{
QString imgPath=QFileDialog::getOpenFileName(this,"select picture",".",
"image(*.png *.jpg *.jpeg)");
if(imgPath.isEmpty()) return;
bgPath=imgPath;
}
void MainWindow::on_tbplayModel_clicked()
{
switch (playMode){
case loopList:
ui->tbplayModel->setIcon(QIcon(":/images/images/order.png"));
playlist->setPlaybackMode(QMediaPlaylist::Loop);
playMode=loopSingle;
break;
case loopSingle:
ui->tbplayModel->setIcon(QIcon(":/images/images/single.png"));
playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
playMode=random;
break;
case random:
ui->tbplayModel->setIcon(QIcon(":/images/images/random.png"));
playlist->setPlaybackMode(QMediaPlaylist::Random);
playMode=loopList;
break;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/Ollivandergitee/msc-player.git
git@gitee.com:Ollivandergitee/msc-player.git
Ollivandergitee
msc-player
Qt-mscPlayer
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385