SoFunction
Updated on 2025-04-06

Android MediaPlayer implements music player instance code

Android MediaPlayer implements music player

1. Layout files

<?xml version="1.0" encoding="utf-8"?> 
<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:padding="10px" 
    android:text="Click the Start button to play audio" /> 
 
  <LinearLayout 
    android: 
    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:enabled="false" 
    android:text="pause" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:enabled="false" 
    android:text="stop" /> 
  </LinearLayout> 
</LinearLayout> 

2. MainActivity member variables

private MediaPlayer player;//MediaPlayer object  private boolean isPause = false;//Whether to pause  private File file;//The audio file to be played  private TextView hint;//Declare the text box showing prompt information

3. Get components in the onCreate() method

final Button button1 = (Button)findViewById(.button1);//Get the "Play" button    final Button button2 = (Button)findViewById(.button2);//Get the "Pause/Continue" button    final Button button3 = (Button)findViewById(.button3);//Get "Stop" button    hint = (TextView)findViewById();//Get the text box for displaying prompt information    file = new File("/storage/emulated/0/qqmusic/song/Qiao Weiyi - White Moonlight [mqms2].mp3");//Get the file to be played    if(()){ 
      player = (this, (()));//Create MediaPlayer exclusive    }else{ 
      ("The audio file to be played does not exist!"); 
      (false); 
      return; 
    } 

4. Write the play() method

private void play(){ 
    try { 
      (); 
      (());//Reset the audio to play      ();//Preload audio      ();//Start playback      ("The audio is playing..."); 
    } catch (Exception e) { 
      (); 
    } 
  } 

5. Add a listening event to the MediaPlayer object and play it again after the broadcast.

(new OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
        play();//Start playback again      } 
    }); 

6. Add a click event listener for playback

(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        play();//Start music playback        if(isPause){ 
          ("pause"); 
          isPause = false;//Set the value of the pause tag variable to false        } 
        (true);//The "Pause/Continue" button is available        (true);//The "Stop" button is available        (false);//The "Play" button is not available      } 
    }); 

7. Add a click event listener in the "Pause/Continue" button

(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      if(()&&!isPause){ 
        ();//Pause playback        isPause = true; 
        ((Button)v).setText("continue"); 
        ("Pause audio playback..."); 
        (true);//The "Play" button is available      }else{ 
        ();//Continue playing        ((Button)v).setText("pause"); 
        ("Audio is playing..."); 
        isPause = false; 
        (false);//The "Play" button is not available      } 
    } 
  }); 

8. Stop button

(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        ();//Stop playback        ("Stop playing audio..."); 
        (false);//The "Pause/Continue" button is not available        (false);//The "Stop" button is not available        (true);//The "Play" button is available      } 
    }); 

9. Rewrite the onDestroy() method of Activity

@Override 
  protected void onDestroy() { 
    if(()){ 
      ();//Stop audio playback    } 
    ();//Release resources    (); 
  } 

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