fold all codes

qt实现的一个简单的musicplayer

/qt/musicplayer/main.cpp
#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setApplicationName("musicplayer");

    Widget w;
    w.show();

    return a.exec();
}
/qt/musicplayer/widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QToolButton>
#include <Phonon/VideoPlayer>
#include <Phonon/MediaSource>
#include <QTimeLine>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
    void createUi();

private slots:
    void prev();
    void next();
    void pause();
    void play();
    void setTimeLabel(int s);

private:
    QVBoxLayout *layout;
    QLabel *nameLabel;
    QLabel *timeLabel;
    QHBoxLayout *buttonLayout;
    QToolButton *backwardButton;
    QToolButton *playButton;
    QToolButton *pauseButton;
    QToolButton *forwardButton;

    void loadDir();
    void loadSong(QString& s);
    void togglePlayPause();
    const QString PlayDir;

    bool isPaused;
    Phonon::VideoPlayer *player;
    QTimeLine *timeLine;

    QStringList songs;
    int totalSongs;
    int songId;
    int prevSongId;
};

#endif // WIDGET_H
/qt/musicplayer/widget.cpp
#include "widget.h"

#include <QStringList>
#include <QDir>

#include <ctime>

Widget::Widget(QWidget *parent)
    : QWidget(parent), PlayDir("d:\\music\\"), isPaused(false),
      songId(0), prevSongId(0)
{
    qsrand((uint) time(0));

    createUi();

    connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
    connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
    connect(backwardButton, SIGNAL(clicked()), this, SLOT(prev()));
    connect(forwardButton, SIGNAL(clicked()), this, SLOT(next()));

    player = new Phonon::VideoPlayer(Phonon::MusicCategory, parent);

    timeLine = new QTimeLine(1000000, parent);
    timeLine->setCurveShape(QTimeLine::LinearCurve);
    timeLine->setFrameRange(0, 1000000);
    connect(timeLine, SIGNAL(frameChanged(int)), this, SLOT(setTimeLabel(int)));

    loadDir();
    if (!totalSongs)
    {
        playButton->setEnabled(false);
        pauseButton->setEnabled(false);
        backwardButton->setEnabled(false);
        forwardButton->setEnabled(false);
    }
    else
    {
        songId = qrand() % totalSongs;
        play();
        connect(player, SIGNAL(finished()), this, SLOT(next()));
    }
}

Widget::~Widget()
{
}

void Widget::loadDir()
{
    QDir dir(PlayDir);
    QStringList filters;
    filters << "*.mp3";
    songs = dir.entryList(filters, QDir::Files);
    totalSongs = songs.count();
}

void Widget::loadSong(QString& s)
{
    player->load(Phonon::MediaSource(PlayDir + s));
    nameLabel->setText(s.left(s.size() - 4));
    timeLine->stop();
}

void Widget::togglePlayPause()
{
    playButton->setHidden(!isPaused);
    pauseButton->setHidden(isPaused);
}

void Widget::setTimeLabel(int s)
{
    int s1 = player->currentTime() / 1000;
    int s2 = player->totalTime() / 1000;
    int m1 = s1 / 60;
    int m2 = s2 / 60;
    timeLabel->setText(tr("%1:%2 / %3:%4").arg(m1).arg(s1 % 60).arg(m2).arg(s2 % 60));
}

void Widget::play()
{
    if (!isPaused)
        loadSong(songs[songId]);

    if (timeLine->state() == QTimeLine::Paused)
        timeLine->resume();
    else
        timeLine->start();

    isPaused = false;
    togglePlayPause();
    player->play();
}

void Widget::pause()
{
    timeLine->setPaused(true);

    isPaused = true;
    togglePlayPause();
    player->pause();
}

void Widget::next()
{
    prevSongId = songId;
    songId = qrand() % totalSongs;

    isPaused = false;
    play();
}

void Widget::prev()
{
    songId = prevSongId;
    prevSongId = 0;

    isPaused = false;
    play();
}

void Widget::createUi()
{
    resize(300, 100);
    setMinimumSize(QSize(300, 100));
    setMaximumSize(QSize(300, 100));

    layout = new QVBoxLayout;
    layout->setSpacing(6);
    layout->setContentsMargins(11, 11, 11, 11);

    nameLabel = new QLabel;
    QFont nameFont;
    nameFont.setPointSize(10);
    nameLabel->setFont(nameFont);
    layout->addWidget(nameLabel);

    timeLabel = new QLabel;
    QFont timeFont;
    timeFont.setPointSize(9);
    timeLabel->setFont(timeFont);
    layout->addWidget(timeLabel);

    buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(6);

    backwardButton = new QToolButton;
    playButton = new QToolButton;
    pauseButton = new QToolButton;
    forwardButton = new QToolButton;
    backwardButton->setIcon(QIcon(QString(":/icon/backward.png")));
    playButton->setIcon(QIcon(QString(":/icon/play.png")));
    pauseButton->setIcon(QIcon(QString(":/icon/pause.png")));
    forwardButton->setIcon(QIcon(QString(":/icon/forward.png")));
    buttonLayout->addWidget(backwardButton);
    buttonLayout->addWidget(playButton);
    buttonLayout->addWidget(pauseButton);
    buttonLayout->addWidget(forwardButton);
    pauseButton->setHidden(true);

    buttonLayout->addStretch();
    layout->addLayout(buttonLayout);
    layout->addStretch();
    setLayout(layout);

    setWindowIcon(QIcon(QString(":/icon/play.png")));
    setWindowTitle(tr("musicplayer"));
}
posted @ 2011-11-03 11:59  hylent  阅读(1525)  评论(0编辑  收藏  举报