我正在使用 Qt 创建一个音频播放器,并且我正在实现它,当我加载一首歌曲时它会自动启动,但是当我调用启动歌曲的函数并计算持续时间时,歌曲不会开始。我使用了调试,当我到达播放功能时,调试在结束步进范围内停止。我也尝试过发出信号但无事可做,只有按下播放按钮时歌曲才会开始。
这里有代码
void AudioPlayer::loadClicked()
{
QString filename = QFileDialog::getOpenFileName(this, "Select an audio file", "C:/Users/belli/Music" ,"File Mp3 (*.mp3)");
if(!filename.isEmpty())
{
qDebug("%s", filename.toLatin1().constData());
player.setMedia(QUrl::fromLocalFile(filename));
this->volumeSliderMoved();
QFileInfo fi(filename);
QString name = fi.fileName();
ui->label->setText(name);
this->playClicked();
}
}
void AudioPlayer::playClicked()
{
player.play();
songLength = player.duration();
ui->horizontalSliderPosition->setRange(0, songLength);
}
void AudioPlayer::stopClicked()
{
player.stop();
ui->horizontalSliderPosition->setValue(0);
}
void AudioPlayer::playerPositionChanged(qint64 pos)
{
ui->horizontalSliderPosition->setValue(pos);
if(pos == songLength)
this->stopClicked();
}
void AudioPlayer::volumeSliderMoved()
{
player.setVolume(ui->horizontalSliderVolume->value());
}
void AudioPlayer::positionSliderMoved()
{
player.setPosition((quint64) ui->horizontalSliderPosition->value());
}
和连接
ui(new Ui::AudioPlayer)
{
ui->setupUi(this);
connect(ui->pushButtonLoad, SIGNAL(clicked(bool)), SLOT(loadClicked()));
connect(ui->pushButtonPlay, SIGNAL(clicked(bool)), SLOT(playClicked()));
connect(ui->pushButtonStop, SIGNAL(clicked(bool)), SLOT(stopClicked()));
connect(ui->horizontalSliderVolume, SIGNAL(valueChanged(int)), SLOT(volumeSliderMoved()));
connect(ui->horizontalSliderPosition, SIGNAL(sliderReleased()), SLOT(positionSliderMoved()));
connect(&player, SIGNAL(positionChanged(qint64)), SLOT(playerPositionChanged(qint64)));
}