mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void init();
void on_btnAdd_clicked();
void on_btnPlay_clicked();
void on_btnPause_clicked();
void on_btnStop_clicked();
void on_btnPrevious_clicked();
void on_btnNext_clicked();
void on_btnSound_clicked();
void on_sliderVolumn_valueChanged(int value);
void on_listWidget_doubleClicked(const QModelIndex &index);
void on_btnRemove_clicked();
void on_btnClear_clicked();
void on_sliderPosition_valueChanged(int value);
private:
Ui::MainWindow *ui;
QMediaPlayer *player;
QMediaPlaylist *playlist;
QString durationTime,positionTime;//总时间,当前位置的时间
private slots:
void onStateChanged(QMediaPlayer::State state);//播放状态
void onPlaylistChanged(int position);
void onDurationChanged(qint64 duration);
void onPositionChanged(qint64 position);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDir>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
player=new QMediaPlayer(this);
playlist=new QMediaPlaylist(this);
playlist->setPlaybackMode(QMediaPlaylist::Loop);//循环播放
player->setPlaylist(playlist);
connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),
this,SLOT(onStateChanged(QMediaPlayer::State)));
//状态改变
connect(player,SIGNAL(durationChanged(qint64)),
this,SLOT(onDurationChanged(qint64)));
//歌曲时长
connect(player,SIGNAL(positionChanged(qint64)),
this,SLOT(onPositionChanged(qint64)));
//位置
connect(playlist,SIGNAL(currentIndexChanged(int)),
this,SLOT(onPlaylistChanged(int)));
//当前索引改变
ui->listWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
}
void MainWindow::on_btnAdd_clicked()
{
QString curpath=QDir::homePath();
//qDebug()<<curpath;
QString dlgTitle="选择音频文件";
QString filter="mp3文件(*.mp3)";
QStringList fileList=QFileDialog::getOpenFileNames(this,dlgTitle,curpath,filter);
if(fileList.count()<1)return ;
for(int i=0;i<fileList.count();i++)
{
QString aFile=fileList.at(i);
playlist->addMedia(QUrl::fromLocalFile(aFile));
//qDebug()<<QUrl::fromLocalFile(aFile);
//qDebug()<<aFile;
QFileInfo fileInfo(aFile);
ui->listWidget->addItem(fileInfo.fileName());
//qDebug()<<fileInfo.fileName();
}
if(player->state()!=QMediaPlayer::PlayingState)
playlist->setCurrentIndex(0);
player->play();
}
void MainWindow::on_btnPlay_clicked()
{
if(playlist->currentIndex()<0)
playlist->setCurrentIndex(0);
player->play();
}
void MainWindow::on_btnPause_clicked()
{
player->pause();
}
void MainWindow::on_btnStop_clicked()
{
player->stop();
}
void MainWindow::on_btnPrevious_clicked()
{
playlist->previous();
player->play();
}
void MainWindow::on_btnNext_clicked()
{
playlist->next();
player->play();
}
void MainWindow::on_btnSound_clicked()
{
bool mute=player->isMuted();
player->setMuted(!mute);
if(mute){
ui->btnSound->setIcon(QIcon(":/images/images/volumn.bmp"));
}
else{
ui->btnSound->setIcon(QIcon(":/images/images/mute.bmp"));
}
}
void MainWindow::on_sliderVolumn_valueChanged(int value)
{
player->setVolume(value);
}
void MainWindow::on_listWidget_doubleClicked(const QModelIndex &index)
{
int rowNo=index.row();
//qDebug()<<index;
playlist->setCurrentIndex(rowNo);
player->play();
}
void MainWindow::on_btnRemove_clicked()
{
int pos=ui->listWidget->currentRow();
QListWidgetItem *item=ui->listWidget->takeItem(pos);
delete item;
playlist->removeMedia(pos);
}
void MainWindow::on_btnClear_clicked()
{
playlist->clear();
ui->listWidget->clear();
player->stop();
}
void MainWindow::on_sliderPosition_valueChanged(int value)
{
player->setPosition(value);//?
}
void MainWindow::onStateChanged(QMediaPlayer::State state)
{
ui->btnStop->setEnabled(state==QMediaPlayer::PlayingState);
ui->btnPause->setEnabled(state==QMediaPlayer::PlayingState);
ui->btnPlay->setEnabled(!(state==QMediaPlayer::PlayingState));
}
void MainWindow::onPlaylistChanged(int position)
{
//qDebug()<<position;
ui->listWidget->setCurrentRow(position);
QListWidgetItem *item=ui->listWidget->currentItem();
if(item)
ui->LabCurMedia->setText(item->text());
}
void MainWindow::onDurationChanged(qint64 duration)
{
ui->sliderPosition->setMaximum(duration);
int secs=duration/1000;
//qDebug()<<secs;
int mins=secs/60;
secs=secs%60;
durationTime=QString::asprintf("%d:%02d",mins,secs);
ui->LabRatio->setText(positionTime+"/"+durationTime);
}
void MainWindow::onPositionChanged(qint64 position)
{
if(ui->sliderPosition->isSliderDown())return ;
ui->sliderPosition->setSliderPosition(position);
int secs=position/1000;
int mins=secs/60;
secs=secs%60;
positionTime=QString::asprintf("%d:%02d",mins,secs);
ui->LabRatio->setText(positionTime+"/"+durationTime);
}
qss:文章来源:https://www.toymoban.com/news/detail-526661.html
QLabel{
font: 12pt "楷体";
}
QFrame#line_2{
color:rgb(68, 170, 232);
}
#groupBox{
font: 12pt "楷体";
}
QListView {
outline: none;
}
#listWidget{/*
border-style:solid;
border-color:rgb(69, 173, 237);
border-width:2px;*/
border:none;
}
#listWidget::item {
background-color: #ffffff;
color: #000000;
border: transparent;
border-bottom: 1px solid #dbdbdb;
padding: 8px;
}
#listWidget::item:hover {
background-color: #f5f5f5;
}
#listWidget::item:selected {
border-left: 5px solid rgb(69, 173, 237);
}
#centralWidget{
background-color: rgb(255, 255, 255);
}
QSlider{
border:none;
}
QSlider::groove:horizontal {
border: 1px solid #bbb;
background: white;
height: 8px;
border-radius: 4px;
}
QSlider::sub-page:horizontal {
background: rgb(69, 173, 237);
border: 1px solid #777;
height: 8px;
border-radius: 4px;
}
QSlider::add-page:horizontal {
background: #fff;
border: 1px solid #777;
height: 8px;
border-radius: 4px;
}
QSlider::handle:horizontal {
background: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,
stop:0.6 #45ADED, stop:0.778409 rgba(255, 255, 255, 255));
border: 1px solid #777;
border-radius:10px;
width: 20px;
margin-top: -6px;
margin-bottom: -6px;
}
QSlider::handle:horizontal:hover {
background: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0.6 #2A8BDA,
stop:0.778409 rgba(255, 255, 255, 255));
border: 1px solid #444;
border-radius:10px;
width: 20px;
margin-top: -6px;
margin-bottom: -6px;
}
QPushButton:hover{
border:2px solid rgb(69, 173, 237);
}
QPushButton{
font: 14pt "方正小标宋简体";
color:rgb(108,108,108);
min-width:150px;
max-width:150px;
min-height:50px;
max-height:50px;
border:2px solid rgb(214,214,214);
border-radius:20px;
}
QScrollBar:vertical{
margin:16px 0px 16px 0px;
background-color:rgb(255,255,255);
border:0px;
width:14px;
}
QScrollBar::handle:vertical{
background-color:rgb(69, 173, 237);
border-radius:7px;
width:13px;
}
QScrollBar::handle:vertical:hover{
background-color:rgb(42, 139, 218);
}
QScrollBar::sub-line:vertical{
subcontrol-position:top;
subcontrol-origin:margin;
background-color:rgb(255,255,255);
height:0px;
}
QScrollBar::add-line:vertical{
subcontrol-position:bottom;
subcontrol-origin:margin;
background-color:rgb(255,255,255);
height:0px;
}
QScrollBar::sub-page:vertical,QScrollBar::add-page:vertical{
background-color:rgb(255,255,255);
}
源码:Qt与学习通页面: 记录与Qt相关的代码 - Gitee.com文章来源地址https://www.toymoban.com/news/detail-526661.html
到了这里,关于Qt实现本地音乐播放器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!