zhibo/android-app/问题修复总结.md
xiao12feng@outlook.com 40bfd4ec5c 直播功能正常使用
2025-12-17 08:47:15 +08:00

161 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Android 直播应用问题修复总结
## 问题现象
OBS 推流成功,但 Android 模拟器显示"主播尚未开播"(未开播状态)
## 根本原因
### 1. SRS API URL 格式错误 ❌
后端代码访问 SRS API 时缺少末尾斜杠:
- 错误:`http://localhost:1985/api/v1/streams?count=100`
- 正确:`http://localhost:1985/api/v1/streams/?count=100`
SRS 服务器对缺少斜杠的请求返回 302 重定向,导致后端无法获取推流状态。
### 2. 缺少 SRS_API_PORT 配置 ❌
`.env` 文件中没有配置 `SRS_API_PORT=1985`,虽然代码有默认值,但明确配置更好。
### 3. 房间数据未持久化 ❌
重启服务器后,内存中的房间数据丢失,导致推流无法关联到房间。
## 已完成的修复 ✅
### 1. 修复 SRS API URL
**文件**`live-streaming/server/utils/srsHttpApi.js`
```javascript
// 修复前
const url = `http://${host}:${apiPort}/api/v1/streams?count=100`;
// 修复后
const url = `http://${host}:${apiPort}/api/v1/streams/?count=100`;
```
### 2. 添加 SRS_API_PORT 配置
**文件**`live-streaming/.env`
```env
SRS_API_PORT=1985
```
### 3. 实现房间数据持久化
**文件**`live-streaming/server/store/roomStore.js`
- 添加了文件存储功能
- 房间数据保存在 `live-streaming/data/rooms.json`
- 服务器重启后自动加载房间数据
### 4. 修复 RTMP 地址格式
**文件**`live-streaming/server/utils/streamUrl.js`
```javascript
// 修复前
rtmp: `rtmp://${host}:${rtmpPort}/live`,
// 修复后
rtmp: `rtmp://${host}:${rtmpPort}/live/${streamKey}`,
```
### 5. 改进错误日志
添加了更详细的 SRS API 错误信息,便于调试。
## 验证结果 ✅
测试 API 返回:
```json
{
"isLive": true,
"streamKey": "868c49cc-1021-4664-95a3-ed71e789adb2",
"streamUrls": {
"rtmp": "rtmp://localhost:1935/live/868c49cc-1021-4664-95a3-ed71e789adb2",
"flv": "http://localhost:8080/live/868c49cc-1021-4664-95a3-ed71e789adb2.flv",
"hls": "http://localhost:8080/live/868c49cc-1021-4664-95a3-ed71e789adb2.m3u8"
}
}
```
## 使用说明
### 1. 启动服务
```bash
cd live-streaming
node server/index.js
```
### 2. 在 Android 应用中
- 打开应用,查看房间列表
- 点击房间进入,应该显示"直播中"状态
- 视频播放器会自动加载 HLS 流
### 3. OBS 推流设置
- 服务器:`rtmp://localhost:1935/live/868c49cc-1021-4664-95a3-ed71e789adb2`
- 或者分开填写:
- 服务器:`rtmp://localhost:1935/live`
- 串流密钥:`868c49cc-1021-4664-95a3-ed71e789adb2`
## 技术架构
```
┌─────────────┐
│ OBS 推流 │
└──────┬──────┘
│ RTMP (1935)
┌─────────────────┐
│ SRS 服务器 │
│ (Docker) │
│ - RTMP: 1935 │
│ - HTTP: 8080 │
│ - API: 1985 │
└────────┬────────┘
│ HTTP API (1985)
┌─────────────────┐
│ Node.js 后端 │
│ (端口 3001) │
└────────┬────────┘
│ REST API
┌─────────────────┐
│ Android 应用 │
│ (模拟器) │
└─────────────────┘
```
## 网络地址映射
| 位置 | localhost | 10.0.2.2 | 说明 |
|------|-----------|----------|------|
| 电脑 OBS | ✅ | ❌ | 使用 localhost |
| Android 模拟器 | ❌ | ✅ | 使用 10.0.2.2 |
| 后端服务器 | ✅ | ✅ | 监听 0.0.0.0 |
## 常见问题
### Q: 重启服务器后房间消失?
A: 已修复!现在房间数据会保存在 `live-streaming/data/rooms.json`
### Q: Android 应用显示"未开播"
A: 检查:
1. OBS 是否正在推流
2. 后端服务器是否运行
3. SRS Docker 容器是否运行:`docker ps`
4. 查看后端日志是否有 SRS API 错误
### Q: 视频播放黑屏?
A: 可能原因:
1. HLS 转码未启用(需要 FFmpeg
2. Android 播放器不支持 FLV
3. 网络延迟或缓冲
### Q: 如何查看 SRS 推流状态?
A: 访问 `http://localhost:1985/api/v1/streams/`
## 相关文件
- 后端服务器:`live-streaming/server/index.js`
- SRS API 工具:`live-streaming/server/utils/srsHttpApi.js`
- 流地址生成:`live-streaming/server/utils/streamUrl.js`
- 房间存储:`live-streaming/server/store/roomStore.js`
- 环境配置:`live-streaming/.env`
- SRS 配置:`live-streaming/docker/srs/srs.conf`
- Android 配置:`android-app/app/build.gradle.kts`