问题描述
对于你们中的一些人来说可能是一个简单的问题。
我正在尝试检查在线音频流的状态,然后在某些代码中使用该流(https://api.tmw.media/ggradio/stream 和 https://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
}
}
}
}
}
}
我认为是我在循环内部返回的方式实际上返回了函数。