android Realizes Frequent Play of Small Audio

  • 2021-11-14 07:16:39
  • OfStack

In android, multimedia files (music and video) are played in MediaPlayer mode, which is familiar to everyone. But now let's talk about another way to play music files, SoundPool. Comparatively speaking, playing music with MediaPlayer takes up a lot of system resources, and it takes time to load resources, so it is not suitable for playing small audio files frequently, such as control focus, and playing small audio files when clicking. At this time, playing audio files with SoundPool is much more efficient than playing audio files with MediaPlayer. Let's also talk about it here. If you use MediaPlayer to play small audio files, there will be a delay. After clicking, the sound will appear later. Therefore, SoundPool is lighter than MediaPlayer and suitable for small audio files that are played frequently.
Let's talk about its usage:

Step 1: Place the target audio file

Place the audio file in the res/raw directory so that it can be referenced through R. If you want to put it under the Assert directory, it is also possible to put it under raw first.

Step 2: Write a class for audio playback


public class MusicPlayer {
  private Context mContext ;
  private static MusicPlayer sInstance ;
  static class Type{
    public final static int MUSIC_CLICK = 1 ;
    public final static int MUSIC_FOCUSED = 2 ;
  }
  private SoundPool mSp ;
  private Map<integer ,integer=""> sSpMap ;
  private MusicPlayer(Context context){
    mContext = context ;
    sSpMap = new TreeMap<integer ,integer="">() ;
    mSp = new SoundPool(10 ,AudioManager.STREAM_MUSIC ,100) ;
    sSpMap.put(Type.MUSIC_CLICK, mSp.load(mContext, R.raw.click, 1)) ;
    sSpMap.put(Type.MUSIC_FOCUSED, mSp.load(mContext, R.raw.focused, 1)) ;
  }
   
  public static MusicPlayer getInstance(Context context){
    if(sInstance == null)
      sInstance = new MusicPlayer(context) ;
    return sInstance ;
  }
   
  public void play(int type){
    if(sSpMap.get(type) == null) return ;
    mSp.play(sSpMap.get(type), 1, 1, 0, 0, 1) ;
  }
}

Step 3: Call the interface and play the audio


//  You need to call this on the interface 
//  Initialization 
mMusic = MusicPlayer.getInstance(MainActivity.this) ;
//  In onClick Play in click The sound of the hour 
mMusic.play(MusicPlayer.Type.MUSIC_CLICK) ;
//  In onFocusChange Play the focused sound in the focus state 
mMusic.play(MusicPlayer.Type.MUSIC_FOCUSED) ;

If you want to put it in Assert, you should pay attention to using AssetFileDescriptor when referring to audio files, as follows:


MediaPlayer mMediaPlayer = new MediaPlayer() ; 
  AssetFileDescriptor fd = mContext.getAssets().openFd( " music/click.pm3 " ) ; // assert Directory music Directory 
  mMediaPlayer.setDataSource(fd.getFileDescriptor() ,fd.getStartOffset() ,fd.getLength()) ;
  mMediaPlayer.prepare() ;
  mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer arg0) {
      mMediaPlayer.release() ;
    }
  }) ;
  mMediaPlayer.setOnErrorListener(new OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
      mMediaPlayer.release() ;
      return false;
    }
  }) ;
mMediaPlayer.start() ;

Note: Remember to release resources when using MediaPlayer! Is implemented in two callback interfaces.


Related articles: