import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import java.io.*;
import java.net.*;
import java.util.Random;
public class App {
public static void main(String[] args) throws IOException {
int anInt;
String idArray[] = geMusicIdArray("虫儿飞");
if (idArray != null){
do {
Random random = new Random();
anInt = random.nextInt(10);
System.out.println(anInt);
}while (!play(getUrl(idArray[anInt])));
}
}
//获取音乐id数组
public static String[] geMusicIdArray(String musicName){
String jsonStr = null;
String id[] = null;
try {
String str = URLEncoder.encode(musicName, "utf-8").replaceAll("\\+", "");
// 转换成encode
URL url = new URL("http://music.163.com/api/search/pc?s=" + str + "&type=1");
//拼接url
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setConnectTimeout(3000);
httpCon.setDoInput(true);
httpCon.setRequestMethod("GET");
// 获取相应码
int respCode = httpCon.getResponseCode();
if (respCode == 200) {
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
while ((len = httpCon.getInputStream().read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
System.out.println(jsonStr);
JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(JSONObject.parseObject(jsonStr).getString("result")).getString("songs"));
//jsonArray 内就是歌曲信息了 之后可以用jsonArray.getJSONObject(0).getString(/*这里放你要获取的字段名如id*/)获取详细信息
id = new String[10];
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println(jsonArray.getJSONObject(i).getString("id"));
id[i] = jsonArray.getJSONObject(i).getString("id");
}
} else {
System.out.println("网易云错误,错误码为" + respCode);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return id;
}
//播放url音乐
public static boolean play(String url) throws MalformedURLException {
URL url1 = new URL(url);
BufferedInputStream buffer = null;
try {
URLConnection connection = url1.openConnection();
buffer = new BufferedInputStream(connection.getInputStream());
if (buffer.read() == -1){
System.out.println("不可用的url:" + url);
return false;
}
// Bitstream bitstream = new Bitstream(buffer);
Player player = new Player(buffer);
player.play();
} catch (IOException e) {
e.printStackTrace();
} catch (JavaLayerException e) {
e.printStackTrace();
} finally {
if (buffer != null){
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static String getUrl(String id) {
return "http://music.163.com/song/media/outer/url?id=" + id + ".mp3";
}
}
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
<dependency>
<groupId>javazoom</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1</version>
</dependency>