一、前言
在用Qt写一个简单的音乐播放器(一):使用QMediaPlayer播放音乐中,我们已经知道如何去使用QMediaPlayer播放音乐。
在用Qt写一个简单的音乐播放器(二):增加界面(开始和暂停音乐)中,我们增加了播放音乐/暂停音乐的按钮。
在用Qt写一个简单的音乐播放器(三):增加界面(播放跳转与音量控制)中,我们加入了播放控制和音量控制。
在用Qt写一个简单的音乐播放器(四):歌曲浏览、上一曲、下一曲中,我们增加了歌曲选择的功能。
这第五篇就来谈谈一个简单的问题,显示歌曲的时长和当前播放的时间:
二、控件
这次控件很简单,选两个label,分别重命名为lblMusicTime(总时长)、lblPlayTime(播放时长),如图所示:
三、代码
1.mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QtMultimedia/QMediaPlayer> // 包含头文件#include <QTimer>#include <QListWidgetItem>#include <QString>#include <QFileDialog>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();QString settime(int time);private slots:void on_btnPlay_clicked();void on_hSliderPlayProgress_sliderMoved(int position);void setSliderValue();void on_vSliderVolume_sliderMoved(int position);void on_lwMusicList_itemDoubleClicked(QListWidgetItem *item);void on_btnAddMusic_clicked();void on_btnPreMusic_clicked();void on_btnNextMusic_clicked();void on_btnVolume_clicked();void on_vSliderVolume_sliderReleased();void getduration();void setPlayTime();private:Ui::MainWindow *ui;QMediaPlayer m_mediaPlayer; // 实例化对象QTimer *timer; //定时器QString m_playPath;int m_PlayRow;int playtime;};#endif // MAINWINDOW_H
2.mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);timer = new QTimer();timer->setInterval(2000);timer->start();ui->vSliderVolume->setVisible(false);QString fileName = "D:/music";QDir dir(fileName);QStringList nameFilters;nameFilters << "*.mp3";QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);ui->lwMusicList->addItems(files);m_playPath = fileName;}MainWindow::~MainWindow(){delete ui;}void MainWindow::on_btnPlay_clicked(){if(QMediaPlayer:: PlayingState == m_mediaPlayer.state()){m_mediaPlayer.pause();ui->btnPlay->setText("Play");}else{m_mediaPlayer.play();ui->btnPlay->setText("Pause");}}void MainWindow::on_hSliderPlayProgress_sliderMoved(int position){m_mediaPlayer.setPosition(m_mediaPlayer.duration()*position/100);}void MainWindow::setSliderValue(){ui->hSliderPlayProgress->setValue(m_mediaPlayer.position()*100/m_mediaPlayer.duration());if(m_mediaPlayer.position()>=m_mediaPlayer.duration()){on_btnNextMusic_clicked();}}void MainWindow::on_vSliderVolume_sliderMoved(int position){m_mediaPlayer.setVolume(position);}void MainWindow::on_lwMusicList_itemDoubleClicked(QListWidgetItem *item){m_PlayRow = ui->lwMusicList->row(item);m_mediaPlayer.stop();m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));m_mediaPlayer.play();ui->hSliderPlayProgress->setValue(m_mediaPlayer.position());ui->btnPlay->setText("Pause");connect(timer, SIGNAL(timeout()), this, SLOT(setSliderValue()));connect(&m_mediaPlayer,SIGNAL(durationChanged(qint64)),this,SLOT(getduration()));connect(&m_mediaPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(setPlayTime()));}void MainWindow::on_btnAddMusic_clicked(){QString fileName = QFileDialog::getExistingDirectory(NULL,"Select Music Dir",".",NULL);QDir dir(fileName);QStringList nameFilters;nameFilters << "*.mp3";QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);ui->lwMusicList->addItems(files);m_playPath = fileName;}void MainWindow::on_btnPreMusic_clicked(){if(m_PlayRow == 0){m_PlayRow = ui->lwMusicList->count() - 1;}else{m_PlayRow--;}QListWidgetItem *item = ui->lwMusicList->item(m_PlayRow);item->setSelected(true);m_mediaPlayer.stop();m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));m_mediaPlayer.play();}void MainWindow::on_btnNextMusic_clicked(){if(m_PlayRow + 1 == ui->lwMusicList->count()){m_PlayRow = 0;}else{m_PlayRow++;}QListWidgetItem *item = ui->lwMusicList->item(m_PlayRow);item->setSelected(true);m_mediaPlayer.stop();m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));m_mediaPlayer.play();}void MainWindow::on_btnVolume_clicked(){if(ui->vSliderVolume->isVisible()){ui->vSliderVolume->setVisible(false);}else{ui->vSliderVolume->setVisible(true);}}void MainWindow::on_vSliderVolume_sliderReleased(){ui->vSliderVolume->setVisible(false);}QString MainWindow::settime(int time){int h,m,s;time /= 1000; //获得的时间是以毫秒为单位的h = time/3600;m = (time-h*3600)/60;s = time-h*3600-m*60;return QString("%1:%2:%3").arg(h).arg(m).arg(s);}void MainWindow::getduration(){playtime = m_mediaPlayer.duration();ui->lblMusicTime->setText(settime(playtime));}void MainWindow::setPlayTime(){ui->lblPlayTime->setText(settime(m_mediaPlayer.position()));}
四、效果
五、最后
歌词显示等功能敬请期待,今天上了一天班,有点累了,晚安 !
用Qt写一个简单的音乐播放器(六):显示歌词(正则表达式)