Android enables frequent playback of small audio

We are familiar with playing multimedia files (music and video) in Android using mediaplayer, but now let's talk about another way of playing music files, soundpool. In comparison, playing music with mediaplayer takes a lot of system resources and takes time to load resources, Therefore, it is not suitable for frequently playing small audio files, such as the control focus and the need to play small audio files when clicking. At this time, using soundpool to play audio files is much more efficient than using mediaplayer. Let's also talk about 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 is suitable for small audio files that play frequently. Let's talk about its usage:

Step 1: place the target audio file

Put the audio file in the RES / raw directory so that it can be referenced through R. If you want to put it in the asset directory, you can put it in the raw directory 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,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) ;
  }
}

Step 3: call the interface to play audio

// 在界面上需要这样调用
// 初始化
mMusic = MusicPlayer.getInstance(MainActivity.this) ;
// 在onClick中播放click时的声音
mMusic.play(MusicPlayer.Type.MUSIC_CLICK) ;
// 在onFocusChange中聚焦状态播放聚焦的声音
mMusic.play(MusicPlayer.Type.MUSIC_FOCUSED) ;

If you want to put it in assert, you should use assetfiledescriptor when referencing audio files. The specific methods are as follows:

MediaPlayer mMediaPlayer = new MediaPlayer() ;
  AssetFileDescriptor fd = mContext.getAssets().openFd(“music/click.pm3”) ; // assert目录下的music目录
  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! Implemented in two callback interfaces.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>