2

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();
   }
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
Uzeeta Siloth
  • 45
  • 1
  • 2
  • 6

3 Answers3

3

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.

Ravi Kabra
  • 1,284
  • 1
  • 11
  • 17
  • What path your are getting in completepath String??.. Have you checked is it returning you the path?. Also your file is present directly in the storage or it is inside any folder?. – Ravi Kabra May 08 '15 at 06:17
  • my music file is present in music inside Media folder – Uzeeta Siloth May 08 '15 at 06:47
  • Why you want READ_EXTERNAL_STORAGE permission for reading file from internal storage? – Abhi Aug 21 '20 at 11:30
  • [Can I catch multiple Java exceptions in the same catch clause?](https://stackoverflow.com/questions/3495926/can-i-catch-multiple-java-exceptions-in-the-same-catch-clause) – ggorlen Oct 22 '21 at 19:24
1

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()

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • Why you want READ_EXTERNAL_STORAGE permission for reading file from internal storage? – Abhi Aug 21 '20 at 11:30
0

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

m-tech
  • 338
  • 2
  • 14