gpt4 book ai didi

android - 在 Android 上接收 ShoutCast 流的信息

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:38 25 4
gpt4 key购买 nike

我目前正在制作一个应用程序以配合我的在线广播网站,我正在使用 Android 2.2 (API 8) 对其进行编码,并且我已经通过两个按钮获得了 Shoutcast Stream。

这是我的主类的代码:

 public class GrooveOfMusicRadioActivity extends Activity {
/** Called when the activity is first created. */


MediaPlayer mediaPlayer;
Button start, stop;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

start = (Button) findViewById(R.id.button1);
stop = (Button) findViewById(R.id.button2);




start.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mediaPlayer.start();

}
});
stop.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mediaPlayer.pause();
}
});





String url = "http://67.212.165.106:8161"; // your URL here
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}


}

所以我想知道如何接收流标题、歌曲、艺术家等。并让它出现

主要的 XML 是相对布局的

谢谢,在编程方面我完全是菜鸟。

谢谢马克:)

最佳答案

我只需要自己获取元数据,我基本上做了同样的事情:Pulling Track Info From an Audio Stream Using PHP .标题中有很多数据,因此您可以使用它们,但我想要的只是流标题,这就是我得到的。

Activity mainAct = this;
public void getNowPlaying(View v) {
Log.w("getNowPlaying", "fired");
new Thread(new Runnable() {
public void run() {
String title = null, djName = null;
try {
URL updateURL = new URL(YOUR_STREAM_URL_HERE);
URLConnection conn = updateURL.openConnection();
conn.setRequestProperty("Icy-MetaData", "1");
int interval = Integer.valueOf(conn.getHeaderField("icy-metaint")); // You can get more headers if you wish. There is other useful data.

InputStream is = conn.getInputStream();

int skipped = 0;
while (skipped < interval) {
skipped += is.skip(interval - skipped);
}

int metadataLength = is.read() * 16;

int bytesRead = 0;
int offset = 0;
byte[] bytes = new byte[metadataLength];

while (bytesRead < metadataLength && bytesRead != -1) {
bytesRead = is.read(bytes, offset, metadataLength);
offset = bytesRead;
}

String metaData = new String(bytes).trim();
title = metaData.substring(metaData.indexOf("StreamTitle='") + 13, metaData.indexOf(" / ", metaData.indexOf("StreamTitle='"))).trim();
djName = metaData.substring(metaData.indexOf(" / ", metaData.indexOf("StreamTitle='")) + 3, metaData.indexOf("';", metaData.indexOf("StreamTitle='"))).trim();
Log.w("metadata", metaData);
is.close();
} catch (MalformedURLException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace(); }

final String titleFin = title;
final String djNameFin = djName;
mainAct.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(mainAct, titleFin + "\n" + djNameFin, Toast.LENGTH_SHORT).show();
}
});
}
}).start();
}

关于android - 在 Android 上接收 ShoutCast 流的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8865199/

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