gpt4 book ai didi

java - 从 shoutcast 流中获取元数据

转载 作者:太空狗 更新时间:2023-10-29 13:16:25 25 4
gpt4 key购买 nike

我正在开发一个带有多个 radio 的 radio 应用程序,流媒体播放正常。但我目前正在努力显示艺术家和正在播放的音乐。

这是我用来从 shoutcast 流中获取元数据的类:

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IcyStreamMeta<Message> {

protected URL streamUrl;
private Map<String, String> metadata;
private boolean isError;

public IcyStreamMeta(URL streamUrl) {
setStreamUrl(streamUrl);

isError = false;
}

/**
* Get artist using stream's title
*
* @return String
* @throws IOException
*/
public String getArtist() throws IOException {
Map<String, String> data = getMetadata();

if (!data.containsKey("StreamTitle"))
return "";

String streamTitle = data.get("StreamTitle");
String title = streamTitle.substring(0, streamTitle.indexOf("-"));
return title.trim();
}

/**
* Get title using stream's title
*
* @return String
* @throws IOException
*/
public String getTitle() throws IOException {
Map<String, String> data = getMetadata();

if (!data.containsKey("StreamTitle"))
return "";

String streamTitle = data.get("StreamTitle");
String artist = streamTitle.substring(streamTitle.indexOf("-")+1);
return artist.trim();
}

public Map<String, String> getMetadata() throws IOException {
if (metadata == null) {
refreshMeta();
}

return metadata;
}

public void refreshMeta() throws IOException {
retreiveMetadata();
}

private void retreiveMetadata() throws IOException {
URLConnection con = streamUrl.openConnection();
con.setRequestProperty("Icy-MetaData", "1");
con.setRequestProperty("Connection", "close");
con.setRequestProperty("Accept", null);
con.connect();

int metaDataOffset = 0;
Map<String, List<String>> headers = con.getHeaderFields();
InputStream stream = con.getInputStream();

if (headers.containsKey("icy-metaint")) {
// Headers are sent via HTTP
metaDataOffset = Integer.parseInt(headers.get("icy-metaint").get(0));
} else {
// Headers are sent within a stream
StringBuilder strHeaders = new StringBuilder();
char c;
while ((c = (char)stream.read()) != -1) {
strHeaders.append(c);
if (strHeaders.length() > 5 && (strHeaders.substring((strHeaders.length() - 4), strHeaders.length()).equals("\r\n\r\n"))) {
// end of headers
break;
}
}

// Match headers to get metadata offset within a stream
Pattern p = Pattern.compile("\\r\\n(icy-metaint):\\s*(.*)\\r\\n");
Matcher m = p.matcher(strHeaders.toString());
if (m.find()) {
metaDataOffset = Integer.parseInt(m.group(2));
}
}

// In case no data was sent
if (metaDataOffset == 0) {
isError = true;
return;
}

// Read metadata
int b;
int count = 0;
int metaDataLength = 4080; // 4080 is the max length
boolean inData = false;
StringBuilder metaData = new StringBuilder();
// Stream position should be either at the beginning or right after headers
while ((b = stream.read()) != -1) {
count++;

// Length of the metadata
if (count == metaDataOffset + 1) {
metaDataLength = b * 16;
}

if (count > metaDataOffset + 1 && count < (metaDataOffset + metaDataLength)) {
inData = true;
} else {
inData = false;
}
if (inData) {
if (b != 0) {
metaData.append((char)b);
}
}
if (count > (metaDataOffset + metaDataLength)) {
break;
}

}

// Set the data
metadata = IcyStreamMeta.parseMetadata(metaData.toString());

// Close
stream.close();
}

public boolean isError() {
return isError;
}

public URL getStreamUrl() {
return streamUrl;
}

public void setStreamUrl(URL streamUrl) {
this.metadata = null;
this.streamUrl = streamUrl;
this.isError = false;
}

public static Map<String, String> parseMetadata(String metaString) {
Map<String, String> metadata = new HashMap();
String[] metaParts = metaString.split(";");
Pattern p = Pattern.compile("^([a-zA-Z]+)=\\'([^\\']*)\\'$");
Matcher m;
for (int i = 0; i < metaParts.length; i++) {
m = p.matcher(metaParts[i]);
if (m.find()) {
metadata.put(m.group(1), m.group(2));
}
}

return metadata;
}
}

以及 MainActivity 上每 10 秒获取元数据的方法

private void getMeta()
{
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
try {
IcyStreamMeta icy = new IcyStreamMeta(new URL(RadiophonyService.getRadioURL()));
final String data = icy.getArtist() + " - " + icy.getTitle();
final TextView meta = (TextView) findViewById(R.id.now_playing);

runOnUiThread(new Runnable() {
public void run() {
meta.setText(data);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 10000);
}

最初,当我选择一个电台时,它播放但不显示元数据,当我选择另一个电台时,应用程序崩溃并出现此运行时错误:

E/AndroidRuntime:致命异常:Timer-0 进程:com.example.app,PID:23597

java.lang.StringIndexOutOfBoundsException: 长度=0;区域开始=0;区域长度=-1 在 java.lang.String.startEndAndLength(String.java:504) 在 java.lang.String.substring(String.java:1333) 在 com.example.app.utilities.IcyStreamMeta.getArtist(IcyStreamMeta.java:41) 在 com.example.app.activities.MainActivity$8.run(MainActivity.java:306) 在 java.util.Timer$TimerImpl.run(Timer.java:284)

我被困在这个问题上好几天了,我尝试了其他解决方案但没有任何效果!

最佳答案

看起来像这条线

String title = streamTitle.substring(0, streamTitle.indexOf("-"));

是罪魁祸首。

如果 streamTitle 的标题中没有破折号,indexOf() 将返回 -1,而 substring() 令人窒息因为结束索引不能小于起始索引。

也许你需要这样的东西:

int pos = streamTitle.indexOf("-");
String title = (pos == -1) ? streamTitle : streamTitle.substring(0, pos);

关于java - 从 shoutcast 流中获取元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34267353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com