Qt 5.10 Book Examples
SoundPlayer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // SoundPlayer.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include "SoundPlayer.h"
13 
14 // ----------------------------------------------------------------------
15 SoundPlayer::SoundPlayer(QWidget* pwgt/*=0*/) : QWidget(pwgt)
16 {
17  QPushButton* pcmdOpen = new QPushButton("&Open");
18  QDial* pdiaVolume = new QDial;
19 
20  m_pcmdPlay = new QPushButton;
21  m_pcmdStop = new QPushButton;
22  m_psldPosition = new QSlider;
23  m_plblCurrent = new QLabel(msecsToString(0));
24  m_plblRemain = new QLabel(msecsToString(0));
25  m_pmp = new QMediaPlayer;
26 
27  m_pcmdPlay->setEnabled(false);
28  m_pcmdPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
29 
30  m_pcmdStop->setEnabled(false);
31  m_pcmdStop->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
32 
33  m_psldPosition->setRange(0, 0);
34  m_psldPosition->setOrientation(Qt::Horizontal);
35 
36  pdiaVolume->setRange(0, 100);
37  int nDefaultVolume = 50;
38  m_pmp->setVolume(nDefaultVolume);
39  pdiaVolume->setValue(nDefaultVolume);
40 
41  connect(pcmdOpen, SIGNAL(clicked()), SLOT(slotOpen()));
42  connect(m_pcmdPlay, SIGNAL(clicked()), SLOT(slotPlay()));
43  connect(m_pcmdStop, SIGNAL(clicked()), m_pmp, SLOT(stop()));
44  connect(pdiaVolume, SIGNAL(valueChanged(int)),
45  m_pmp, SLOT(setVolume(int))
46  );
47 
48  connect(m_psldPosition, SIGNAL(sliderMoved(int)),
49  SLOT(slotSetMediaPosition(int))
50  );
51 
52  connect(m_pmp, SIGNAL(positionChanged(qint64)),
53  SLOT(slotSetSliderPosition(qint64))
54  );
55  connect(m_pmp, SIGNAL(durationChanged(qint64)),
56  SLOT(slotSetDuration(qint64))
57  );
58  connect(m_pmp, SIGNAL(stateChanged(QMediaPlayer::State)),
59  SLOT(slotStatusChanged(QMediaPlayer::State))
60  );
61 
62  //Layout setup
63  QHBoxLayout* phbxPlayControls = new QHBoxLayout;
64  phbxPlayControls->addWidget(pcmdOpen);
65  phbxPlayControls->addWidget(m_pcmdPlay);
66  phbxPlayControls->addWidget(m_pcmdStop);
67  phbxPlayControls->addWidget(pdiaVolume);
68 
69  QHBoxLayout* phbxTimeControls = new QHBoxLayout;
70  phbxTimeControls->addWidget(m_plblCurrent);
71  phbxTimeControls->addWidget(m_psldPosition);
72  phbxTimeControls->addWidget(m_plblRemain);
73 
74  m_pvbxMainLayout = new QVBoxLayout;
75  m_pvbxMainLayout->addLayout(phbxPlayControls);
76  m_pvbxMainLayout->addLayout(phbxTimeControls);
77 
78  setLayout(m_pvbxMainLayout);
79 }
80 
81 // ----------------------------------------------------------------------
82 void SoundPlayer::slotOpen()
83 {
84  QString strFile = QFileDialog::getOpenFileName(this,
85  "Open File"
86  );
87  if (!strFile.isEmpty()) {
88  m_pmp->setMedia(QUrl::fromLocalFile(strFile));
89  m_pcmdPlay->setEnabled(true);
90  m_pcmdStop->setEnabled(true);
91  }
92 }
93 
94 // ----------------------------------------------------------------------
95 void SoundPlayer::slotPlay()
96 {
97  switch(m_pmp->state()) {
98  case QMediaPlayer::PlayingState:
99  m_pmp->pause();
100  break;
101  default:
102  m_pmp->play();
103  break;
104  }
105 }
106 
107 // ----------------------------------------------------------------------
108 void SoundPlayer::slotSetMediaPosition(int n)
109 {
110  m_pmp->setPosition(n);
111 }
112 
113 // ----------------------------------------------------------------------
114 void SoundPlayer::slotSetDuration(qint64 n)
115 {
116  m_psldPosition->setRange(0, n);
117  m_plblCurrent->setText(msecsToString(0));
118  m_plblRemain->setText(msecsToString(n));
119 }
120 
121 // ----------------------------------------------------------------------
122 void SoundPlayer::slotStatusChanged(QMediaPlayer::State state)
123 {
124  switch(state) {
125  case QMediaPlayer::PlayingState:
126  m_pcmdPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
127  break;
128  default:
129  m_pcmdPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
130  break;
131  }
132 }
133 
134 // ----------------------------------------------------------------------
135 void SoundPlayer::slotSetSliderPosition(qint64 n)
136 {
137  m_psldPosition->setValue(n);
138 
139  int nDuration = m_psldPosition->maximum();
140  m_plblCurrent->setText(msecsToString(n));
141  m_plblRemain->setText(msecsToString(nDuration - n));
142 }
143 
144 // ----------------------------------------------------------------------
145 QString SoundPlayer::msecsToString(qint64 n)
146 {
147  int nHours = (n / (60 * 60 * 1000));
148  int nMinutes = ((n % (60 * 60 * 1000)) / (60 * 1000));
149  int nSeconds = ((n % (60 * 1000)) / 1000);
150 
151  return QTime(nHours, nMinutes, nSeconds).toString("hh:mm:ss");
152 }
153 
SoundPlayer(QWidget *pwgt=0)
Definition: SoundPlayer.cpp:15
QVBoxLayout * m_pvbxMainLayout
Definition: SoundPlayer.h:25
QMediaPlayer * m_pmp
Definition: SoundPlayer.h:24