SoFunction
Updated on 2025-04-09

Android using mediaplayer to play music in res/raw folder

Android using mediaplayer to play music in res/raw folder

(1) Create a new folder in the res folder and rename it to raw, and put the music to be played into the raw folder

(2) Modify the xml layout file in the layout directory, add 3 button spaces and a text control to prompt the current playback status and playback pause and stop. The specific code is as follows

<LinearLayout xmlns:andro 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <TextView 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click to play to start playing music" /> 
 
  <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
     
   <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Play" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="pause" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="stop" /> 
  </LinearLayout> 
 
</LinearLayout> 

(3) Open MainActivity In this class, define the required member variables, the specific code is as follows

private MediaPlayer mp;//mediaPlayer objectprivate Button play,pause,stop;//Play Pause/Continue Stop Buttonprivate TextView hint;//Show the current playback statusprivate boolean isPause=false;//Whether to pause 

(4) In the onCreate() method, get the playback Pause/Continue Stop button to prompt the text box in the current state and create a playback object for the mediaplayer object. The specific code is as follows.

play=(Button) findViewById(.button1); 
pause=(Button) findViewById(.button2); 
stop=(Button) findViewById(.button3); 
hint=(TextView) findViewById(); 
(20); 
mp=(, );//Create mediaplayer object

(5) Write a play() method that has no return value for playing music. In this method, first call the reset() method of the mediaplayer object to reset the mediaplayer object, and then reset the audio file to be played for it. Finally, call the start() method to start playing the audio

private void play(){ 
  try{ 
    (); 
    mp=(, );//Reset the audio to play    ();//Start playback    ("Audio is playing..."); 
    (false); 
    (true); 
    (true); 
  }catch(Exception e){ 
    ();//Output exception information  } 
} 

(6) Add a completion time listener to the mediaplayer object, which is used to restart the playback of music after the music is finished

(new OnCompletionListener() { 
       
      @Override 
      public void onCompletion(MediaPlayer arg0) { 
        // TODO Auto-generated method stub 
        play();//Start playback again      } 
    }); 

(7) Add a click event listener for the play button

(new OnClickListener() { 
     
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
      play(); 
      if(isPause){ 
        ("pause"); 
        isPause=false; 
      } 
    } 
  }); 

(8) Add a click event listener for the pause button

(new OnClickListener() { 
       
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        if(()&&!isPause){ 
          (); 
          isPause=true; 
          ("continue"); 
          ("Pause audio playback..."); 
          (true); 
        }else{ 
          (); 
          ("pause"); 
          ("Continue to play the audio..."); 
          isPause=false; 
          (false); 
        } 
      } 
    }); 

(9) Add a click event listener for the stop button

(new OnClickListener() { 
       
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        (); 
        ("Stop playing audio..."); 
        (false); 
        (false); 
        (true); 
      } 
    }); 

(10) Be sure to remember this. Rewrite the onDestroy() method of Activity, which is used to stop the audio being played when the current Activity is destroyed and release the resources occupied by the mediaplayer. Otherwise, you will play it once every time you open it, and the last playback will not stop. You can try it, I can't explain it clearly

protected void onDestroy() { 
  // TODO Auto-generated method stub 
  if(()){ 
    (); 
  } 
  ();//Release resources  (); 
} 

Thank you for reading, I hope it can help you. Thank you for your support for this site!