我创建一个音频播放器与Qt和我正在实施,当我加载一首歌它启动automaticaly,但是当我调用函数开始歌曲并计算持续时间,歌曲不会启动。我使用了调试,当我到达播放函数调试停止与结束步进范围。我也尝试发出一个信号,但没有做任何事情,只有当我按下播放按钮时才开始播放歌曲。QMediaPlayer.play()不工作,如果我用它调用一个函数
在这里有代码
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)));
}
我会检查playerPositionChanged'和'positionSliderMoved'之间'的环键。 – LogicStuff