'how to play audio file in android from internal memory of phone

I want to make sample to play an audio file in android ,i want to play music from internal memory of phone .how to give path please suggest.

I am writing this,but facing java.io.IOException:prepare failed error

 final MediaPlayer mp = new MediaPlayer();
 try{ 
      mp.setDataSource(Environment.getRootDirectory()+"/1.mp3");
      mp.prepare();
   }
   catch(Exception e)
   {
     e.printStackTrace();
   }


Solution 1:[1]

You can try something like this.

// Add permission to your menifest to Read File from your storage


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Then in your JAVA class/Activity from where you are calling to load mp3 File.

MediaPlayer mPlayer;

        // Your Media Player will be called with Audio file here..
          private void loadAudio(){

            String fileName = "YourAudio.mp3";
            String completePath = Environment.getExternalStorageDirectory() + "/" + fileName;

            File file = new File(completePath); 

                 Uri myUri1 = Uri.fromFile(file);
                    mPlayer = new MediaPlayer();
                    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    try {
                        mPlayer.setDataSource(getApplicationContext(), myUri1);
                    } catch (IllegalArgumentException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (SecurityException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (IllegalStateException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        mPlayer.prepare();
                    } catch (IllegalStateException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    }
                    mPlayer.start();
            }

I hope this will help you.

Solution 2:[2]

Firstly, if you didn't, add the following permission in your manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

And then this code:

String path = Environment.getExternalStorageDirectory();

MediaPlayer mp = new MediaPlayer();    
try {
    mPlayer.setDataSource(path+"/audio/1.mp3"); 
    mPlayer.prepare();                              
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}     
mPlayer.start();

Environment.getExternalStorageDirectory() gets exactly the path to your external directory as it is, not the Environment.getRootDirectory()

Solution 3:[3]

Use this code it work ,

   try {
    MediaPlayer mediaPlayer = new MediaPlayer();
    String   path ,Filename ,fomrmat ;
    path   = "/storage/emulated/0/Download/a/";
    Filename = "abc";
    fomrmat =".mp3";
    mediaPlayer.reset();
    mediaPlayer.setDataSource(path + Filename + fomrmat);
    mediaPlayer.prepare();
    mediaPlayer.start();
    Log.i("moh Service", "End try Player");
    } catch (Exception e) {
    Log.i("moh Service", e.toString());
    e.printStackTrace();
    }

Please Set permissions

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gabriella Angelova
Solution 3 m-tech