-3

hello I am very new to android programming.I want Button in first activity and when it is clicked second activity will open and plays mp3 file from sd card i have mention path but mp3 file not plays after running project.I am posting my player.java

This is my class

public class player extends AppCompatActivity {

Button btPv,btplay,btStop,btPause;
SeekBar sb;
MediaPlayer mp3;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
    ;
    btplay=(Button)findViewById(R.id.btplay);
    btStop=(Button)findViewById(R.id.btstop);
    btPause=(Button)findViewById(R.id.btpause);
    mp3=new MediaPlayer();
    try {
        mp3.setDataSource("sdcard/musicblee/Over_the_horizon.mp3");
        mp3.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    btplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp3.start();

        }
    });
    btStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {

                mp3.stop();
                mp3.reset();
                mp3.setDataSource("sdcard/musicblee/Over_the_horizon.mp3");
                mp3.prepare();
            } catch (Exception e) {

                e.printStackTrace();
            }

        }
    });
    btPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {

                mp3.pause();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    });

}




}
Dhanumjay
  • 540
  • 7
  • 17
snehal
  • 1
  • Try changing mp3.setDataSource("sdcard/musicblee/Over_the_horizon.mp3"); to mp3.setDataSource("/mnt/sdcard/musicblee/Over_the_horizon.mp3"); – Akshay Jul 05 '17 at 06:45
  • Also I would suggest you to move the entire try catch block in the onclicklistenr of the start button – Akshay Jul 05 '17 at 06:48

1 Answers1

0

refer to this link use mp3.setDataSource() like that

mp3 = MediaPlayer.create(this,Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/musicblee/Over_the_horizon.mp3")); 

to set mp3 source. And Also add this permission to your Android Manifest file :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • after inserting this line final String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/Musicblee/Over_the_horizone"; get error unfortunately app stops – snehal Jul 06 '17 at 06:51
  • final String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/Musicblee/Over_the_horizon.mp3"; – snehal Jul 07 '17 at 10:39
  • storage/extSdCard/Musicblee/Over_the_horizone.mp3 this is path of file store in phone – snehal Jul 07 '17 at 10:43