Android downloads audio files using urlconnection

Link to this article: Android downloads audio files using urlconnection

Use mediaplayer to play online audio, please refer to Android mediaplayer to play audio

Sometimes we need to download audio files. Here is an idea to write online audio files to local files through streaming. Use urlconnection to establish a connection and write the obtained data to a file.

After urlconnection is established, the data length can be obtained. From this, we can calculate the download progress.

    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,count);
                }
                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {
                Log.e(TAG,e);
            }
        }
    }

Start the download, that is, start the thread.

new DownloadStreamThread(urlStr,targetFileAbsPath).start();

It is worth noting that if there are already local files, some logical judgment needs to be made. For example, whether to delete the old file and download it again. Or judge the existing file and abort the download task. For example, you can use connection. Getcontentlength () to compare with the current file length. If it is inconsistent, delete the local file and download it again.

In fact, urlconnection can handle a lot of streaming media. Here is used to download audio files. It can realize the download function and similar functions of "broadcasting while downloading".

Refer to the example project for code: https://github.com/RustFisher/android-MediaPlayer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>