QMediaPlayer mp3 playback stalled
-
wrote on 13 May 2025, 05:57 last edited by
Hey there! I'm using QMediaPlayer to play audio files in my app. The issue is that when playing an mp3 file, the playback never starts, but when I manually seek it to change the position, only then does it start playing. There is no such problem with other formats such as FLAC/AAC/ALAC. Here's the code I'm using:
void player::init(queuemodel *model) { mediaplayer = new QMediaPlayer(this); output = new QAudioOutput(this); connect(mediaplayer, &QMediaPlayer::playbackStateChanged, this, &player::player_state_changed); connect(mediaplayer, &QMediaPlayer::positionChanged, this, &player::time_changed); mediaplayer->setAudioOutput(output); output->setVolume(100); this->model = model; reader = new Tag_reader(this); //media status signal connect(mediaplayer, &QMediaPlayer::mediaStatusChanged, this, &player::media_status_changed); emit player_ready(); } void player::play_current() { QModelIndex index = model->index(current_index, 0); mediaplayer->setSource(QUrl::fromLocalFile(model->data(index, queuemodel::PathRole).toString())); mediaplayer->play(); }
Even when i wait for the loaded media signal and then play it, it makes no difference. I'm on Qt6.9 on Windows 11 and using it with QtQuick/QML. Any help is appreciated.
-
Hey there! I'm using QMediaPlayer to play audio files in my app. The issue is that when playing an mp3 file, the playback never starts, but when I manually seek it to change the position, only then does it start playing. There is no such problem with other formats such as FLAC/AAC/ALAC. Here's the code I'm using:
void player::init(queuemodel *model) { mediaplayer = new QMediaPlayer(this); output = new QAudioOutput(this); connect(mediaplayer, &QMediaPlayer::playbackStateChanged, this, &player::player_state_changed); connect(mediaplayer, &QMediaPlayer::positionChanged, this, &player::time_changed); mediaplayer->setAudioOutput(output); output->setVolume(100); this->model = model; reader = new Tag_reader(this); //media status signal connect(mediaplayer, &QMediaPlayer::mediaStatusChanged, this, &player::media_status_changed); emit player_ready(); } void player::play_current() { QModelIndex index = model->index(current_index, 0); mediaplayer->setSource(QUrl::fromLocalFile(model->data(index, queuemodel::PathRole).toString())); mediaplayer->play(); }
Even when i wait for the loaded media signal and then play it, it makes no difference. I'm on Qt6.9 on Windows 11 and using it with QtQuick/QML. Any help is appreciated.
@nova_xo said in QMediaPlayer mp3 playback stalled:
mediaplayer = new QMediaPlayer(this);
Do you delete the previous player somewhere?
-
@nova_xo said in QMediaPlayer mp3 playback stalled:
mediaplayer = new QMediaPlayer(this);
Do you delete the previous player somewhere?
wrote on 13 May 2025, 07:24 last edited by nova_xo@jsulm No, I do not. The mediaplayer object is defined in the global scope and initialized once the init function is called. The problem is specifically with mp3 files, other formats work fine. Also, I've created only one player.
-
wrote on 13 May 2025, 07:39 last edited by
I found a hack. Though I'm still not sure whats causing the playback to get stalled.
void player::play_current() { QModelIndex index = model->index(current_index, 0); mediaplayer->setSource(QUrl::fromLocalFile(model->data(index, songmodel::PathRole).toString())); reader->read(model->data(index, songmodel::PathRole).toString().toUtf8().data()); reader->get_synced_lyrics(model->data(index, songmodel::PathRole).toString().toUtf8().data()); if (model->data(index, songmodel::PathRole).toString().endsWith(".mp3")) { mediaplayer->play(); wait(1000); set_position(1); }else { mediaplayer->play(); } } void player::wait(int milliseconds) { QEventLoop loop; QTimer::singleShot(milliseconds, &loop, &QEventLoop::quit); loop.exec(); // Blocks here until the timer calls quit() }
This works for the time being.
-
wrote on 13 May 2025, 07:55 last edited by
250ms timer is the sweet spot for me
-
@nova_xo I think that is the right order. Adding 250ms or 200ms delay is ok. Sometimes it takes some time for the pipeline to be ready. Nothing wrong with some delay.
-
@JoeCFD What I don't understand is why is this happening specifically with mp3 files. All other formats work fine. What I'm doing is hacky at best and not a concrete solution.
wrote 30 days ago last edited by@nova_xo I am not sure either. But be aware that some delay is often needed in the pipeline for state or position change. Good you found the solution. I use raw gstreamer code and can find out where waiting is exactly needed. You may need to guess a bit with Qt.
3/10