文章目录
Qt播放音频
在linux系统中,可以通过aplay进行简单的播放音频,如
aplay /opt/Audio/test.wav
在图形界面,也可以封装aplay进行播放,但事实上,图形界面qt,通过封装alsa-lib,为我们提供了更简单易用的播放音频类,其中包括:QMediaPlayer,QSound,QSoundEffect以及最新的QAudioOutput,本人经过测试,验证以上类都可以正常播放音频文件,点击不同的界面,使用不同的方法进行播放:
方法一 QMediaPlayer
官方描述:
The QMediaPlayer class is a high level media playback class. It can be used to playback such content as songs, movies and internet radio. The content to playback is specified as a QMediaContent object, which can be thought of as a main or canonical URL with additional information attached. When provided with a QMediaContent playback may be able to commence.
官方提供的示例
player = new QMediaPlayer;
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
player->setVolume(50);
player->play();
测试的示例
QMediaPlayer* mediaplayer = new QMediaPlayer;
mediaplayer->setMedia(QUrl::fromLocalFile("/opt/Audio/test.wav"));
mediaplayer->setVolume(50);
mediaplayer->play();
方法二 QSound
Qt provides