用QMediaPlayer实现MP3音乐播放器(C++Qt) qt实现音乐播放器

这个国庆又没闲着,利用中间这几天实现了一个简易的MP3音乐播放器,其音乐播放的核心功能是采用QtQMultimedia中的QMediaPlayer控件(网上使用Phonon框架的较多)。在实现的过程中,界面使用QtDesigner设计,这在很大程度上减小了C++代码量。时间紧迫,只实现了一些基本的功能,当然,要将播放器做得很完美,还是需要投入不少时间的。(一)开发环境:Windows 7/C++ Qt5.1 sdk/Qt Creator2.8(二)下载地址:最近更新:2013年12月08日源代码下载地址:http://vdisk.weibo.com/s/aMEiEcvN1Y1JF(三)基本功能:播放、暂停、停止、上一首、下一首、进度调节、列表循环、随机播放、单曲循环、顺序播放、音量调节、窗口移动、最小化到系统托盘、歌曲列表的显示与隐藏以及批量导入等功能。(四)播放器截图:
图(1):播放器主界面
图(2): 右键菜单界面
图(3):系统托盘图标界面(五)源代码分享:以下是MainWindow.h头文件:#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 

namespace Ui {
class MainWindow;
}

class QAction;
class QMenu;
class QMediaPlayer;
class QMediaPlaylist;
class QSystemTrayIcon;

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void positionChanged(qint64);
    void durationChanged(qint64);
    void playTo(int, int);
    void updateSongList(int);

    void showOrHideSongList();
    void importSongs();
    void playLast();
    void playOrPause();
    void playNext();

    void plusSound();
    void reduceSound();

    void setPlaybackMode1();
    void setPlaybackMode2();
    void setPlaybackMode3();
    void setPlaybackMode4();

    void support();
    void aboutUs();

    void setPosition(int);

    void iconActivated(QSystemTrayIcon::ActivationReason reason);

protected:
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void closeEvent(QCloseEvent *);
    
private:
    void createContextMenu();
    void createSystemTrayIcon();

    Ui::MainWindow *ui;

    QPoint dragPos;
    int volume;

    QAction *restoreAction;
    QAction *quitAction;
    QAction *seperatorAction1;
    QAction *seperatorAction2;
    QAction *seperatorAction3;
    QAction *seperatorAction4;
    QAction *seperatorAction5;

    QMenu *trayContextMenu;

    QMediaPlayer *player;
    QMediaPlaylist *playList;
    QSystemTrayIcon *trayIcon;
};

用QMediaPlayer实现MP3音乐播放器(C++Qt) qt实现音乐播放器
#endif // MAINWINDOW_H
以下是MainWindow.cpp实现文件:#include
#include 
#include 
#include 
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    ui->horizontalSlider->setRange(0, 0);
    ui->tableWidget->hide();
    ui->tableWidget->hideColumn(2);
    volume=80;

    createContextMenu();
    createSystemTrayIcon();

    playList=new QMediaPlaylist;
    playList->setPlaybackMode(QMediaPlaylist::Loop);
    player=new QMediaPlayer;
    player->setPlaylist(playList);
    player->setVolume(volume);

    connect(ui->horizontalSlider, SIGNAL(sliderMoved(int)), this, SLOT(setPosition(int)));
    connect(ui->tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(playTo(int, int)));

    connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
    connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
    connect(playList, SIGNAL(currentIndexChanged(int)), this, SLOT(updateSongList(int)));

    connect(ui->action_SongList, SIGNAL(triggered()), this, SLOT(showOrHideSongList()));
    connect(ui->action_Import, SIGNAL(triggered()), this, SLOT(importSongs()));
    connect(ui->action_Last, SIGNAL(triggered()), this, SLOT(playLast()));
    connect(ui->action_Play, SIGNAL(triggered()), this, SLOT(playOrPause()));
    connect(ui->action_Stop, SIGNAL(triggered()), player, SLOT(stop()));
    connect(ui->action_Next, SIGNAL(triggered()), this, SLOT(playNext()));
    connect(ui->action_SoundPlus, SIGNAL(triggered()), this, SLOT(plusSound()));
    connect(ui->action_SoundReduce, SIGNAL(triggered()), this, SLOT(reduceSound()));
    connect(ui->action_Mode1, SIGNAL(triggered()), this, SLOT(setPlaybackMode1()));
    connect(ui->action_Mode2, SIGNAL(triggered()), this, SLOT(setPlaybackMode2()));
    connect(ui->action_Mode3, SIGNAL(triggered()), this, SLOT(setPlaybackMode3()));
    connect(ui->action_Mode4, SIGNAL(triggered()), this, SLOT(setPlaybackMode4()));
    connect(ui->action_Support, SIGNAL(triggered()), this, SLOT(support()));
    connect(ui->action_About, SIGNAL(triggered()), this, SLOT(aboutUs()));
    connect(ui->action_Quit, SIGNAL(triggered()), this, SLOT(close()));

    connect(ui->toolButton_Last, SIGNAL(clicked()), this, SLOT(playLast()));
    connect(ui->toolButton_Play, SIGNAL(clicked()), this, SLOT(playOrPause()));
    connect(ui->toolButton_Stop, SIGNAL(clicked()), player, SLOT(stop()));
    connect(ui->toolButton_Next, SIGNAL(clicked()), this, SLOT(playNext()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createContextMenu()
{
    seperatorAction1=new QAction(this);
    seperatorAction1->setSeparator(true);
    seperatorAction2=new QAction(this);
    seperatorAction2->setSeparator(true);
    seperatorAction3=new QAction(this);
    seperatorAction3->setSeparator(true);
    seperatorAction4=new QAction(this);
    seperatorAction4->setSeparator(true);
    seperatorAction5=new QAction(this);
    seperatorAction5->setSeparator(true);

    addAction(ui->action_SongList);
    addAction(ui->action_Import);
    addAction(seperatorAction1);
    addAction(ui->action_Last);
    addAction(ui->action_Play);
    addAction(ui->action_Stop);
    addAction(ui->action_Next);
    addAction(seperatorAction2);
    addAction(ui->action_Mode1);
    addAction(ui->action_Mode2);
    addAction(ui->action_Mode3);
    addAction(ui->action_Mode4);
    addAction(seperatorAction3);
    addAction(ui->action_SoundPlus);
    addAction(ui->action_SoundReduce);
    addAction(seperatorAction4);
    addAction(ui->action_Support);
    addAction(ui->action_About);
    addAction(seperatorAction5);
    addAction(ui->action_Quit);
    setContextMenuPolicy(Qt::ActionsContextMenu);
}

void MainWindow::createSystemTrayIcon()
{
    trayIcon=new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(tr(":/new/background/images/icon.ico")));
    trayIcon->setToolTip(tr("酷音听听 - 音乐播放器"));

    restoreAction=new QAction(tr("打开主面板"), this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(show()));

    quitAction=new QAction(tr("退出"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

    trayContextMenu=new QMenu(this);
    trayContextMenu->addAction(ui->action_SongList);
    trayContextMenu->addAction(ui->action_Import);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(ui->action_Last);
    trayContextMenu->addAction(ui->action_Play);
    trayContextMenu->addAction(ui->action_Stop);
    trayContextMenu->addAction(ui->action_Next);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(ui->action_Mode1);
    trayContextMenu->addAction(ui->action_Mode2);
    trayContextMenu->addAction(ui->action_Mode3);
    trayContextMenu->addAction(ui->action_Mode4);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(ui->action_SoundPlus);
    trayContextMenu->addAction(ui->action_SoundReduce);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(ui->action_Support);
    trayContextMenu->addAction(ui->action_About);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(restoreAction);
    trayContextMenu->addAction(quitAction);
    trayIcon->setContextMenu(trayContextMenu);

    trayIcon->show();
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), 
            this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}

void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason) {
    case QSystemTrayIcon::DoubleClick:
    case QSystemTrayIcon::Trigger:
        if(this->isVisible()==true) {
            ;
        } else {
            this->show();
            this->activateWindow();
        }
        break;
    default:
        break;
    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(trayIcon->isVisible()) {
        hide();
        trayIcon->showMessage(tr("提示"), tr("酷音听听将在后台运行"));
        event->ignore();
    } else {
        event->accept();
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton) {
        dragPos=event->globalPos()-frameGeometry().topLeft();
        event->accept();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton) {
        move(event->globalPos()-dragPos);
        event->accept();
    }
}

void MainWindow::positionChanged(qint64 position)
{
    ui->horizontalSlider->setValue(position);
}

void MainWindow::durationChanged(qint64 duration)
{
    ui->horizontalSlider->setRange(0, duration);
}

void MainWindow::updateSongList(int i)
{
    ui->tableWidget->selectRow(i);
    ui->label->setText(tr("正在播放: %1").arg(ui->tableWidget->item(i, 0)->text()));
}

void MainWindow::showOrHideSongList()
{
    if(ui->tableWidget->isHidden()) {
        ui->tableWidget->show();
    } else {
        ui->tableWidget->hide();
    }
}

void MainWindow::playTo(int i, int )
{
    playList->setCurrentIndex(i);
    player->play();
}

void MainWindow::importSongs()
{
    QString initialName=QDir::homePath();
    QStringList pathList=QFileDialog::getOpenFileNames(this, tr("选择文件"), initialName, tr(".mp3"));
    for(int i=0; i<</SPAN>pathList.size(); ++i) {
        QString path=QDir::toNativeSeparators(pathList.at(i));
        if(!path.isEmpty()) {
            playList->addMedia(QUrl::fromLocalFile(path));
            QString fileName=path.split("\").last();
            int rownum=ui->tableWidget->rowCount();
            ui->tableWidget->insertRow(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));
        }
    }
}

void MainWindow::playLast()
{
    int currentIndex=playList->currentIndex();
    if(--currentIndex<</SPAN>0) currentIndex=0;
    playList->setCurrentIndex(currentIndex);
    player->play();
}

void MainWindow::playOrPause()
{
    if(ui->toolButton_Play->text()==tr("播放")) {
        player->play();
        ui->toolButton_Play->setText(tr("暂停"));
    } else {
        player->pause();
        ui->toolButton_Play->setText(tr("播放"));
    }
}

void MainWindow::playNext()
{
    int currentIndex=playList->currentIndex();
    if(++currentIndex==playList->mediaCount()) currentIndex=0;
    playList->setCurrentIndex(currentIndex);
    player->play();
}

void MainWindow::plusSound()
{
    volume+=20;
    if(volume>=100) {
        volume=100;
        ui->action_SoundPlus->setEnabled(false);
    }
    player->setVolume(volume);

    if(!ui->action_SoundReduce->isEnabled())
        ui->action_SoundReduce->setEnabled(true);
}

void MainWindow::reduceSound()
{
    volume-=20;
    if(volume<=0) {
        volume=0;
        ui->action_SoundReduce->setEnabled(false);
    }
    player->setVolume(volume);

    if(!ui->action_SoundPlus->isEnabled())
        ui->action_SoundPlus->setEnabled(true);
}

void MainWindow::setPosition(int position)
{
    player->setPosition(position);
}

void MainWindow::setPlaybackMode1()
{
    playList->setPlaybackMode(QMediaPlaylist::Loop);
}

void MainWindow::setPlaybackMode2()
{
    playList->setPlaybackMode(QMediaPlaylist::Random);
}

void MainWindow::setPlaybackMode3()
{
    playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
}

void MainWindow::setPlaybackMode4()
{
    playList->setPlaybackMode(QMediaPlaylist::Sequential);
}

void MainWindow::support()
{
    QMessageBox::about(this, tr("关于酷音听听"), 
                       tr("酷音听听,感谢您的使用!"));
}

void MainWindow::aboutUs()
{
    const QUrl url("http://blog.sina.com.cn/terrynotes");
    QDesktopServices::openUrl(url);
}
以下是main.cpp文件:#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}
由于最近工作非常繁忙,播放器程序就先试验到此了,主要是为了学习不规则窗体UI的设计,UI是一门很高深的学问,要做好还要下相当大的功夫。

  

爱华网本文地址 » http://www.aihuau.com/a/25101012/134530.html

更多阅读

鼻毛修剪器好用吗?鼻毛修剪器哪个好? 鼻毛修剪器什么牌子好

鼻毛修剪器好用吗?鼻毛修剪器哪个好?——简介如果某天突然一两根鼻毛悄悄的暴露在阳光下的时候,糟糕的是自己肯定不会注意到,但别人一定会看到,这对形象无疑是一个严骏的考验,为了防患于未然,建议大家可以选购一个鼻毛修剪器,鼻毛修剪器采用

制作MP3音乐伴奏带 北国之春伴奏带

? 假如你遇到喜欢的音乐 或者因为要演出找不到伴奏版,那该着么办?没关系,这一节亲亲家园教你如何把自己一首喜欢的音乐,消除原唱改成伴奏版,而且还提供软件给大家下载哦: 现在小雨沙沙就和大家分享一下 怎么用软件修改MP3文件使原唱

MP3 DirectCut让你轻松剪辑MP3音乐 mp3directcut教程

MP3 DirectCut让你轻松剪辑MP3音乐随着手机硬件的发展,昔日被看做体积庞大的MP3铃声已经逐渐成为标准铃声格式。而MP3格式的音乐文件更是随处可见,但一首3-4分钟的MP3可能就要5MB了,我们必须要对其进行处理后才适合作为MP3铃声使用。Co

在新浪博客文章中添加MP3音乐的方法 新浪博客发表文章过多

(原文地址:在新浪博客文章中添加MP3音乐的方法作者:龙的兰)新浪博客现在不支持直接上传音乐文件,但是支持超链接功能,就是可以是利用添加代码来实现这个功能。在编辑文章时钩选下方的“显示源代码”把音乐播放代码粘贴到最下面即可完成。(

怎样查找mp3音乐地址 歌曲连接软件

2008-05-20 09:28:26|分类: 博客制作(音乐类) |标签: |字号大中小订阅⊙ 怎样查找mp3音乐地址? ⊙寒冬腊月的博客学堂,将向博客新手详细解讲制作过程http://hdly006502.blog.163.com.撰文:寒冬腊月其实,查找mp3歌曲链接地址有很多种方

声明:《用QMediaPlayer实现MP3音乐播放器(C++Qt) qt实现音乐播放器》为网友哥没被超越分享!如侵犯到您的合法权益请联系我们删除