将内置的所有音频文件都加入到了demo中,并修改cli提示信息进行实验:
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 if (strcmp(argv[2], "raw") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u&avformat=rawaudio&avcodec=pcm_s8&channel=1&rate=8000", (uint32_t)&_example_little_raw, _example_little_raw_len);
player_play(get_player_demo(), url, 0);
} else if (strcmp(argv[2], "hello") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_hello, sizeof(local_audio_hello));
player_play(get_player_demo(), url, 0);
} else if (strcmp(argv[2], "net_fail") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_net_fail, sizeof(local_audio_net_fail));
player_play(get_player_demo(), url, 0);
} else if (strcmp(argv[2], "net_succ") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_net_succ, sizeof(local_audio_net_succ));
player_play(get_player_demo(), url, 0);
} else if (strcmp(argv[2], "ok") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_ok, sizeof(local_audio_ok));
player_play(get_player_demo(), url, 0);
} else if (strcmp(argv[2], "sorry2") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_sorry2, sizeof(local_audio_sorry2));
player_play(get_player_demo(), url, 0);
} else if (strcmp(argv[2], "sorry") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_sorry, sizeof(local_audio_sorry));
player_play(get_player_demo(), url, 0);
} else if (strcmp(argv[2], "starting") == 0) {
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_starting, sizeof(local_audio_starting));
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 {
printf("\tplayer play welcom/raw/hello/net_fail/net_succ/ok/sorry2/sorry/starting\n");
printf("\tplayer pause\n");
printf("\tplayer resume\n");
printf("\tplayer stop\n");
printf("\tplayer help");
}
播放测试日志:
系统封装的音频播放接口很简单,很好用,初始化相关参数后直接调API接口播放内容即可,随后我就开始测试web player,测试通过网络获取音频流进行播放,这里就涉及到连网了,不过这块暂时不用关心,直接将init.c代码中的WiFi参数修改一下,用上自己的SSID名称和密码即可:
static void network_init()
{
w800_wifi_param_t w800_param;
/* init wifi driver and network */
w800_param.reset_pin = PA21;
w800_param.baud = 1*1000000;
w800_param.cs_pin = PA15;
w800_param.wakeup_pin = PA25;
w800_param.int_pin = PA22;
w800_param.channel_id = 0;
w800_param.buffer_size = 4*1024;
wifi_w800_register(NULL, &w800_param);
app_netmgr_hdl = netmgr_dev_wifi_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, "test", strlen("test"), "123456789", strlen("123456789"));
netmgr_start(app_netmgr_hdl);
}
}
既然有了实际的联网动作了,当然要把刚刚熟悉的播放音频给用上啊,将联网成功和失败的结果通过音频播放出来,在app_main.c中将联网回调事件监听处理函数里面,增加相应的音频播放代码:
static void network_event(uint32_t event_id, const void *param, void *context)
{
char url[128] = {0};
switch(event_id) {
case EVENT_NETMGR_GOT_IP:
LOGD(TAG, "net got ip");
player_stop(get_player_demo());
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_net_succ, sizeof(local_audio_net_succ));
player_play(get_player_demo(), url, 0);
break;
case EVENT_NETMGR_NET_DISCON:
LOGD(TAG, "net disconnect");
player_stop(get_player_demo());
snprintf(url, sizeof(url), "mem://addr=%u&size=%u", (uint32_t)&local_audio_net_fail, sizeof(local_audio_net_fail));
player_play(get_player_demo(), url, 0);
break;
}
/*do exception process */
app_exception_event(event_id);
}
注意:启动时会播放欢迎音频,如果联网成功或失败需要播放相关音频时,如果之前还在播放需要先停止之前的播放,调用player_stop(get_player_demo())即可。
日志中显示已经获取到了IP地址:192.168.199.193,启动视频如下:
https://bbs.eeworld.com.cn/forum.php?mod=attachment&aid=NTk5MTgzfDY4ZjUxYzdhM2Q1OGJlMzQ2NWZhODRmNzZlN2E2NmY1fDE3MzcxOTUxNTA%3D&request=yes&_f=.mp4
在webplayer demo中,我还增加了音量的控制代码
if (argc == 3 && strcmp(argv[1], "vol") == 0) {
g_volume = atoi(argv[2]);
printf("set player volume %d\n", g_volume);
player_set_vol(get_player_demo(), g_volume);
}
测试播放web音频,播放局域网HTTP服务器中周董的歌曲,测试播放、暂停、继续、音量增加、减小、停止等动作:
https://download.eeworld.com.cn/detail/UwenS/623253
以上为当前记录~
|