Qt——音乐播放控件

界面效果

在这里插入图片描述

头文件

#ifndef MUSICPLAYERWIN_H
#define MUSICPLAYERWIN_H

#include "ui_musicplayerwin.h"
#include <QWidget>
#include <QMediaPlayer>

class QSlider;
class QMediaPlaylist;
class MusicPlayerWin : public QWidget,public Ui_MusicPlayerWin
{
    Q_OBJECT

public:
    explicit MusicPlayerWin(QWidget *parent = nullptr);
    ~MusicPlayerWin();

    void setMusicList(const QStringList &list);
    void addMusic(const QString &path);

signals:
    void currentChanged(int);
protected slots:
    void onPlayer();
    void onNextSong();
    void onUpSong();

    void onVolume();
    void onVolumeChanged(int val);
    void onProcessChanged(int val);

    void onPlayerPositionChanged(qint64 val);
    void onPlayerStatusChanged(QMediaPlayer::State state);
protected:
    void initialize();
    QString getTimeText(qint64 val);
private:
    QMediaPlaylist *m_pPlayerList = nullptr;
    QMediaPlayer *m_pPlayer = nullptr;
};

#endif // MUSICPLAYERWIN_H

源文件

#include "musicplayerwin.h"
#include <QSlider>
#include <QMediaPlaylist>
#include <QMediaMetaData>
#include <QUrl>


MusicPlayerWin::MusicPlayerWin(QWidget *parent) :
    QWidget(parent)
{
    setupUi(this);
    initialize();

    connect(m_pBtnUp,SIGNAL(clicked(bool)),this,SLOT(onUpSong()));
    connect(m_pBtnNext,SIGNAL(clicked(bool)),this,SLOT(onNextSong()));
    connect(m_pBtnPlayer,SIGNAL(clicked(bool)),this,SLOT(onPlayer()));
    connect(m_pBtnStop,SIGNAL(clicked(bool)),m_pPlayer,SLOT(stop()));

    connect(m_pSliderVolume,SIGNAL(valueChanged(int)),this,SLOT(onVolumeChanged(int)));
    connect(m_pSliderProcess,SIGNAL(valueChanged(int)),this,SLOT(onProcessChanged(int)));
    connect(m_pPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(onPlayerPositionChanged(qint64)));
    connect(m_pPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(onPlayerStatusChanged(QMediaPlayer::State)));
    connect(m_pPlayerList,SIGNAL(currentIndexChanged(int)),this,SIGNAL(currentChanged(int)));
}

MusicPlayerWin::~MusicPlayerWin()
{

}

void MusicPlayerWin::setMusicList(const QStringList &list)
{
    m_pPlayer->stop();
    m_pPlayerList->clear();
    m_pSliderProcess->setValue(0);

    foreach (QString path, list) {
        m_pPlayerList->addMedia(QUrl::fromLocalFile(path));
    }

    if(list.count() > 0){
        m_pPlayerList->setCurrentIndex(0);
        m_pPlayer->play();
    }
}

void MusicPlayerWin::addMusic(const QString &path)
{
    m_pPlayerList->addMedia(QUrl::fromLocalFile(path));
}

void MusicPlayerWin::onPlayer()
{
    if(m_pPlayerList->mediaCount() <= 0){
        return;
    }

    QMediaPlayer::State state = m_pPlayer->state();

    switch (state) {
    case QMediaPlayer::PlayingState:
    {
        m_pBtnPlayer->setText("Player");
        m_pPlayer->pause();
    }
        break;
    case QMediaPlayer::PausedState:
    {
        m_pBtnPlayer->setText("Pause");
        m_pPlayer->play();
    }
        break;
    case QMediaPlayer::StoppedState:
    {
        m_pBtnPlayer->setText("Pause");
        m_pPlayerList->setCurrentIndex(0);
        m_pPlayer->play();
    }
    default:
        break;
    }
}

void MusicPlayerWin::onNextSong()
{
    m_pPlayerList->setCurrentIndex(m_pPlayerList->nextIndex());
}

void MusicPlayerWin::onUpSong()
{
    m_pPlayerList->setCurrentIndex(m_pPlayerList->previousIndex());
}

void MusicPlayerWin::onVolume()
{
    if(m_pSliderVolume->isHidden()){
        m_pSliderVolume->show();
    }else {
        m_pSliderVolume->hide();
    }
}

void MusicPlayerWin::onVolumeChanged(int val)
{
    m_pPlayer->setVolume(val);
}

void MusicPlayerWin::onProcessChanged(int val)
{
    if(m_pPlayer->media().isNull()){
        return;
    }
    if(m_pPlayer->state() == QMediaPlayer::PausedState){
        m_pPlayer->setPosition(val);
    }
}

void MusicPlayerWin::onPlayerPositionChanged(qint64 val)
{
    qint64 duration = m_pPlayer->duration();
    m_pSliderProcess->setRange(0,duration);
    m_pSliderProcess->setValue(val);

    m_pLabInfoCurrent->setText(getTimeText(val));
    m_pLabInfoTotal->setText("/" + getTimeText(duration));
}

void MusicPlayerWin::onPlayerStatusChanged(QMediaPlayer::State state)
{
    switch (state) {
    case QMediaPlayer::StoppedState:
    {
        m_pBtnPlayer->setText("Play");
        m_pSliderProcess->setValue(0);
        m_pLabInfoCurrent->setText(getTimeText(0));
        m_pLabInfoTotal->setText("/" + getTimeText(0));
    }
        break;

    default:
        break;
    }
}

void MusicPlayerWin::initialize()
{
    m_pSliderVolume->setRange(0,100);
    m_pSliderVolume->setValue(50);

    m_pPlayerList = new QMediaPlaylist(this);

    m_pPlayer = new QMediaPlayer(this);
    m_pPlayer->setVolume(50);
    m_pPlayer->setPlaylist(m_pPlayerList);
}

QString MusicPlayerWin::getTimeText(qint64 val)
{
    val /= 1000;

    int min = val / 60;
    int sec = val - min * 60;

    QString text;

    if(min < 10){
        text += ("0" + QString::number(min));
    }else {
        text += QString::number(min);
    }

    text += ":";

    if(sec < 10){
        text += ("0" + QString::number(sec));
    }else {
        text += QString::number(sec);
    }

    return text;
}

UI文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MusicPlayerWin</class>
 <widget class="QWidget" name="MusicPlayerWin">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>785</width>
    <height>41</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout">
   <item>
    <widget class="QSlider" name="m_pSliderProcess">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QLabel" name="m_pLabInfoCurrent">
     <property name="minimumSize">
      <size>
       <width>50</width>
       <height>0</height>
      </size>
     </property>
     <property name="maximumSize">
      <size>
       <width>50</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="text">
      <string>00:00</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QLabel" name="m_pLabInfoTotal">
     <property name="minimumSize">
      <size>
       <width>50</width>
       <height>0</height>
      </size>
     </property>
     <property name="maximumSize">
      <size>
       <width>50</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="text">
      <string>/00:00</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QLabel" name="label">
     <property name="maximumSize">
      <size>
       <width>100</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="text">
      <string>voluem:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QSlider" name="m_pSliderVolume">
     <property name="maximumSize">
      <size>
       <width>100</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="m_pBtnUp">
     <property name="maximumSize">
      <size>
       <width>50</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="text">
      <string>up</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="m_pBtnPlayer">
     <property name="maximumSize">
      <size>
       <width>50</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="text">
      <string>Play</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="m_pBtnNext">
     <property name="maximumSize">
      <size>
       <width>50</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="text">
      <string>next</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="m_pBtnStop">
     <property name="maximumSize">
      <size>
       <width>50</width>
       <height>16777215</height>
      </size>
     </property>
     <property name="text">
      <string>stop</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值