SoFunction
Updated on 2025-04-10

Android Multimedia Music (MediaPlayer) player production code

This article shares the specific implementation code of Android multimedia music player for your reference. The specific content is as follows

First, configure permissions in

<!-- Read music from your phone -->
  <uses-permission android:name=".READ_EXTERNAL_STORAGE" />

Note: Several Sources of Android Multimedia Music

1. In Android applications

(this, );
//mnt/sdcard/Mobile Memory Card

2. Put it in your own SD card (memory card)

(this, ("file://mnt/sdcard/1/123.mp3"));

3. On the Internet (know which path)

 (this, ("http://192.168.43.202:8080/sister.mp3"));

Next, write it in Java code

Write global variables in the onCreate method:

 private MediaPlayer mediaPlayer;
  private SeekBar seekBar;
private SurfaceView sv_main_suffce;



 public void onplay(View view) throws IOException {
    imageButton = (ImageButton) view;
    if (mediaPlayer==null){
      //Play the audio file of the application//      mediaPlayer = (this, );

      //Play audio files in memory card      mediaPlayer=new MediaPlayer();
      //The type of audio stream      (AudioManager.STREAM_MUSIC);
      //Set the source of audio//      (this, ("file://mnt/sdcard/1/roar.mp3"));
// ();//Prepare
      //Play online music      (this, ("http://192.168.43.202:8080/crickets.mp3"));
      //One step preparation      ();
      //Set ready listening      (new () {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
          //Start playback          ();
          //Click to play to change the icon          (.ic_media_pause);
          //Get the maximum time to play music          int durtion=();
          //Set the maximum value of the progress bar to the maximum time of playing music          (durtion);
          new Mythred().start();
        }
      });
       //Set an event for the progress bar      (new () {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
          //Get drag progress          int progress=();
          // Play to that location          (progress);
        }
      });
    }
    else if (()){
      ();
      (.ic_media_play);
    }
    else {
      ();
      (.ic_media_pause);
    }

  }

I wrote a child thread extends Thread (seekbar progress bar):

class Mythrod extends Thread{
    @Override
    public void run() {
      ();
      while (()<=()){
       //Get the current playback location of the music        int currentPosition=();
        (currentPosition);
      }
    }
  }

Create another class SoundActivity, and instantiate the audio pool through soundPool, which can realize the playback of multiple sounds together.

//Instantiate the audio pool    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,0);
  (new () {
    @Override
    public void onLoadComplete(SoundPool soundPool, int i, int status) {
     //Play      (i,1,1,1,-1,1);
    }
  });

//How to write sound button public void playKFC(View view){
 //Path of audio pool    (this,,1);
  }
  public void playTwo(View view){
    (this,,1);
  }
  public void playThree(View view){
    (this,,1);
  }
  public void playFour(View view){
    (this,,1);
  }
  public void playDog(View view){
    (this,,1);
  }

//Destroy the audio pool soundPool  @Override
  protected void onDestroy() {
    ();
    if(soundPool!=null){
     //Release the memory      ();
      soundPool=null;
    }
  }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.