Faced with draconian restrictions on the use of resources in SoundPool . In order for soundPool.play() to soundPool.play() desired file with a loop argument greater than 0 (or -1), you had to compress the file to 72 kb / s, which is not very acceptable in my case.

Are there alternatives or solutions?

  • On the Internet they say that the file should be in the format of 'wav', and the size of <1mb. Tried it like that? They also say that SoundPool broke down in Android 4.3 - Vladyslav Matviienko
  • I did not try wav, I tried ogg, since wav files in my case will weigh nemeryannom. In the sense broke? - AnonymusCoder
  • In the sense that it stopped working as it should. But I’m not sure what I’ve been saying is what I’m saying. And you try just for the sake of interest with wav - Vladyslav Matviienko

2 answers 2

I use MediaPlayer for looping.

 private MediaPlayer mPlayer; @Override public void onCreate(Bundle savedInstanceState) { ... mPlayer = MediaPlayer.create(this, R.raw.bg_music); mPlayer.setLooping(true); if(dc.music()) mPlayer.start(); } @Override protected void onDestroy() { mPlayer.release(); super.onDestroy(); } @Override protected void onStop() { super.onStop(); mPlayer.pause(); } @Override protected void onStart() { super.onStart(); if(dc.music()) mPlayer.start(); } OnClickListener oclMusic = new OnClickListener() { @Override public void onClick(View v) { dc.music(!dc.music()); main_btn_music.setImageResource(dc.music() ? R.drawable.music_on : R.drawable.music_off); if(dc.music()){ mPlayer.start(); } else{ mPlayer.pause(); } }}; 

    Thank you, rhino! Indeed, working with MediaPlayer turned out to be much more obvious and more convenient. My version that earned:

     public class MainActivity extends AppCompatActivity { private MediaPlayer drumLoop; private boolean isDrumsAtTime; private int currentDrumLoop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); currentDrumLoop = R.raw.medium_slowchina_nofill; isDrumsAtTime = false; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void onClick(View view) { switch (view.getId()) { case R.id.buttonPlayCurrentDrums: if (!isDrumsAtTime) { startLoop(); } else { stopLoop(); } } } private void setLoop(int drumLoopFile){ drumLoop = MediaPlayer.create(this, drumLoopFile); drumLoop.setLooping(true); } public void startLoop(){ setLoop(currentDrumLoop); drumLoop.start(); isDrumsAtTime = true; } public void stopLoop() { drumLoop.stop(); drumLoop.release(); isDrumsAtTime = false; } 

    }