南京百度网站制作/网站维护一年一般多少钱?
遇到此问题时,我决定在打开播放器之前测试流是否可用。 如果您让用户等待很长时间,并且音乐会开始正常播放(不是,但可以说可以)。最坏的情况是让他等待很长时间,音乐将永远不会开始!因此,我们有两种情况:
直播流场景,例如广播电台。
录制的mp3文件可在线获得。
在无线电场景中,我们可以检查端口是否正在接受连接(打开/关闭状态)。 如果已打开,请为播放器准备音乐,否则完全不要准备。
public static boolean isLiveStreamingAvailable() {
SocketAddress sockaddr = new InetSocketAddress(STREAMING_HOST, STREAMING_PORT);
// Create your socket
Socket socket = new Socket();
boolean online = true;
// Connect with 10 s timeout
try {
socket.connect(sockaddr, 10000);
} catch (SocketTimeoutException stex) {
// treating timeout errors separately from other io exceptions
// may make sense
return false;
} catch (IOException iOException) {
return false;
} finally {
// As the close() operation can also throw an IOException
// it must caught here
try {
socket.close();
} catch (IOException ex) {
// feel free to do something moderately useful here, eg log the event
}
}
return true;
}
在mp3文件的情况下,情况有所不同。 您应该检查http请求后是否包含响应代码。
public static boolean isRecordedStreamingAvailable() {
try {
HttpURLConnection.setFollowRedirects(false);
// note : you may also need
// HttpURLConnection.setInstanceFollowRedirects(false)
HttpURLConnection con =
(HttpURLConnection) new URL(RECORDED_URL).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}