-
You should put mp3 file in /assets folder.
-
put this code inside
onCreate()
method aftersetContentView()
final MediaPlayer mp = new MediaPlayer(); Button b = (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(mp.isPlaying()) { mp.stop(); } try { mp.reset(); AssetFileDescriptor afd; afd = getAssets().openFd("AudioFile.mp3"); mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); mp.prepare(); mp.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } });
3.sound will be played again each time you press button. You don’t have to write any extra code for that.
Note that AudioFile.mp3 is the name of the mp3 file in /assets folder
Hope this answer is helpful:)