配置环境
安卓开发环境
新建工程
主要步骤
页面布局
逻辑实现
做音乐播放器时由于复制播放路径中路径是“/”,会出错误
需要改为“\”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include "widget.h" #include "QSound" #include <QMediaPlayer> #include <QAudioOutput> #include <QUrl> #include <QMessageBox> #include <QNetworkReply> #include <QFile> #include <QNetworkAccessManager> #include<QCoreApplication> #include <QApplication> Widget::Widget(QWidget *parent) : QWidget(parent) { auto mediaplayer = new QMediaPlayer(this); mediaplayer->setMedia(QUrl("http://www.ihaoge.net/kw/226278174/antiserver.kuwo.cn/anti.s?rid=MUSIC_226278174&response=res&format=mp3|aac&type=convert_url&br=128kmp3&agent=iPhone&callback=getlink&jpcallback=getlink.mp3")); mediaplayer->play();
QUrl newUrl(QUrl("http://www.ihaoge.net/kw/226278174/antiserver.kuwo.cn/anti.s?rid=MUSIC_226278174&response=res&format=mp3|aac&type=convert_url&br=128kmp3&agent=iPhone&callback=getlink&jpcallback=getlink.mp3")); if (!newUrl.isValid()) { QMessageBox::information(this, "error", "invalid url"); return; } QNetworkRequest networkRequest; networkRequest.setUrl(newUrl);
QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager;
QNetworkReply *newReply = networkAccessManager->get(networkRequest); connect(newReply, SIGNAL(readyRead()), this, SLOT(readyReadData())); }
void Widget::readyReadData() {
QNetworkReply *reply = (QNetworkReply *)sender();
QFile yy; yy.setFileName(QCoreApplication::applicationDirPath() + "/下载的.mp3");
}
Widget::~Widget() {
}
|
效果图
![](/2023/05/30/QT/1.jpg)
点击中间按钮即可播放音乐