-2

I have created an Android Application to download an .mp3 file from server and store it in a folder in internal memory. Now I want to fetch that downloaded file and play it for the user.

How can I retrieve that single file and play that file using Media Player .?

Can anyone find me a solution for this ?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jithin Ezio
  • 171
  • 12
  • Possible duplicate of [reading a specific file from sdcard in android](http://stackoverflow.com/questions/3779944/reading-a-specific-file-from-sdcard-in-android) – OneCricketeer Aug 19 '16 at 10:08
  • Possible duplicate of [Download file with AsyncTask](http://stackoverflow.com/questions/29188557/download-file-with-asynctask) – U.Swap Aug 19 '16 at 10:09

2 Answers2

1

For getting the file and changing its Extension this code maybe usefull..

public void jjj(View view) {

    File file  = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.srt"); // handler to your ZIP file
    File file2 = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.mp3");
    // destination dir of your file
    boolean success = file.renameTo(file2);

//-----------------------------------------------------------------------------------------------------------
    final MediaPlayer mp=new MediaPlayer();
    try{
        //you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3
        mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Jithin's/downloadedfile.mp3");

        mp.prepare();
    }catch(Exception e){e.printStackTrace();}

    mp.start();
//------------------------------------------------------------------------------------------------------------
    if (success) {
        // File has been renamed
        Toast.makeText(MainActivity.this, "Changed Extension .... ", Toast.LENGTH_SHORT).show();
    }
Jithin Ezio
  • 171
  • 12
0

EDIT: the question was first, how to play a MP3 file from a file from internal storage. My answer is related to that question.

You can use Context.getFilesDir() method to get the path to the internal storage.

Usage:

String path = getFilesDir() + "/" + name;

Note that the name can also contain directory names, if you put the mp3 file in a subdirectory.

Playing it:

mediaPlayer = new  MediaPlayer();
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();   
mediaPlayer.start()
gillesC
  • 677
  • 1
  • 5
  • 23