【平头哥RVB2601开发板试用体验】+webplayer实验
[复制链接]
本帖最后由 damiaa 于 2022-1-26 17:55 编辑
【平头哥RVB2601开发板试用体验】+webplayer实验
1,安装putty,下载安装剑池cdk。这步在以前的工程中做个。就不重复了。
2,导入ch2601webplayer_demo工程。
打开cdk==》file==》new==》new IoT project...
在IoT Project Wizard中找到ch2061==》next==》RVB2601==》next
点击ch2601webplayer_demo,点击Finish导入ch2601webplayer_demo工程
3,代码浅析:
ch2601webplayer_demo代码结构:
// app_main.c: 初始化网络和命令接口进入命令响应状态
int main(void)
{
board_yoc_init();
player_init()
cli_reg_cmd_player();
/* Subscribe */
event_subscribe(EVENT_NETMGR_GOT_IP, network_event, NULL);
event_subscribe(EVENT_NETMGR_NET_DISCON, network_event, NULL);
}
//cli_cmd.c 处理常用命令。
//init.c 网络接口和板级初始化。
player_demo.c player 播放mp3例子的有关的命令处理,内存加载,web解析等
static player_t *g_player;
static void _player_event(player_t *player, uint8_t type, const void *data, uint32_t len)//启动停止命令
{
int rc;
UNUSED(len);
UNUSED(data);
UNUSED(handle);
LOGD(TAG, "=====%s, %d, type = %d", __FUNCTION__, __LINE__, type);
switch (type) {
case PLAYER_EVENT_ERROR:
rc = player_stop(player);
break;
case PLAYER_EVENT_START: {
media_info_t minfo;
memset(&minfo, 0, sizeof(media_info_t));
rc = player_get_media_info(player, &minfo);
LOGD(TAG, "=====rc = %d, duration = %llums, bps = %llu, size = %u", rc, minfo.duration, minfo.bps, minfo.size);
break;
}
case PLAYER_EVENT_FINISH:
player_stop(player);
break;
default:
break;
}
}
static char* g_url;
static void _webtask(void *arg)
{
int cnt = 0, rc, total;
char *val, buf[128];
wsession_t *session;
session = wsession_create();
rc = wsession_get(session, g_url, 3);
if (rc) {
LOGE(TAG, "wsession_get fail. rc = %d, code = %d, phrase = %s", rc, session->code, session->phrase);
goto err;
}
val = (char*)dict_get_val(&session->hdrs, "Content-Length");
CHECK_RET_TAG_WITH_GOTO(val, err);
total = atoi(val);
printf("##total cnt = %d Bytes, read percent = %3d %%", total, cnt * 100 / total);
for (;;) {
rc = wsession_read(session, buf, sizeof(buf), 3*1000);
if (rc <= 0) {
LOGI(TAG, "read may be eof. rc = %d, read cnt = %d", rc, cnt);
break;
} else {
cnt += rc;
printf("\b\b\b\b\b%3d %%", cnt * 100 / total);
}
}
//LOGD(TAG, "rc = %8d, cnt = %8d", rc, cnt);
err:
wsession_destroy(session);
aos_freep(&g_url);
return;
}
static void cmd_ipc_func(char *wbuf, int wbuf_len, int argc, char **argv)//安装命令参数(是网址还是文件)调用player_play
{
if (argc == 3 && strcmp(argv[1], "play") == 0) {
char url[128];
if (strcmp(argv[2], "welcom") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&_welcome_mp3, _welcome_mp3_len);
player_play(get_player_demo(), url, 0);
} else {
player_play(get_player_demo(), argv[2], 0);
}
} else if (argc == 2 && strcmp(argv[1], "stop") == 0) {
player_stop(get_player_demo());
} else if (argc == 2 && strcmp(argv[1], "pause") == 0) {
player_pause(get_player_demo());
} else if (argc == 2 && strcmp(argv[1], "resume") == 0) {
player_resume(get_player_demo());
} else if (argc == 3 && strcmp(argv[1], "web") == 0) {
g_url = strdup(argv[2]);
LOGD(TAG, "g_url = %s", g_url);
aos_task_new("web_task", _webtask, NULL, 6*1024);
} else {
printf("\tplayer play welcom/url[http://]\n");
printf("\tplayer pause\n");
printf("\tplayer resume\n");
printf("\tplayer stop\n");
printf("\tplayer help");
}
}
player_t *get_player_demo()//player前参数初始化
{
if (!g_player) {
ply_conf_t ply_cnf;
player_conf_init(&ply_cnf);
ply_cnf.vol_en = 1;
ply_cnf.vol_index = 160; // 0~255
ply_cnf.event_cb = _player_event;
ply_cnf.period_num = 12; // 12 * 5 period_ms = 60ms
ply_cnf.cache_size = 32 * 1024; // web cache size
g_player = player_new(&ply_cnf);
}
return g_player;
}
int cli_reg_cmd_player(void)
{
char url[128];
static const struct cli_command cmd_info = {
"player",
"player example",
cmd_ipc_func,
};
aos_cli_register_command(&cmd_info);
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&_welcome_mp3, _welcome_mp3_len);
player_play(get_player_demo(), url, 0);
return 0;
}
4,修改init.c
static void network_init()
{
....
if (app_netmgr_hdl) {
utask_t *task = utask_new("netmgr", 2 * 1024, QUEUE_MSG_COUNT, AOS_DEFAULT_APP_PRI);
netmgr_service_init(task);
netmgr_config_wifi(app_netmgr_hdl, "xxxx-xxxx", 9, "xxxxxxxxx", 9); //这行添加一下自己家的WiFi用户名和密码
netmgr_start(app_netmgr_hdl);
}
}
为了每次启动的时候不要老是设置wifi用户和密码
5,编译下载运行。
6,实验1:
连接putty 设置好串口, 波特率 115200,n,8,1。如果没有信息按一下复位键。
板子启动后看到有ip地址就对了。也可以用ifconfig查一下。
这个时候可以输入命令:就可以播放了
player play http://yocbook.oss-cn-hangzhou.aliyuncs.com/av_repo/alibaba.mp3
但如果我们 想播放自己的网络音乐怎么办呢?接下来看:
7,制作简单web播放服务器。
使用node.js express框架 有兴趣的可以看看https://www.expressjs.com.cn/。
安装 node.js
建立一个目录:d:nodewebtest
安装express
输入D:\nodewebtest>npm init -y
输入D:\nodewebtest>npm install express --save
输入D:\nodewebtest>npm install nodemon -g
输入D:\nodewebtest>npm install serve-index --save
输入D:\nodewebtest>npm install multiparty --save
建立一个js文件 music-js-webtest.js如下:
var serveIndex = require('serve-index')
var serveStatic = require('serve-static')
var multiparty = require('multiparty')
var util = require('util') // node的核心工具包
//端口3000
var app = express()
app.use(bodyParser.json())
// 处理POST请求
app.post('/', function (req, res) {
console.log(req.body) // 接收客户端发送的请求数据
res.json(req.body) // 响应数据到客户端
//res.json(req.query)
})
// 配置媒体文件夹mymusic
var serve = serveStatic('./mymusic')
app.use('/', serveIndex('./mymusic', {'icons': true}))
// 处理GET请求
app.get('/*', function(req, res) {
serve(req, res)
});
console.log(`启动文件服务器 ::${3000}, 按ctrl + C to 退出`)
app.listen(3000)
8,实验2
D:\nodewebtest>目录里面建立一个mymusic目录,目录里面放几个要播放的mp3文件 (下面的实验里,这个目录里面现在有001.mp3,002.mp3)
在目录里面启动node服务器:
node music-js-webtest.js
在网页里面运行http://127.0.0.1:3000/002.mp3 就可以播放了。
实验发现:
平头哥RVB2601的player play 可以解析192.168..的命令
和http://yocbook.oss-cn-hangzhou.aliyuncs.com/av_repo/alibaba.mp3 ,
不能解析127.0.0.1:3000...和localhost:3000...命令
现在运行:
player play http://192.168.1.6:3000/002.mp3 可以播放音乐了。
9,实验结束。
|