跳转到内容

源 URL 配置

源 URL 配置

本节介绍如何管理网络音频流的 URL 配置。学习完成后,您将能够:

  • 查找和验证网络电台的音频流 URL
  • 管理和切换多个音频源
  • 理解不同音频格式 URI 的差异
  • 为工厂广播场景选择适当的音频源

在开始本节之前,请确保:

  • 已完成音频流连接配置
  • 了解基本的 HTTP/HTTPS URL 格式
  • 能从网络电台获取流媒体 URL

网络电台的流 URL 有多种格式:

格式示例说明
直接 MP3http://example.com:8000/stream纯 MP3 流,最常见
播放列表 (PLS)http://example.com/listen.pls包含实际流 URL 的播放列表
播放列表 (M3U)http://example.com/listen.m3uM3U 格式播放列表
HTTPS 流https://example.com/stream.mp3加密传输
HLS 流http://example.com/stream.m3u8Apple 的 HLS 协议

方法 1:社区列表

资源URL说明
Radio Browserhttps://www.radio-browser.info/全球电台数据库,提供 API
TuneInhttps://tunein.com/大量电台流 URL
ShoutCasthttps://directory.shoutcast.com/经典的网络电台目录

方法 2:Radio Browser API

Terminal window
# 搜索特定国家的电台
curl "https://de1.api.radio-browser.info/json/stations/byname/Austrian%20Radio"
# 获取电台的流 URL
# 返回结果中包含 url 和 url_resolved 字段

方法 3:从网页解析

许多电台网站不直接显示流 URL。可以通过:

  1. 查看网页源代码,搜索 .mp3.pls
  2. 使用浏览器的开发者工具 → Network 标签 → 过滤 “audio/mpeg”
  3. 使用专门的电台 URL 查找工具
// 多电台 URL 配置
const char* radioStations[] = {
"http://stream1.example.com:8000/radio", // 电台 1
"http://stream2.example.com:8000/radio.mp3", // 电台 2
"http://stream3.example.com:8000/live", // 电台 3
"http://stream4.example.com:8000/stream", // 电台 4
};
const char* stationNames[] = {
"Radio Station 1",
"Radio Station 2",
"Radio Station 3",
"Radio Station 4",
};
const int stationCount = sizeof(radioStations) / sizeof(radioStations[0]);
int currentStation = 0;
// 切换到指定电台
bool switchStation(int stationIndex) {
if (stationIndex < 0 || stationIndex >= stationCount) {
return false;
}
// 停止当前播放
if (mp3->isRunning()) {
mp3->stop();
}
file->close();
// 切换到新电台
currentStation = stationIndex;
file->open(radioStations[currentStation]);
mp3->begin(file, output);
Serial.printf("切换到: %s\n", stationNames[currentStation]);
return true;
}
// 切换到下一个电台
void nextStation() {
switchStation((currentStation + 1) % stationCount);
}
// 切换到上一个电台
void prevStation() {
switchStation((currentStation - 1 + stationCount) % stationCount);
}
bool isValidStreamURL(const char* url) {
// 检查前缀
if (strncmp(url, "http://", 7) != 0 &&
strncmp(url, "https://", 8) != 0) {
Serial.println("URL 必须以 http:// 或 https:// 开头");
return false;
}
// 检查长度
if (strlen(url) < 10 || strlen(url) > 512) {
Serial.println("URL 长度不符合要求");
return false;
}
return true;
}
// 根据时间段自动选择电台
const char* selectStationByTime() {
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
int hour = timeinfo.tm_hour;
// 早间 6:00-9:00 → 晨间新闻
if (hour >= 6 && hour < 9) {
return radioStations[0]; // 新闻台
}
// 白天 9:00-17:00 → 背景音乐
else if (hour >= 9 && hour < 17) {
return radioStations[1]; // 轻音乐
}
// 傍晚 17:00-22:00 → 娱乐节目
else if (hour >= 17 && hour < 22) {
return radioStations[2]; // 流行音乐
}
// 夜间 22:00-6:00 → 安静节目
else {
return radioStations[3]; // 古典/深夜音乐
}
}
广播类型音频源类型内容示例
换班提醒本地 WAV/MP3 文件”上午班开始,请各岗位就位”
紧急告警本地 WAV 文件”紧急情况!请立即疏散”
背景音乐网络电台流工厂车间背景音乐
定时通知服务器合成语音(TTS)Node-RED 生成语音并推流
生产进度广播网络电台流实时播报产量数据
// 广播优先级
enum BroadcastPriority {
PRIORITY_NORMAL = 0, // 背景音乐
PRIORITY_ANNOUNCEMENT = 1, // 一般通知
PRIORITY_ALARM = 2 // 紧急告警
};
BroadcastPriority currentPriority = PRIORITY_NORMAL;
// 处理高优先级广播
void handleHighPriorityBroadcast(const char* audioURL,
BroadcastPriority priority) {
if (priority > currentPriority) {
// 保存当前播放状态
currentPriority = priority;
// 中断当前播放
if (mp3->isRunning()) {
mp3->stop();
}
// 播放高优先级广播
file->open(audioURL);
mp3->begin(file, output);
Serial.printf("高优先级广播播放中(优先级: %d\n", priority);
}
}
// 恢复常规播放
void resumeNormalBroadcast() {
currentPriority = PRIORITY_NORMAL;
switchStation(currentStation);
}
考量维度说明买家沟通要点
音频源支持网络电台、本地文件、TTS 语音”可根据需要选择多种音频源”
切换策略支持优先级和定时切换”紧急广播自动打断背景音乐”
多区管理每个 ESP32 独立播放”不同车间播放不同内容”
音质要求语音通知 64kbps,音乐 128kbps”语音通知品质完全够用”

本节介绍了音频源 URL 的配置和管理:

  1. URL 格式:网络电台流 URL 有多种格式,直接 MP3 URL 最易于使用
  2. 多源管理:通过数组或配置管理多个音频源,支持切换
  3. 定时切换:可根据时间段自动切换不同内容
  4. 优先级控制:支持紧急告警打断常规广播