配置环境

安卓开发环境

新建工程

主要步骤

  1. 页面布局

  2. 逻辑实现

做音乐播放器时由于复制播放路径中路径是“/”,会出错误

需要改为“\”

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 audiooutput = new QAudioOutput(this);
auto mediaplayer = new QMediaPlayer(this);
//mediaplayer->setAudioRole(audiooutput);
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();

/* 获取 URL 链接 */
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;
/* 保存到当前路径,名称为"下载的.jpg" */
yy.setFileName(QCoreApplication::applicationDirPath() + "/下载的.mp3");

//auto mediaplayer = new QMediaPlayer(this);
//mediaplayer->setAudioRole(audiooutput);
//mediaplayer->setMedia(QUrl::fromLocalFile("/下载的.mp3"));
//mediaplayer->play();
}

Widget::~Widget()
{

}

效果图


点击中间按钮即可播放音乐