gpt4 book ai didi

android - 如何获取流式在线广播的元数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:41 26 4
gpt4 key购买 nike

我正在 Android 2.2 中开发应用程序。我需要获取流式在线广播的元数据,例如 shoutcast。

我使用 IcyStreamMeta 类:

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((String)m.group(1), (String)m.group(2));
}
}

return metadata;
}
}

我的主要 Activity 是使用函数:

public void getMeta(){

timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {

URL url;
//Message msg = handler.obtainMessage();
try {
Log.d("Metadatos","Dentro del try para obtener los metadatos");
url = new URL("http://relay5.slayradio.org:8000");
IcyStreamMeta icy = new IcyStreamMeta(url);
//Log.d("Metadatos","El metadata de la url es "+icy.getMetadata().toString());

Log.d("Metadatos","El titulo de la cancion es "+icy.getTitle());
//msg.obj = icy.getTitle();

Log.d("Metadatos","El nombre del artista es "+icy.getArtist());
//handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}, 0, 10000);

}

当我运行应用程序时,不获取元数据。我已经看过所有帖子,但找不到解决方案。我需要你的帮助。

问候,胡安。

最佳答案

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
{
protected URL streamUrl;
private Map<String, String> metadata;
private boolean isError;
private Map<String, String> data;

public IcyStreamMeta()
{
isError = false;
}

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

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

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

/**
* Get streamTitle
*
* @return String
* @throws IOException
*/
public String getStreamTitle() throws IOException
{
data = getMetadata();

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

return data.get("StreamTitle");
}

/**
* Get title using stream's title
*
* @return String
* @throws IOException
*/
public String getTitle() throws IOException {
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;
}

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

synchronized 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((String)m.group(1), (String)m.group(2));
}
}

return metadata;
}}

在第二类中创建AsyncTask

protected class MetadataTask2 extends AsyncTask<URL, Void, IcyStreamMeta> 
{
@Override
protected IcyStreamMeta doInBackground(URL... urls)
{
try
{
streamMeta.refreshMeta();
Log.e("Retrieving MetaData","Refreshed Metadata");
}
catch (IOException e)
{
Log.e(MetadataTask2.class.toString(), e.getMessage());
}
return streamMeta;
}

@Override
protected void onPostExecute(IcyStreamMeta result)
{
try
{
title_artist=streamMeta.getStreamTitle();
Log.e("Retrieved title_artist", title_artist);
if(title_artist.length()>0)
{
updateMetadata();
}
metaFlag=true;
}
catch (IOException e)
{
Log.e(MetadataTask2.class.toString(), e.getMessage());
}
}
}

然后如下调用AsyncTask

    streamMeta = new IcyStreamMeta();
streamMeta.setStreamUrl(new URL(ConstantData.currentStationData.getUrl()));
metadataTask2=new MetadataTask2();
metadataTask2.execute(new URL(ConstantData.currentStationData.getUrl()));

在Timer的Run方法中编写代码

    {
streamMeta.refreshMeta();
String title_artist=streamMeta.getStreamTitle();
}

上面的代码对我来说效果很好。如果对格式有任何疑问和抱歉,请告诉我。

关于android - 如何获取流式在线广播的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970548/

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