Qtでオーディオプレーヤーを作成しており、曲をロードすると自動的に開始されるように実装していますが、曲を開始する関数を呼び出してデュレーションを計算すると、曲は開始しません。私はデバッグを使用し、再生機能に到達するとデバッグは end-stepping-range で停止しました。また、信号を発信しようとしましたが、何もすることはありません。再生ボタンを押した場合にのみ曲が始まります。
ここにコードがあります
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)));
}