源 URL 配置
源 URL 配置
本节介绍如何管理网络音频流的 URL 配置。学习完成后,您将能够:
- 查找和验证网络电台的音频流 URL
- 管理和切换多个音频源
- 理解不同音频格式 URI 的差异
- 为工厂广播场景选择适当的音频源
在开始本节之前,请确保:
- 已完成音频流连接配置
- 了解基本的 HTTP/HTTPS URL 格式
- 能从网络电台获取流媒体 URL
Understanding Stream URLs
Section titled “Understanding Stream URLs”Common Stream URL Formats
Section titled “Common Stream URL Formats”网络电台的流 URL 有多种格式:
| 格式 | 示例 | 说明 |
|---|---|---|
| 直接 MP3 | http://example.com:8000/stream | 纯 MP3 流,最常见 |
| 播放列表 (PLS) | http://example.com/listen.pls | 包含实际流 URL 的播放列表 |
| 播放列表 (M3U) | http://example.com/listen.m3u | M3U 格式播放列表 |
| HTTPS 流 | https://example.com/stream.mp3 | 加密传输 |
| HLS 流 | http://example.com/stream.m3u8 | Apple 的 HLS 协议 |
Finding Stream URLs
Section titled “Finding Stream URLs”方法 1:社区列表
| 资源 | URL | 说明 |
|---|---|---|
| Radio Browser | https://www.radio-browser.info/ | 全球电台数据库,提供 API |
| TuneIn | https://tunein.com/ | 大量电台流 URL |
| ShoutCast | https://directory.shoutcast.com/ | 经典的网络电台目录 |
方法 2:Radio Browser API
# 搜索特定国家的电台curl "https://de1.api.radio-browser.info/json/stations/byname/Austrian%20Radio"
# 获取电台的流 URL# 返回结果中包含 url 和 url_resolved 字段方法 3:从网页解析
许多电台网站不直接显示流 URL。可以通过:
- 查看网页源代码,搜索
.mp3或.pls - 使用浏览器的开发者工具 → Network 标签 → 过滤 “audio/mpeg”
- 使用专门的电台 URL 查找工具
Stream URL Management
Section titled “Stream URL Management”Multi-Station Source Array
Section titled “Multi-Station Source Array”// 多电台 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);}URL Validation and Auto-Selection
Section titled “URL Validation and Auto-Selection”URL Format Validation
Section titled “URL Format Validation”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;}Station Auto-Selection Logic
Section titled “Station Auto-Selection Logic”// 根据时间段自动选择电台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]; // 古典/深夜音乐 }}Factory Broadcast Use Cases
Section titled “Factory Broadcast Use Cases”Typical Audio Sources
Section titled “Typical Audio Sources”| 广播类型 | 音频源类型 | 内容示例 |
|---|---|---|
| 换班提醒 | 本地 WAV/MP3 文件 | ”上午班开始,请各岗位就位” |
| 紧急告警 | 本地 WAV 文件 | ”紧急情况!请立即疏散” |
| 背景音乐 | 网络电台流 | 工厂车间背景音乐 |
| 定时通知 | 服务器合成语音(TTS) | Node-RED 生成语音并推流 |
| 生产进度广播 | 网络电台流 | 实时播报产量数据 |
Priority-Based Broadcast System
Section titled “Priority-Based Broadcast System”// 广播优先级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);}Pre-sales Key Points
Section titled “Pre-sales Key Points”广播方案考量
Section titled “广播方案考量”| 考量维度 | 说明 | 买家沟通要点 |
|---|---|---|
| 音频源 | 支持网络电台、本地文件、TTS 语音 | ”可根据需要选择多种音频源” |
| 切换策略 | 支持优先级和定时切换 | ”紧急广播自动打断背景音乐” |
| 多区管理 | 每个 ESP32 独立播放 | ”不同车间播放不同内容” |
| 音质要求 | 语音通知 64kbps,音乐 128kbps | ”语音通知品质完全够用” |
Summary
Section titled “Summary”本节介绍了音频源 URL 的配置和管理:
- URL 格式:网络电台流 URL 有多种格式,直接 MP3 URL 最易于使用
- 多源管理:通过数组或配置管理多个音频源,支持切换
- 定时切换:可根据时间段自动切换不同内容
- 优先级控制:支持紧急告警打断常规广播