Я хочу создать программу для загрузки файла из интернета и чтения его в Media Player. Мой код устарел. Пожалуйста, приведите простой пример с объяснением
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File file = new File(path, "Enrique Iglesias - Hero.mp3");
if (file.exists()) {
Toast.makeText(this, "اجرا", Toast.LENGTH_SHORT).show();
// String path = Environment.DIRECTORY_MUSIC +"Enrique Iglesias - Hero.mp3";
MediaPlayer player = new MediaPlayer();
//Toast.makeText(this, path, Toast.LENGTH_SHORT).show();
try {
player.setDataSource(file.getPath());
player.prepare();
// Toast.makeText(this, "setdata", Toast.LENGTH_SHORT).show();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception of type : " + e.toString());
e.printStackTrace();
}
player.start();
}else {
Toast.makeText(this, "Downloading", Toast.LENGTH_SHORT).show();
DownloadManager downloadManager=(DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.tarafdari.com/sites/default/files/contents/user6984/content-sound/66.enrique_iglesias_-_hero.mp3"));
request.setTitle("Audio Download");
request.setDescription("Enrique Iglesias - Hero.mp3");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "Enrique Iglesias - Hero.mp3");
downloadManager.enqueue(request);
}
}