soundpool 中的布尔值检查 android

boolean value check in soundpool android

编辑:此代码如果加载 soundpool 声音设置 boolean true。

log cat load 3 声音但加载不正确。

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);
    }

从未加载 == true 。 如果清除此检查值,播放声音。

试试这个:

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);
    }
}