问题描述
为了解决同源问题,其实可能也不是同源问题,反正我的audio标签不能播放音乐,显示403错误,但是那个链接是可以下载的,在mxplayer上播放网络流也是可以播放的,但是在audio上就是不行,所以我用服务器转发了一下那个歌曲文件,就不存在授权问题了,但是转发之后的歌曲文件播放起来一卡一卡的,有时候还会自己跳一小段,这就说明数据不连续,研究了一会儿之后想到,输入流是从网络上来的,有可能输出流输出速度太快,超过了输入流导致了一些数据损失,所以在输出部分限制了速度。
##解决代码
public InputStream getMusic(Integer musicid,HttpServletResponse response) throws NumberFormatException, HttpException, IllegalArgumentException, JSONException, IOException, InterruptedException{
yData.musicMapper = musicMapper;
String url = yData.findMusicByIdFromBaidu(musicid).get("file_link").toString();
OutputStream os = response.getOutputStream();
InputStream in = yData.getMusicByte(url);
byte[] b = new byte[1024];
while( in.read(b)!= -1){
Thread.sleep(10);
os.write(b);
}
in.close();
os.flush();
os.close();
return null;
}
public InputStream getMusicByte(String url) throws IOException{
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
client.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
// 执行并返回状态
int status = client.executeMethod(getMethod);
InputStream is = getMethod.getResponseBodyAsStream();
return is;
}