17 QPushButton* pcmdOpen =
new QPushButton(
"&Open");
18 QDial* pdiaVolume =
new QDial;
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;
27 m_pcmdPlay->setEnabled(
false);
28 m_pcmdPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
30 m_pcmdStop->setEnabled(
false);
31 m_pcmdStop->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
33 m_psldPosition->setRange(0, 0);
34 m_psldPosition->setOrientation(Qt::Horizontal);
36 pdiaVolume->setRange(0, 100);
37 int nDefaultVolume = 50;
38 m_pmp->setVolume(nDefaultVolume);
39 pdiaVolume->setValue(nDefaultVolume);
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))
48 connect(m_psldPosition, SIGNAL(sliderMoved(
int)),
49 SLOT(slotSetMediaPosition(
int))
52 connect(
m_pmp, SIGNAL(positionChanged(qint64)),
53 SLOT(slotSetSliderPosition(qint64))
55 connect(
m_pmp, SIGNAL(durationChanged(qint64)),
56 SLOT(slotSetDuration(qint64))
58 connect(
m_pmp, SIGNAL(stateChanged(QMediaPlayer::State)),
59 SLOT(slotStatusChanged(QMediaPlayer::State))
63 QHBoxLayout* phbxPlayControls =
new QHBoxLayout;
64 phbxPlayControls->addWidget(pcmdOpen);
65 phbxPlayControls->addWidget(m_pcmdPlay);
66 phbxPlayControls->addWidget(m_pcmdStop);
67 phbxPlayControls->addWidget(pdiaVolume);
69 QHBoxLayout* phbxTimeControls =
new QHBoxLayout;
70 phbxTimeControls->addWidget(m_plblCurrent);
71 phbxTimeControls->addWidget(m_psldPosition);
72 phbxTimeControls->addWidget(m_plblRemain);
82 void SoundPlayer::slotOpen()
84 QString strFile = QFileDialog::getOpenFileName(
this,
87 if (!strFile.isEmpty()) {
88 m_pmp->setMedia(QUrl::fromLocalFile(strFile));
89 m_pcmdPlay->setEnabled(
true);
90 m_pcmdStop->setEnabled(
true);
95 void SoundPlayer::slotPlay()
97 switch(
m_pmp->state()) {
98 case QMediaPlayer::PlayingState:
108 void SoundPlayer::slotSetMediaPosition(
int n)
110 m_pmp->setPosition(n);
114 void SoundPlayer::slotSetDuration(qint64 n)
116 m_psldPosition->setRange(0, n);
117 m_plblCurrent->setText(msecsToString(0));
118 m_plblRemain->setText(msecsToString(n));
122 void SoundPlayer::slotStatusChanged(QMediaPlayer::State state)
125 case QMediaPlayer::PlayingState:
126 m_pcmdPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
129 m_pcmdPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
135 void SoundPlayer::slotSetSliderPosition(qint64 n)
137 m_psldPosition->setValue(n);
139 int nDuration = m_psldPosition->maximum();
140 m_plblCurrent->setText(msecsToString(n));
141 m_plblRemain->setText(msecsToString(nDuration - n));
145 QString SoundPlayer::msecsToString(qint64 n)
147 int nHours = (n / (60 * 60 * 1000));
148 int nMinutes = ((n % (60 * 60 * 1000)) / (60 * 1000));
149 int nSeconds = ((n % (60 * 1000)) / 1000);
151 return QTime(nHours, nMinutes, nSeconds).toString(
"hh:mm:ss");
SoundPlayer(QWidget *pwgt=0)
QVBoxLayout * m_pvbxMainLayout