首页 > 解决方案 > 获取音频流的状态 - HTTP 响应

问题描述

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

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

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
        }
      }
    }
  }
}

this.fetchhttps://www.npmjs.com/package/node-fetch

标签: javascripttypescriptfetchaudio-streamingnode-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
          }
        }
      }
    }
  }
}

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


推荐阅读