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

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

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();



 

你可能感兴趣的:(下载)