|
@@ -3,6 +3,7 @@ package ipcast
|
|
|
import (
|
|
|
"crypto/rand"
|
|
|
"encoding/hex"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/goccy/go-json"
|
|
@@ -11,6 +12,7 @@ import (
|
|
|
"lcfns/model/common/response"
|
|
|
ipResp "lcfns/model/ipcast/response"
|
|
|
"net/http"
|
|
|
+ "os"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
)
|
|
@@ -19,7 +21,11 @@ type IpcastApi struct {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Play(c *gin.Context) {
|
|
|
- url := GetIpcastUrl(c, Play)
|
|
|
+ url, err := GetIpcastUrl(c, Play)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
data, err := c.GetRawData()
|
|
|
if err != nil {
|
|
|
response.FailWithMessage("获取请求参数出错", c)
|
|
@@ -38,7 +44,11 @@ func (api *IpcastApi) Play(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Stop(c *gin.Context) {
|
|
|
- url := GetIpcastUrl(c, Stop)
|
|
|
+ url, err := GetIpcastUrl(c, Stop)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
all, err := ipcastService.Common(url, http.MethodDelete, nil)
|
|
|
if err != nil {
|
|
|
response.Fail(c)
|
|
@@ -51,7 +61,11 @@ func (api *IpcastApi) Stop(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Status(c *gin.Context) {
|
|
|
- url := GetIpcastUrl(c, Status)
|
|
|
+ url, err := GetIpcastUrl(c, Status)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
all, err := ipcastService.Common(url, http.MethodGet, nil)
|
|
|
if err != nil {
|
|
|
response.Fail(c)
|
|
@@ -64,7 +78,11 @@ func (api *IpcastApi) Status(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Ping(c *gin.Context) {
|
|
|
- url := GetIpcastUrl(c, Ping)
|
|
|
+ url, err := GetIpcastUrl(c, Ping)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
all, err := ipcastService.Common(url, http.MethodGet, nil)
|
|
|
if err != nil {
|
|
|
response.Fail(c)
|
|
@@ -77,7 +95,11 @@ func (api *IpcastApi) Ping(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Volume(c *gin.Context) {
|
|
|
- url := GetIpcastUrl(c, Volume)
|
|
|
+ url, err := GetIpcastUrl(c, Volume)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
data, err := c.GetRawData()
|
|
|
if err != nil {
|
|
|
response.FailWithMessage("获取请求参数出错", c)
|
|
@@ -113,7 +135,7 @@ var ipcastData = `
|
|
|
"url": "http://192.168.110.69:8889/ipcast/AudioSource/%s",
|
|
|
"sync": false,
|
|
|
"queue": true,
|
|
|
- "volume": 50,
|
|
|
+ "volume": 70,
|
|
|
"loop": {
|
|
|
"duration": %s,
|
|
|
"times": 1,
|
|
@@ -124,6 +146,7 @@ var ipcastData = `
|
|
|
|
|
|
// PlayAudio 前端传递mp3资源
|
|
|
func (api *IpcastApi) PlayAudio(c *gin.Context) {
|
|
|
+ fmt.Println("headers:", c.Request.Header)
|
|
|
//生成uuid
|
|
|
uuid := make([]byte, 8)
|
|
|
rand.Read(uuid)
|
|
@@ -136,21 +159,36 @@ func (api *IpcastApi) PlayAudio(c *gin.Context) {
|
|
|
}()
|
|
|
}()
|
|
|
//读取mp3数据
|
|
|
- //mp3, err := c.GetRawData()
|
|
|
- mp3, err := c.FormFile("mp3")
|
|
|
+ mp3, err := c.FormFile("file")
|
|
|
if err != nil {
|
|
|
response.FailWithMessage("获取请求参数出错", c)
|
|
|
logrus.Error(err)
|
|
|
return
|
|
|
}
|
|
|
open, err := mp3.Open()
|
|
|
+ if err != nil {
|
|
|
+ logrus.Error("open err ", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
readAll, err := io.ReadAll(open)
|
|
|
+ if err != nil {
|
|
|
+ logrus.Error("ReadAll err ", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
//保存到内存
|
|
|
audio[uuidString] = readAll
|
|
|
+ os.WriteFile("map.wav", readAll, 0644)
|
|
|
//播放
|
|
|
t := c.PostForm("timeLength")
|
|
|
data := []byte(fmt.Sprintf(ipcastData, uuidString, t))
|
|
|
- all, err := ipcastService.Common(GetIpcastUrl(c, Play), http.MethodPost, data)
|
|
|
+ fmt.Println("data:", string(data))
|
|
|
+ url, err := GetIpcastUrl(c, Play)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("url:", url)
|
|
|
+ all, err := ipcastService.Common(url, http.MethodPost, data)
|
|
|
if err != nil {
|
|
|
response.Fail(c)
|
|
|
logrus.Error(err)
|
|
@@ -170,12 +208,12 @@ func (api *IpcastApi) AudioSource(c *gin.Context) {
|
|
|
id := c.Param("id")
|
|
|
fmt.Println("id:", id)
|
|
|
// 设置响应头,指定内容类型为 audio/mpeg
|
|
|
- c.Header("Content-Type", "audio/mp3")
|
|
|
+ c.Header("Content-Type", "audio/wav")
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
|
c.Header("Content-Length", strconv.Itoa(len(audio[id])))
|
|
|
|
|
|
// 将内存中的 MP3 数据作为响应返回给浏览器
|
|
|
- c.Data(http.StatusOK, "audio/mp3", audio[id])
|
|
|
+ c.Data(http.StatusOK, "audio/wav", audio[id])
|
|
|
}
|
|
|
|
|
|
//↑实现语音喊话功能↑
|
|
@@ -188,21 +226,21 @@ const (
|
|
|
Volume
|
|
|
)
|
|
|
|
|
|
-func GetIpcastUrl(c *gin.Context, expr byte) string {
|
|
|
+func GetIpcastUrl(c *gin.Context, expr byte) (string, error) {
|
|
|
baseUrl := IpcastBaseUrl(c)
|
|
|
if baseUrl == "" {
|
|
|
- response.FailWithMessage("获取baseUrl失败", c)
|
|
|
- return ""
|
|
|
+ return "", errors.New("获取baseUrl失败")
|
|
|
}
|
|
|
switch expr {
|
|
|
case Stop, Play:
|
|
|
- return baseUrl + "/v1/speech"
|
|
|
+ return baseUrl + "/v1/speech", nil
|
|
|
case Status:
|
|
|
- return baseUrl + "/v1/play_status"
|
|
|
+ return baseUrl + "/v1/play_status", nil
|
|
|
case Ping:
|
|
|
- return baseUrl + "/v1/check_alive"
|
|
|
+ return baseUrl + "/v1/check_alive", nil
|
|
|
case Volume:
|
|
|
- return baseUrl + "/v1/volume"
|
|
|
+ return baseUrl + "/v1/volume", nil
|
|
|
+ default:
|
|
|
+ return "", errors.New("获取baseUrl失败")
|
|
|
}
|
|
|
- return ""
|
|
|
}
|