Search code examples
androidsoundpool

boolean value check in soundpool android


Edit : this code if load soundpool sound set boolean true .

log cat load 3 sound but loaded not true .

boolean loaded = false;

 SoundPool sp = new SoundPool(70, AudioManager.STREAM_MUSIC, 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                                   int status) {
            Log.i("OnLoadCompleteListener", "Sound " + sampleId + " loaded.");

                loaded=true;

        }
    });

    final int sol [] = new int[3];
    sol[0] = sp.load(this, R.raw.x1, 1);
    sol[1] = sp.load(this, R.raw.x2, 1);
    sol[2] = sp.load(this,R.raw.x3, 1);

    if (loaded) {
        autosynce(sol);
    }

never not loaded == true . if clear this check value , playing sound .


Solution

  • Try this:

    public boolean loaded = false;
    public SoundPool sp;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        sp = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                Log.i("OnLoadCompleteListener", "Sound " + sampleId + " loaded.");
                loaded=true;
            }
        });
    
        int sol = new int[3];
        sol[0] = sp.load(this, R.raw.x1, 1);
        sol[1] = sp.load(this, R.raw.x2, 1);
        sol[2] = sp.load(this,R.raw.x3, 1);
    
        //This 100 is the number of loops, I think it will be enough, but if
        //you want to set an infinite loop, you should better use MediaPlayer
        //then, but that will prevent you from playing many sounds together.
        sp.play(sol, 1.0f, 1.0f, 1, 100, 1.0f);
    
        if (loaded) {
            //I am not sure what's that method. It's yours...
            autosynce(sol);
        }
    }