简体   繁体   English

在Android中的后台下载多个MP3文件

[英]Download multiple MP3 files in the background in Android

LEVEL: Beginner 等级:初学者
Language: Java 语言:Java
IDE: Android Studio IDE:Android Studio
CASE: Downloading multiple MP3 files from multiple URL's in a row in the background. 案例:在后台连续下载多个URL中的多个MP3文件。
ISSUE: download task in the loop to download all the urls. 问题:在循环中下载任务以下载所有网址。 the songs did download in the following path but aren't fully downloaded (every song is downloaded for 3 seconds/4 seconds basically very less). 歌曲确实按照以下路径下载但未完全下载(每首歌曲下载3秒/ 4秒基本上非常少)。 don't know what's the problem, It'll be great if i can get any help 不知道是什么问题,如果我能得到任何帮助,那就太好了

  • receive mp3 url's from API 从API接收mp3网址
  • download them using the following code. 使用以下代码下载它们。 (ISSUE) (问题)
  • get the path of all and store them in the Database 获取所有路径并将它们存储在数据库中
  • fetch the path and play one by one continuously. 获取路径并连续播放。

     private class DownloadTask extends AsyncTask<SongDetails, Integer, String> { Context context; public DownloadTask(Context context) { this.context = context; } @Override protected void onPreExecute() { super.onPreExecute(); Utilities.showProgressDialog(getActivity(), getFragmentManager()); } @Override protected String doInBackground(SongDetails... songDetails) { for (SongDetails songs : songDetails ) { donwloadsong(songs); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Utilities.dismissProgressDialog(); } } for (SongDetails songs : songDetails ) { downloadTask(songs); } public void downloadTask(SongDetails songs){ HttpURLConnection httpURLConnection = null; try { URL url = new URL(songs.getUrl()); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.connect(); int fileLength = httpURLConnection.getContentLength(); InputStream inputStream = new BufferedInputStream(url.openStream(), 8192); OutputStream outputStream1; outputStream1 = new FileOutputStream(Environment.getExternalStorageDirectory() + "/gbeduwarssongs/" + songDetails.getTitle().trim() + ".mp3"); int count = 0; byte data[] = new byte[1024]; int buffer_length; while ((buffer_length = inputStream.read(data)) != -1) { outputStream1.write(data, 0, count); count += buffer_length; } outputStream1.flush(); outputStream1.close(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); Log.e("Error", "Download Error Exception " + e.getMessage()); } finally { if (httpURLConnection != null) httpURLConnection.disconnect(); } } 

Rather calling iteration inside a new thread, use iteration first and then start new thread in every iteration. 而是在新线程内部调用迭代,首先使用迭代,然后在每次迭代中启动新线程。 I hope it will help you. 我希望它会对你有所帮助。 I am posting code for your help. 我发布了您的帮助代码。

for (SongDetails songs : songDetails) {
        new DownloadTask(pass your song object in constructor).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

and in doInBackground call your downloading method stuff. 并在doInBackground中调用您的下载方法。

I offer to use DownloadManager or workmanager for it. 我提议使用DownloadManagerworkmanager AsyncTask has used a bad idea. AsyncTask使用了一个坏主意。 It has a problem with a memory leak . 它存在内存泄漏问题。 The asyncTask use only one thread asyncTask只使用一个线程

The same question 同样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM