社区新版论坛已上线,点击立即前往!使用 openKylin 账户授权登录,解锁更多体验!

openKylin论坛

 找回密码

qt实现的音乐播放 [复制链接]

  1. /qt/musicplayer/main.cpp

  2. #include <QtGui/QApplication>
  3. #include "widget.h"

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

  8.     Widget w;
  9.     w.show();

  10.     return a.exec();
  11. }

  12. /qt/musicplayer/widget.h

  13. #ifndef WIDGET_H
  14. #define WIDGET_H

  15. #include <QWidget>
  16. #include <QVBoxLayout>
  17. #include <QHBoxLayout>
  18. #include <QLabel>
  19. #include <QToolButton>
  20. #include <Phonon/VideoPlayer>
  21. #include <Phonon/MediaSource>
  22. #include <QTimeLine>

  23. class Widget : public QWidget
  24. {
  25.     Q_OBJECT

  26. public:
  27.     Widget(QWidget *parent = 0);
  28.     ~Widget();
  29.     void createUi();

  30. private slots:
  31.     void prev();
  32.     void next();
  33.     void pause();
  34.     void play();
  35.     void setTimeLabel(int s);

  36. private:
  37.     QVBoxLayout *layout;
  38.     QLabel *nameLabel;
  39.     QLabel *timeLabel;
  40.     QHBoxLayout *buttonLayout;
  41.     QToolButton *backwardButton;
  42.     QToolButton *playButton;
  43.     QToolButton *pauseButton;
  44.     QToolButton *forwardButton;

  45.     void loadDir();
  46.     void loadSong(QString& s);
  47.     void togglePlayPause();
  48.     const QString PlayDir;

  49.     bool isPaused;
  50.     Phonon::VideoPlayer *player;
  51.     QTimeLine *timeLine;

  52.     QStringList songs;
  53.     int totalSongs;
  54.     int songId;
  55.     int prevSongId;
  56. };

  57. #endif // WIDGET_H

  58. /qt/musicplayer/widget.cpp

  59. #include "widget.h"

  60. #include <QStringList>
  61. #include <QDir>

  62. #include <ctime>

  63. Widget::Widget(QWidget *parent)
  64.     : QWidget(parent), PlayDir("d:\\music\"), isPaused(false),
  65.       songId(0), prevSongId(0)
  66. {
  67.     qsrand((uint) time(0));

  68.     createUi();

  69.     connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
  70.     connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
  71.     connect(backwardButton, SIGNAL(clicked()), this, SLOT(prev()));
  72.     connect(forwardButton, SIGNAL(clicked()), this, SLOT(next()));

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

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

  78.     loadDir();
  79.     if (!totalSongs)
  80.     {
  81.         playButton->setEnabled(false);
  82.         pauseButton->setEnabled(false);
  83.         backwardButton->setEnabled(false);
  84.         forwardButton->setEnabled(false);
  85.     }
  86.     else
  87.     {
  88.         songId = qrand() % totalSongs;
  89.         play();
  90.         connect(player, SIGNAL(finished()), this, SLOT(next()));
  91.     }
  92. }

  93. Widget::~Widget()
  94. {
  95. }

  96. void Widget::loadDir()
  97. {
  98.     QDir dir(PlayDir);
  99.     QStringList filters;
  100.     filters << "*.mp3";
  101.     songs = dir.entryList(filters, QDir::Files);
  102.     totalSongs = songs.count();
  103. }

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

  110. void Widget::togglePlayPause()
  111. {
  112.     playButton->setHidden(!isPaused);
  113.     pauseButton->setHidden(isPaused);
  114. }

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

  123. void Widget::play()
  124. {
  125.     if (!isPaused)
  126.         loadSong(songs[songId]);

  127.     if (timeLine->state() == QTimeLine::Paused)
  128.         timeLine->resume();
  129.     else
  130.         timeLine->start();

  131.     isPaused = false;
  132.     togglePlayPause();
  133.     player->play();
  134. }

  135. void Widget::pause()
  136. {
  137.     timeLine->setPaused(true);

  138.     isPaused = true;
  139.     togglePlayPause();
  140.     player->pause();
  141. }

  142. void Widget::next()
  143. {
  144.     prevSongId = songId;
  145.     songId = qrand() % totalSongs;

  146.     isPaused = false;
  147.     play();
  148. }

  149. void Widget::prev()
  150. {
  151.     songId = prevSongId;
  152.     prevSongId = 0;

  153.     isPaused = false;
  154.     play();
  155. }

  156. void Widget::createUi()
  157. {
  158.     resize(300, 100);
  159.     setMinimumSize(QSize(300, 100));
  160.     setMaximumSize(QSize(300, 100));

  161.     layout = new QVBoxLayout;
  162.     layout->setSpacing(6);
  163.     layout->setContentsMargins(11, 11, 11, 11);

  164.     nameLabel = new QLabel;
  165.     QFont nameFont;
  166.     nameFont.setPointSize(10);
  167.     nameLabel->setFont(nameFont);
  168.     layout->addWidget(nameLabel);

  169.     timeLabel = new QLabel;
  170.     QFont timeFont;
  171.     timeFont.setPointSize(9);
  172.     timeLabel->setFont(timeFont);
  173.     layout->addWidget(timeLabel);

  174.     buttonLayout = new QHBoxLayout();
  175.     buttonLayout->setSpacing(6);

  176.     backwardButton = new QToolButton;
  177.     playButton = new QToolButton;
  178.     pauseButton = new QToolButton;
  179.     forwardButton = new QToolButton;
  180.     backwardButton->setIcon(QIcon(QString(":/icon/backward.png")));
  181.     playButton->setIcon(QIcon(QString(":/icon/play.png")));
  182.     pauseButton->setIcon(QIcon(QString(":/icon/pause.png")));
  183.     forwardButton->setIcon(QIcon(QString(":/icon/forward.png")));
  184.     buttonLayout->addWidget(backwardButton);
  185.     buttonLayout->addWidget(playButton);
  186.     buttonLayout->addWidget(pauseButton);
  187.     buttonLayout->addWidget(forwardButton);
  188.     pauseButton->setHidden(true);

  189.     buttonLayout->addStretch();
  190.     layout->addLayout(buttonLayout);
  191.     layout->addStretch();
  192.     setLayout(layout);

  193.     setWindowIcon(QIcon(QString(":/icon/play.png")));
  194.     setWindowTitle(tr("musicplayer"));
  195. }
复制代码
楼主
发表于 2013-8-1 21:38:59
回复

使用道具 举报

qt实现的音乐播放 [复制链接]

应该简要介绍一下qt是如何实现音乐播放的,也就是把实现思路介绍一下,这样便于别人参与讨论。把代码直接贴上,就石沉大海啦{:5_116:}
沙发
发表于 2013-8-2 08:33:02
回复

使用道具 举报

qt实现的音乐播放 [复制链接]

bunny 发表于 2013-8-2 08:33
应该简要介绍一下qt是如何实现音乐播放的,也就是把实现思路介绍一下,这样便于别人参与讨论。把代码直接贴 ...

{:3_52:}就是 就是!O(∩_∩)O哈哈~
板凳
发表于 2013-8-2 15:52:41
回复

使用道具 举报

qt实现的音乐播放 [复制链接]

Jiaowen520Li 发表于 2013-8-2 15:52
就是 就是!O(∩_∩)O哈哈~

瞎起哄,,恨世不乱
地板
发表于 2013-11-16 09:21:36
回复

使用道具 举报

qt实现的音乐播放 [复制链接]

笨鸟弟弟 发表于 2013-11-16 09:21
瞎起哄,,恨世不乱

木有吧。。呵呵~
5#
发表于 2013-11-16 23:10:16
回复

使用道具 举报

qt实现的音乐播放 [复制链接]

Jiaowen520Li 发表于 2013-11-16 23:10
木有吧。。呵呵~

วางจากที่อื่นอธิบายให้คุณสิ่งที่วิธี
6#
发表于 2013-11-17 07:34:40
回复

使用道具 举报

qt实现的音乐播放 [复制链接]

笨鸟弟弟 发表于 2013-11-17 07:34
วางจากที่อื่นอ ...

{:3_52:}求翻译!O(∩_∩)O~
7#
发表于 2013-11-17 09:48:28
回复

使用道具 举报

qt实现的音乐播放 [复制链接]

Jiaowen520Li 发表于 2013-11-17 09:48
求翻译!O(∩_∩)O~

不加解释,,,
8#
发表于 2013-11-17 10:37:28
回复

使用道具 举报

openKylin

GMT+8, 2024-7-3 06:04 , Processed in 0.024014 second(s), 18 queries , Gzip On.

Copyright ©2022 openKylin. All Rights Reserved .

ICP No. 15002470-12 Tianjin

快速回复 返回顶部 返回列表