qt音乐播放器一个按钮实现点一下播放音乐再点一下暂停播放
时间: 2023-07-13 14:35:37 浏览: 111
您可以使用QMediaPlayer类来实现这个功能。您需要创建一个QPushButton并将其连接到一个槽函数。在槽函数中,您可以使用QMediaPlayer的play()和pause()函数来播放和暂停音乐。您还需要跟踪当前的播放状态以便在按钮被点击时进行适当的操作。以下是一个示例代码:
```cpp
#include <QtWidgets>
class MusicPlayer : public QWidget
{
Q_OBJECT
public:
MusicPlayer(QWidget *parent = nullptr);
private slots:
void togglePlayback();
private:
QPushButton *m_button;
QMediaPlayer *m_player;
QMediaPlaylist *m_playlist;
QMediaPlaylist *m_playedPlaylist;
enum PlaybackState { StoppedState, PlayingState, PausedState };
PlaybackState m_state = StoppedState;
};
MusicPlayer::MusicPlayer(QWidget *parent)
: QWidget(parent)
{
// Create the button and connect it to the togglePlayback slot
m_button = new QPushButton("Play", this);
connect(m_button, &QPushButton::clicked, this, &MusicPlayer::togglePlayback);
// Create the player and playlist
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(m_player);
m_playedPlaylist = new QMediaPlaylist(m_player);
// Add some songs to the playlist
m_playlist->addMedia(QUrl("qrc:/music/song1.mp3"));
m_playlist->addMedia(QUrl("qrc:/music/song2.mp3"));
m_playlist->addMedia(QUrl("qrc:/music/song3.mp3"));
// Set the playlist to loop
m_playlist->setPlaybackMode(QMediaPlaylist::Loop);
// Set the player's playlist
m_player->setPlaylist(m_playlist);
}
void MusicPlayer::togglePlayback()
{
switch (m_state) {
case StoppedState:
m_player->play();
m_state = PlayingState;
m_button->setText("Pause");
break;
case PlayingState:
m_player->pause();
m_state = PausedState;
m_playedPlaylist->addMedia(m_player->currentMedia());
m_button->setText("Play");
break;
case PausedState:
m_player->play();
m_state = PlayingState;
m_button->setText("Pause");
break;
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// Load the music files from resources
Q_INIT_RESOURCE(music);
MusicPlayer player;
player.show();
return app.exec();
}
#include "main.moc"
```
在这个示例中,我们创建了一个名为MusicPlayer的窗口小部件,并在其中添加了一个QPushButton。我们还创建了一个QMediaPlayer和两个QMediaPlaylist,一个用于播放列表,另一个用于存储已播放的媒体。在槽函数togglePlayback()中,我们根据当前的播放状态来决定是播放还是暂停音乐,并更新按钮的文本。当用户点击按钮时,该函数将被调用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)