简体   繁体   English

networkState,readyState无法用于音频流

[英]networkState, readyState not working for audio stream

I'm trying to check for an audio stream, to see if it is active. 我正在尝试检查音频流,以查看它是否处于活动状态。 The server is on and streaming audio or it is off and not streaming. 服务器已打开并且正在传输音频,或者服务器已关闭而不正在传输。 I found two methods, .networkState and .readyState, that work against an audio tag or an Audio() object. 我发现了两种方法,.networkState和.readyState,它们可用于音频标签或Audio()对象。 Things look okay when the stream is off and never been listened to (not in browser cache). 当流关闭并且从未被收听(不在浏览器缓存中)时,情况看起来还不错。 I have both methods being executed every 4s to catch any change in the status of the stream because I want to continuously monitor for any change and update the web page. 我想每4秒执行一次这两种方法以捕获流状态的任何变化,因为我想不断监视任何变化并更新网页。

The trouble is that I can't get either method to correctly update the status of the stream once it has changed, that is, from off to on or on to off. 麻烦的是,一旦改变了流,即从关闭到开启或从开启到关闭,我无法获得任何一种方法来正确更新流的状态。 I have to refresh the page to get a real update. 我必须刷新页面才能获得真正的更新。 Then, after you've listened to the stream and it is cached, it always shows as 'on'. 然后,在您听完流并将其缓存后,它始终显示为“ on”。

Why don't either of these methods correctly detect the change of the stream? 为什么这些方法都不能正确检测到流的变化? Is there a way to force a check against the actual object instead of the cached object? 有没有办法强制检查实际对象而不是缓存对象?

(excuse my code - I quickly chunked this together and I am a newbie) (I tried more than shown below, but tried to make this post succinct.) (对不起,我的代码-我很快将其分了块,成为了新手)(我尝试了以下所示的工作,但试图使这篇文章简洁。)

 <script> var myTimerVar = setInterval(myTimer, 4000); var myFunctionVar = setInterval(myFunction, 4000); // var aud = new Audio('http://myserver.com:8080/live.mp3'); function myTimer() { var acheck = document.getElementById("myAudio").networkState; if (acheck == 1) { document.getElementById("netstate").innerHTML = "ONLINE " + acheck; document.getElementById("netstate").style.backgroundColor = "MediumSeaGreen"; } else if (acheck == 3) { document.getElementById("netstate").innerHTML = "OFFLINE " + acheck; document.getElementById("netstate").style.backgroundColor = "yellow"; } } function myFunction() { var x = document.getElementById("myAudio").readyState; if (x == 0) { document.getElementById("readystate").innerHTML = "OFFLINE " + x; document.getElementById("readystate").style.backgroundColor = "yellow"; } else if (x == 4) { document.getElementById("readystate").innerHTML = "ONLINE " + x; document.getElementById("readystate").style.backgroundColor = "MediumSeaGreen"; } } </script> 
 <audio id="myAudio" controls> <source src="http://myserver.com:8080/live.mp3" type="audio/mpeg"> </audio> <!-- this is what does not update correctly on the page --> <p>StreamN0 is: <span id="netstate">OFFLINE</span></p> <p>StreamR0 is: <span id="readystate">OFFLINE</span></p> 

I have both methods being executed every 4s to catch any change in the status of the stream 我有两种方法每隔4秒执行一次,以捕获流状态的任何变化

Firstly, consider using the readystatechange event instead. 首先,考虑改用readystatechange事件。

const readyStates = {
  0: 'HAVE_NOTHING',
  1: 'HAVE_METADATA',
  2: 'HAVE_CURRENT_DATA',
  3: 'HAVE_FUTURE_DATA',
  4: 'HAVE_ENOUGH_DATA'
}

document.querySelector('audio#myAudio').addEventListener('readystatechange', (e) => {
  document.querySelector('#readyState').innerText = readyStates[e.readyState];
});

The trouble is that I can't get either method to correctly update the status of the stream once it has changed, that is, from off to on or on to off. 麻烦的是,一旦改变了流,即从关闭到开启或从开启到关闭,我无法获得任何一种方法来正确更新流的状态。

What I think you're asking about is whether or not the stream is actually live... online or not. 您要问的是,该流实际上是否在线(在线)直播。 That's not what the readyState indicates. 这不是readyState指示的。 The browser doesn't actually know that you're playing a live stream. 浏览器实际上并不知道您正在播放实时流。 It assumes that the data it's cached is intended to be played. 它假定要缓存其缓存的数据。 A readyState of HAVE_ENOUGH_DATA ( 4 ) just means that the browser thinks it's sufficiently buffered the media, and the rate of which it's buffered means it thinks it can continue to play without interruption . readyStateHAVE_ENOUGH_DATA4 )只是意味着浏览器认为它已经充分缓冲了媒体,并且它的缓冲速率意味着它认为可以继续播放而不会中断

The events you're probably looking for include: 您可能正在寻找的事件包括:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM