【问题标题】:programatically downloaded MP3 file is not appearing in any music player以编程方式下载的 MP3 文件未出现在任何音乐播放器中
【发布时间】:2019-07-03 09:23:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: android storage android-music-player


    【解决方案1】:

    你可能需要告诉安卓系统你已经下载了一个新文件

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

    【讨论】:

    • 感谢您的帮助,先生,但这似乎不起作用,还是我做错了什么?
    • @GaganSroay 在问题中发布您最新的代码实现。
    • @GaganSroay 你用的是什么路径??尝试使用绝对路径
    • 我也试过传绝对路径,还是不行
    • 我检查了,文件存在于文件夹中,打开后可以播放。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多