скачанный MP3 файл не отображается ни в одном музыкальном проигрывателе - PullRequest
0 голосов
/ 03 июля 2019

Я хочу, чтобы загруженный файл отображался в музыкальных проигрывателях. Воспроизводится, когда я открываю его вручную из проводника файлов. Я попытался добавить название альбома и имя исполнителя, но оно не работает.

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)
                {
                }
            });
}

Я хочу, чтобы загруженный файл отображался в музыкальных проигрывателях. Воспроизводится, когда я открываю его вручную из проводника. Я попытался добавить название альбома и имя исполнителя, но оно не работает.

1 Ответ

0 голосов
/ 03 июля 2019

Вам может потребоваться сообщить системе 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)
        {
        }
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...