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