1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Android如何使用URLConnection下載音頻文件

      這篇文章主要為大家展示了“Android如何使用URLConnection下載音頻文件”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android如何使用URLConnection下載音頻文件”這篇文章吧。

      創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供霞山網(wǎng)站建設(shè)、霞山做網(wǎng)站、霞山網(wǎng)站設(shè)計(jì)、霞山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、霞山企業(yè)網(wǎng)站模板建站服務(wù),十余年霞山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

      使用MediaPlayer播放在線音頻,請參考Android MediaPlayer 播放音頻

      有時(shí)候我們會需要下載音頻文件。這里提供一種思路,將在線音頻文件通過流寫到本地文件中。

      使用URLConnection來建立連接,獲取到的數(shù)據(jù)寫到文件中。

      URLConnection建立連接后,可以獲取到數(shù)據(jù)長度。由此我們可以計(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)下載功能和類似“邊下邊播”的功能。

      以上是“Android如何使用URLConnection下載音頻文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


      本文題目:Android如何使用URLConnection下載音頻文件
      分享地址:http://www.ef60e0e.cn/article/poocdp.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        鄢陵县| 赤壁市| 余姚市| 教育| 五莲县| 临颍县| 特克斯县| 伊川县| 阳城县| 鄢陵县| 信阳市| 祁阳县| 湟源县| 定州市| 宝应县| 定襄县| 兰州市| 鹤山市| 法库县| 轮台县| 全南县| 靖宇县| 上犹县| 饶阳县| 牡丹江市| 东明县| 洞口县| 南川市| 惠安县| 荆门市| 东兰县| 两当县| 双流县| 尉犁县| 察哈| 确山县| 连南| 桓仁| 浦东新区| 阳东县| 襄垣县|