您的位置:首页 > 理论基础 > 计算机网络

下载一首网络歌曲 该歌曲地址获取不到歌曲的大小,但可以正常播放

2013-10-17 10:29 393 查看

有些歌曲的下载地址,无法获得歌曲的大小:但可以正常播放

player.reset();
player.setDataSource(currSoundFileUrl);
System.out.println("yuan网址:" + currSoundFileUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepare();
soundLength = player.getDuration();

soundLength有时获取不到歌曲的大小,为0;

这时最好是下载到本地再读取本地歌曲,用完时将歌曲删除:

下面是下载该类歌曲的代码:

new AsyncTask<Void, Void, Void>() {

@Override
protected Void doInBackground(Void... params) {

try {
URL url = new URL("http://218.94.93.115:8002/audioservernews/news/file2/news/tqh/air/download/52579f60c8f27d5f81c3d8b2?rate=wma/");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");
int lene = conn.getContentLength();

InputStream is = conn.getInputStream();

BufferedInputStream bis = new BufferedInputStream(is);
int len = -1;
byte[] buf = new byte[1024 * 8];

File file = new File("/mnt/sdcard/hello.wma");

RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(0);

while((len = bis.read(buf)) != -1){
raf.write(buf, 0, len);
}

System.out.println("hello>>" + lene);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

源码下载

return null;
}
}.execute();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐