Android soundpool enables short and small sound effects

1、 Soundpool introduction

We have used mediaplayer to play audio files before, but when our application needs to play dense and short sound effects frequently, calling mediaplayer will occupy a lot of resources of the system, and the delay time is long. It does not support multiple audio playing at the same time. This simple music playback is applied to our soundpool. It uses the concept of sound pool to manage short sound effects. For example, it can load 20 sound effects at the beginning and manage and play them through their ID. The advantage of soundpool is that it occupies less CPU resources and reduces reaction delay. In addition, it also supports setting the sound quality, volume and playback ratio.

2、 Use example

Note: when using, we need to create a new folder raw (the name is fixed and must be written like this) in the res directory, and put the music under the folder.

use:

public class MainActivity extends Activity implements OnClickListener{
  private Button mbtn_start;
  private SoundPool spool;
  private int id;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mbtn_start=(Button) findViewById(R.id.bt_start);
    id=initSoundpool();
    mbtn_start.setOnClickListener(this);
  }

  // @TargetApi(Build.VERSION_CODES.L)
  private int initSoundpool() {
    //Sdk版本>=21时使用下面的方法
  if(Build.VERSION.SDK_INT>=21){
  SoundPool.Builder builder=new SoundPool.Builder();
  //设置最多容纳的流数
    builder.setMaxStreams(2);
    AudioAttributes.Builder attrBuilder=new AudioAttributes.Builder();
    attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
    builder.setAudioAttributes(attrBuilder.build());
    pool=builder.build();
  }else{

    spool=new SoundPool(2,AudioManager.STREAM_MUSIC,0);

  }
  //加载音频文件,返回音频文件的id
    int id=spool.load(getApplicationContext(),R.raw.mali,1);

    return id;
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bt_start:
  //SoundPool的创建需要时间,因此不能将SoundPool初始化后直接start
  /*参数: (int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate)*/

        spool.play(id,1,-1,1); 

      break;

    default:
      break;
    }

  }
}

Note: introduction to spool.play parameters (refer to API):

Parameters

ID number returned by soundid load method leftvolume left volume value (range = 0.0 to 1.0) left channel rightvolume right volume value (range = 0.0 to 1.0) right channel priority stream priority (0 = lowest priority) priority loop loop mode (0 = no loop, - 1 = loop forever) whether to loop play rate playback rate (1.0 = normal playback, range 0.5 to 2.0) property sets or returns the current playback speed of audio / video

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
分享
二维码
< <上一篇
下一篇>>