代码拉取完成,页面将自动刷新
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->sound_slider->hide();//隐藏音量控件
//获取播放列表
db=new MySqlConnect;
curlist=db->query_music();
//进度条改变和时长
//connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(change_process(qint64)));
//初始化历史播放列表
historylist=curlist;
init_historylist();
//播放默认音乐
player = new QMediaPlayer;
playlist=new QMediaPlaylist;
//更新playlist;
refresh_playlist();
playlist->setCurrentIndex(0);//默认第一首
playlist->setPlaybackMode(QMediaPlaylist::Loop);//循环播放
ui->play_but->setIcon(QIcon(":/image/image/button/loop.png"));
player->setPlaylist(playlist);
//音量
player->setVolume(100);
ui->sound_slider->setValue(100);//默认100音量
//默认暂停
player->pause();
ui->play_button->setIcon(QIcon(":/image/image/button/pase.png"));
ui->song_inf->setText(curlist[playlist->currentIndex()].getTitle());
//默认下载位置
localurl=QUrl("C:/Users/82719/Desktop/music_download");
//默认首页
ui->stackedWidget_2->setCurrentIndex(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
/**
* 槽函数
*/
void MainWindow::on_quit_button_clicked()//关闭窗口
{
QCoreApplication::quit();
}
void MainWindow::on_minimize_but_clicked()//小化窗口
{
}
void MainWindow::on_sound_button_clicked()//显示隐藏声音控件
{
if(ui->sound_slider->isHidden()){
ui->sound_slider->show();
}else{
ui->sound_slider->hide();
}
}
void MainWindow::on_cur_button_clicked()//转到当前播放列表
{
ui->stackedWidget->setCurrentIndex(0);
}
void MainWindow::on_like_button_clicked()//转到喜欢列表
{
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow::on_local_button_clicked()//转到本地音乐列表
{
ui->stackedWidget->setCurrentIndex(2);
}
void MainWindow::on_play_button_clicked()//播放暂停按钮
{
if(player->state()==QMediaPlayer::PlayingState)
{
ui->play_button->setIcon(QIcon(":/image/image/button/pase.png"));
player->pause();
}
else
{
ui->play_button->setIcon(QIcon(":/image/image/button/play.png"));
player->play();
}
}
void MainWindow::on_cur_list_itemClicked(QListWidgetItem *item)//点击当前播放列表中的音乐
{
//qDebug()<<ui->cur_list->row(item)<<endl;
curlist=historylist;
refresh_playlist();
playlist->setCurrentIndex(ui->cur_list->row(item));
playmusic();
}
void MainWindow::on_down_but_clicked()//点击下载按钮
{
QMessageBox::information(NULL, "下载提示", "开始下载...");
Music tmp=curlist.at(playlist->currentIndex());
locallist.push_back(tmp);
ui->local_list->addItem(tmp.getTitle());
}
void MainWindow::on_sound_slider_sliderMoved(int position)//修改音量
{
player->setVolume(ui->sound_slider->value());
}
void MainWindow::on_next_button_clicked()//下一首
{
playlist->next();
playmusic();
}
void MainWindow::on_pre_button_clicked()//上一首
{
playlist->previous();
playmusic();
}
void MainWindow::on_play_but_clicked()//切换播放方式
{
if(playlist->playbackMode()==QMediaPlaylist::Loop)
{
ui->play_but->setIcon(QIcon(":/image/image/button/play-random.png"));
//ui->play_but->setToolTip(("随机播放"));
playlist->setPlaybackMode(QMediaPlaylist::Random);
}
else if(playlist->playbackMode()==QMediaPlaylist::Random)
{
ui->play_but->setIcon(QIcon(":/image/image/button/loop-one.png"));
//ui->play_but->setToolTip(("随机播放"));
playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
}
else if(playlist->playbackMode()==QMediaPlaylist::CurrentItemInLoop)
{
ui->play_but->setIcon(QIcon(":/image/image/button/loop.png"));
//ui->play_but->setToolTip(("随机播放"));
playlist->setPlaybackMode(QMediaPlaylist::Loop);
}
}
/**
* private函数
*/
void MainWindow::init_historylist()
{
//QListWidgetItem* item = new QListWidgetItem;
for(auto i=historylist.cbegin();i!=historylist.cend();++i)
{
//qDebug()<<i->getTitle();
//item->setText(i->getTitle());
ui->cur_list->addItem(i->getTitle());
}
}
void MainWindow::refresh_playlist()
{
playlist->clear();
for(auto i=curlist.cbegin();i!=curlist.cend();++i){
//qDebug()<<i->getTitle()<<endl;
playlist->addMedia(QMediaContent(i->getUrl()));
}
}
void MainWindow::playmusic()
{
player->play();
ui->play_button->setIcon(QIcon(":/image/image/button/play.png"));
ui->song_inf->setText(curlist[playlist->currentIndex()].getTitle());
}
void MainWindow::on_home_but_clicked()
{
ui->search_text->clear();//清空搜索内容
ui->stackedWidget_2->setCurrentIndex(0);
}
void MainWindow::on_search_button_clicked()
{
ui->search_widget->clear();//清空搜索列表
if(ui->search_text->text()!=nullptr)
{
ui->stackedWidget_2->setCurrentIndex(1);
curlist=db->search_query(ui->search_text->text());//更新当前播放列表
for(auto i=curlist.cbegin();i!=curlist.cend();++i)
{
ui->search_widget->addItem(i->getTitle());
}
}else{
ui->stackedWidget_2->setCurrentIndex(0);
}
}
void MainWindow::on_search_widget_itemClicked(QListWidgetItem *item)//点击查询结果列表里的数据
{
int index=ui->search_widget->row(item);
refresh_playlist();//同步音乐播放列表
playlist->setCurrentIndex(index);
playmusic();
historylist.push_back(curlist.at(index));
ui->cur_list->addItem(curlist.at(index).getTitle());
}
void MainWindow::on_progress_sliderMoved(int position)
{
qint64 playtime = player->duration();
player->setPosition(playtime*ui->progress->value()*0.01);//
}
void MainWindow::change_process(qint64 t)
{
//ui->progress->setValue(t);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。