我正在开发一个播放窗体的网址的播放器
shoucast http://292.3.23.23:8000是,因为我可以恢复
元数据?艺术家名称mediaplayer使用标题等
没有问题,但我无法检索元数据
任何人都知道我能做什么,并在文本中显示
titulo.settext(title);
shoucast http://292.3.23.23:8000是,因为我可以恢复
元数据?艺术家名称mediaplayer使用标题等
没有问题,但我无法检索元数据
任何人都知道我能做什么,并在文本中显示
titulo.settext(title);
解决方法
检查这个
http://developer.android.com/reference/android/media/MediaMetadataRetriever.html,但它在API LEVEL 10
谢谢.
我已经使用线程,而不是伟大的解决方案,但它的作品
public class IcyStreamMeta { 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; }
}
线程调用每10秒
public void startThread(){ timer = new Timer(); timer.schedule(new TimerTask() { public void run() { URL url; Message msg = handler.obtainMessage(); try { url = new URL(URL); IcyStreamMeta icy = new IcyStreamMeta(url); Log.d("SONG",icy.getTitle()); msg.obj = icy.getTitle(); Log.d("ARTITSi",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(); } } },10000); }