微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取音频流的状态 - HTTP 响应

如何解决获取音频流的状态 - HTTP 响应

对于你们中的一些人来说可能是一个简单的问题。

我正在尝试检查在线音频流的状态,然后在某些代码中使用该流(https://api.tmw.media/ggradio/streamhttps://api.tmw.media/ggradio/stream/ogg)。我知道如果文件不存在,我会得到 404,但我遇到了问题,如果状态为 200,我会卡在等待获取返回。关于我应该如何做的任何建议?

import React,{useState} from 'react';

    function lasttouches() {


const [active,setActive] = useState(false);
  const Press1 = () => setActive(!active);

  const buttonTextStyle = {
    color: active ? 'green' : 'red'
  };

              return (
                <Box>
                  <Box>
                    <TouchableOpacity onPress={Press1}>
                      <Text style={buttonTextStyle}>
                        Change Color
                      </Text>
                    </TouchableOpacity>
                </Box>
              );
            }
            
            export default lasttouches;

export async function startbroadcast(this: Root) { if (!this.extensions.broadcast) { this.extensions['broadcast'] = this.client.voice?.createbroadcast() if (this.config.radio?.streamLink) { if (this.extensions.broadcast) { const broadcast = this.extensions.broadcast as Voicebroadcast let tested: number = 0 for (const link of this.config.radio.streamLink) { const req = await this.fetch(link) console.log(req) if (!req.ok) { this.log(LoggingLevels.error,`${link} is 404`) tested++ if (tested == this.config.radio.streamLink.length) { this.log( LoggingLevels.emergency,`All stream links Failed. Radio is likely offline` ) throw new Error('All stream links Failed') } return } const broadcastOptions: StreamOptions = { highWaterMark: 50,volume: false } this.log( LoggingLevels.debug,`Starting up broadcast using stream link: ${link}` ) broadcast.play(link,broadcastOptions) break } } } } } https://www.npmjs.com/package/node-fetch

解决方法

我不完全确定我进行了哪些更改以使其正常工作,但这是工作代码。

export async function startBroadcast(this: Root) {
  if (!this.extensions.broadcast) {
    this.extensions['broadcast'] = this.client.voice?.createBroadcast()
    if (this.config.radio?.streamLink) {
      if (this.extensions.broadcast) {
        const broadcast = this.extensions.broadcast as VoiceBroadcast
        let tested: number = 0
        for (const link of this.config.radio.streamLink) {
          console.log(link)
          const req = await this.fetch(link)
          console.log(req)
          if (!req.ok) {
            this.log(LoggingLevels.alert,`${link} is 404`)
            tested++
            if (tested == this.config.radio.streamLink.length) {
              this.log(
                LoggingLevels.emergency,`All stream links failed. Radio is likely offline`
              )
              throw new Error('All stream links failed')
            }
          } else {
            const broadcastOptions: StreamOptions = {
              highWaterMark: 50,volume: false
            }
            this.log(
              LoggingLevels.debug,`Starting up broadcast using stream link: ${link}`
            )

            broadcast.play(link,broadcastOptions)
            break
          }
        }
      }
    }
  }
}

我认为是我在循环内部返回的方式实际上返回了函数。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。