之前我用QSound播放音乐,生成可执行文件可以听到声音。
void Widget::bgMusic()
{//QSound实现的不能调节音量
QSound *startsound=new QSound(":/02-Hans Zimmer-Cornfield Chase.wav",this);
startsound->play(); //播放
startsound->setLoops(-1);//循环次数,-1代表一直循环
}
后来换成了QMediaPlayer,再生成可执行文件时就没用声音了,(在QT软件中运行有声音。)
static QMediaPlayer *player = new QMediaPlayer;
void Widget::bgMusic()
{
ui->VoiceSlider->setVisible(false);
QMediaPlaylist *playlist = new QMediaPlaylist;
playlist->addMedia(QUrl("qrc:/02-Hans Zimmer-Cornfield Chase.wav"));//资源路径
playlist->setCurrentIndex(1);
playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);//循环模式
player->setPlaylist(playlist);
player->setVolume(50);//音量大小,滑动条控制
player->play();
}