Playing sounds in Android

Posted by Sumeet Arora
6
Jul 22, 2015
129 Views
Image

Copy your sounds in raw/ folder in res/ . If you do not have raw/ folder then make one by right clicking on res/ folder, click on Android resource directory and then giving directory name as “raw” and selecting resource type as “raw” and then copy your sounds in this directory. 

The following code starts a sound in onCreate() method and handle its lifecycle:

  • Starts the sound on onCreate().
  • Pause the Sound on onPause() and getting its seek position.
  • Playing the sound on onResume() from where it was stopped at onPause().
  • Release the resources associated with MediaPlayer object on onDestroy().


  1. public class MainActivity extends Activity {
  2. private static MediaPlayer mp ;
  3. private int musicSeekLength = 0;
  4. @Override
  5. protected void onCreate() {
  6. startSound();
  7. }
  8. @Override
  9. protected void onResume() {
To read this full blog on Playing sounds in Android please visit FindNerd. 

If you have any queries related to Android Programming than you can ask your query at our Android Developer Forum.
Comments
avatar
Please sign in to add comment.