0

I used download manager to download an mp3 file, but what I also want is to be able to play the downloaded file from my app in an activity but I can't do that. Below is the code I used to download the file from server:

BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                Query query = new Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c
                            .getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c
                            .getInt(columnIndex)) {

                        ImageView view = (ImageView) findViewById(R.id.imageView1);
                        String uriString = c
                                .getString(c
                                        .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        view.setImageURI(Uri.parse(uriString));
                    }
                }
            }
        }
    };

    registerReceiver(receiver, new IntentFilter(
            ACTION_DOWNLOAD_COMPLETE));
}

public void onClick(View view) {
    dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Request request = new Request(
            Uri.parse("https://eplayer.000webhostapp.com/Sleep%20Away.mp3"));
    enqueue = dm.enqueue(request);

}

public void showDownload(View view) {
    Intent i = new Intent();
    i.setAction(ACTION_VIEW_DOWNLOADS);
    startActivity(i);
}
public void audioPlayer(String path, String fileName){
    //set up MediaPlayer
    MediaPlayer mp = new MediaPlayer();
}

Code to try and play downloaded mp3:

 public void audioPlayer(String path, String fileName){
    //set up MediaPlayer
    MediaPlayer mp = new MediaPlayer();

    try {
        mp.setDataSource( ACTION_DOWNLOAD_COMPLETE + File.separator +"Sleep Away");
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
}
Pochmurnik
  • 780
  • 6
  • 18
  • 35
  • i think you might have misunderstood my question , i don't want to play the file from its URL . i want to download the mp3 to external storage and then play it from the app with Media player. – franklyn omeben Dec 21 '18 at 18:44
  • i dont get you can you just write out the code please i am getting errors – franklyn omeben Dec 22 '18 at 07:36

0 Answers0