how to input system music file using QMediaplayer in qt6.2
-
Hi, I'm writing a program that count down time with music playing background in qt6.2, but following detailed description in https://doc-snapshots.qt.io/qt6-dev/qmediaplayer.html,
player = new QMediaPlayer; connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); player->setSource(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); player->setVolume(50); player->play();
my music cannot play, and strangely, my qt doesn't have setvolume function, here is my music part code:
m_player = new QMediaPlayer(this); connect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); m_player->setSource(QUrl::fromLocalFile("C:/Users/aki/Downloads/bgm1.mp3")); m_player->play();
this file location is real and the file is exist, what's wrong about my code?
-
Same. It's confusing that I got
QMediaPlayer::ResourceError
.I didn't understand your solution till I read Qt Multimedia in Qt 6:
QMediaPlayer in Qt 6 requires you to actively connect it to both an audio and video output using the
setAudioOutput()
andsetVideoOutput()
methods. Not setting an audio output will imply that the media player doesn’t play audio. This is a change from Qt 5, where a default audio output was always selected.(bolds mine)
-
Same. It's confusing that I got
QMediaPlayer::ResourceError
.I didn't understand your solution till I read Qt Multimedia in Qt 6:
QMediaPlayer in Qt 6 requires you to actively connect it to both an audio and video output using the
setAudioOutput()
andsetVideoOutput()
methods. Not setting an audio output will imply that the media player doesn’t play audio. This is a change from Qt 5, where a default audio output was always selected.(bolds mine)
Then I met
QAudioOutput
crash issue.However, all I need (this time) is to play a WAV, so I turn to PowerShell. Some thing like this:
private: QProcess *_shell; public: WavPlayer() { this->_shell = new QProcess(this); this->_shell->setProgram("powershell"); this->_shell->start(); } void play(const QFileInfo &file) { this->_shell->write("(New-Object Media.SoundPlayer '" + file.absoluteFilePath() + "').Play()\r\n".toUtf8()); }
Note: Details (changing PS's code page, dealing with qrc path, etc.) are removed to make it clear. The above code may not work in real project.
(Multi-
QProcess
needed if you want to play several WAV simultaneously) -
This post is deleted!
-
@akikaede said in how to input system music file using QMediaplayer in qt6.2:
I find that I need to add
QAudioOutput
in mediaplayer, so that I can play music in qt.Hi there . I have same problem and i dont know how to use QAudioOutput ! Can you help me please ?
-
@akikaede said in how to input system music file using QMediaplayer in qt6.2:
I find that I need to add
QAudioOutput
in mediaplayer, so that I can play music in qt.Hi there . I have same problem and i dont know how to use QAudioOutput ! Can you help me please ?
@Alireza_ALZ hi,
The QMediaPlayer documentation shows a minimal example.