以编程方式下载的MP3文件未出现在任何音乐播放器中

时间:2019-07-03 09:23:20

标签: android storage android-music-player

我希望下载的文件出现在Music Players中,当我从文件资源管理器手动打开它时可以播放。 我尝试添加专辑名称和艺术家名称,但似乎不起作用。

Context context;
public DownloadMusic(Context context)
{
    this.context = context;
}

NotificationCompat.Builder noti;
Notification notify;
NotificationManager manager;
String channel="chennel";
MediaScannerConnection scanner;
@Override
protected String doInBackground(String... strings) {
    try {
        downloadStartNotification(strings[3]);
        URL url = new URL(strings[strings.length-2]);
        URLConnection connection = url.openConnection();
        connection.connect();
        int fileLength = connection.getContentLength();
        int progress;
        InputStream input = new BufferedInputStream(url.openStream());
        File path = new File(Environment.getExternalStorageDirectory() + File.separator
                + "G-Music");
        String filepath = path+File.separator+strings[3]+".mp3";
        try {
            path.mkdir();
        }catch (Exception e){}
        OutputStream output = new FileOutputStream(filepath);
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        showNotification(strings[3]);
        while ((count = input.read(data)) != -1) {
            total += count;
            progress = (int) (total * 100 / fileLength);
            noti.setProgress(100,progress,false);
            noti.setContentText(progress+" %");
            manager.notify(2,noti.build());
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();

        modify(filepath,strings);
        downloadFinishNotification(strings[3]);
        musicAdd(filepath);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}
void musicAdd(final String path)
{
    File audioFilePath = new File(path);
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(audioFilePath)));
    MediaScannerConnection.scanFile(
            context,
            new String[]{ path },
            new String[]{ "audio/mp3", "*/*" },
            new MediaScannerConnection.MediaScannerConnectionClient()
            {
                public void onMediaScannerConnected()
                {
                }
                public void onScanCompleted(String path, Uri uri)
                {
                }
            });
}

我希望下载的文件出现在Music Players中,当我从文件资源管理器手动打开它时可以播放。 我尝试添加专辑名称和艺术家名称,但似乎不起作用。

1 个答案:

答案 0 :(得分:0)

您可能需要告知Android系统您已经下载了新文件

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(audioFilePath)));

,如果仍然无法使用

MediaScannerConnection.scanFile(
    context, 
    new String[]{ pathFile1, pathFile2 }, 
    new String[]{ "audio/mp3", "*/*" }, 
    new MediaScannerConnectionClient()
    {
        public void onMediaScannerConnected()
        {
        }
        public void onScanCompleted(String path, Uri uri)
        {
        }
    });