javascript - 获取音频流的状态 - 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.fetch
是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
}
}
}
}
}
}
我认为这是我在循环内部返回的方式实际上是在返回函数。
推荐阅读
- apache-poi - 如何修复“无效的行号(-4)超出允许范围(0..1048575)”?
- python - Snakemake 删除 --config 值中的下划线?
- php - 使用 mailtrap 在 laravel 中发送电子邮件
- google-cloud-platform - public instance CNAME for GCP compute instance
- node.js - 使用 Promise 在 MongoDB 中插入记录的 Node.js 程序示例
- javascript - How to have aspect ratio by parent height?
- django - 如何模拟原生 CREATE 响应
- windows - How to access a different console, such as writing to its input buffer
- java - 如何将 JSON 字段的各种可能类型映射到 POJO 属性?
- c++ - C - 外部结构