Android 使用URLConnection下載音頻文件的方法
使用MediaPlayer 在線音頻,請參考Android MediaPlayer 音頻
有時候我們會需要下載音頻文件。這里提供一種思路,將在線音頻文件通過流寫到本地文件中。
使用URLConnection來建立連接,獲取到的數(shù)據(jù)寫到文件中。
URLConnection建立連接后,可以獲取到數(shù)據(jù)長度。由此我們可以計算出下載進(jìn)度。
public class DownloadStreamThread extends Thread { String urlStr; final String targetFileAbsPath; public DownloadStreamThread(String urlStr, String targetFileAbsPath) { this.urlStr = urlStr; this.targetFileAbsPath = targetFileAbsPath; } @Override public void run() { super.run(); int count; File targetFile = new File(targetFileAbsPath); try { boolean n = targetFile.createNewFile(); Log.d(TAG, "Create new file: " + n + ", " + targetFile); } catch (IOException e) { Log.e(TAG, "run: ", e); } try { URL url = new URL(urlStr); URLConnection connection = url.openConnection(); connection.connect(); int contentLength = connection.getContentLength(); InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream(targetFileAbsPath); byte[] buffer = new byte[1024]; long total = 0; while ((count = input.read(buffer)) != -1) { total += count; Log.d(TAG, String.format(Locale.CHINA, "Download progress: %.2f%%", 100 * (total / (double) contentLength))); output.write(buffer, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e) { Log.e(TAG, "run: ", e); } } }
啟動下載,即啟動線程。
new DownloadStreamThread(urlStr, targetFileAbsPath).start();
值得注意的是,如果本地已經(jīng)有了文件,需要做一些邏輯判斷。例如是否刪掉舊文件,重新下載?;蚴桥袛喑鲆延形募?,中止此次下載任務(wù)。
例如可以用connection.getContentLength()
與當(dāng)前文件長度來比較,如果不一致,則刪掉本地文件,重新下載。
實(shí)際上,URLConnection能處理很多流媒體。在這里是用來下載音頻文件??梢詫?shí)現(xiàn)下載功能和類似“邊下邊播”的功能。
代碼可以參考示例工程: https://github.com/RustFisher/android-MediaPlayer
總結(jié)
以上所述是小編給大家介紹的Android 使用URLConnection下載音頻文件的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
欄 目:Android
本文標(biāo)題:Android 使用URLConnection下載音頻文件的方法
本文地址:http://mengdiqiu.com.cn/a1/Android/9172.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實(shí)現(xiàn)簡單計算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文