SoundPool仅停止一次

时间:2012-10-22 20:36:33

标签: java android audio soundpool soundplayer

第一次按下按钮会停止声音。但在此之后按下它没有任何效果。这是我的java代码:

public class App extends MultiTouchActivity {

SoundPool sp;
    int  dub1s;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
     dub1s = sp.load(this, R.raw.dub1, 1);


}

public void dubstep1(View view) {

    sp.stop(dub1s);
    sp.play(dub1s, 1, 1, 1, 0, 1f);
}

1 个答案:

答案 0 :(得分:2)

SoundPool.stop()获取流ID (来自play的返回值),而非声音ID (来自load的返回值)。

他们不是一回事。

public class App extends MultiTouchActivity {

    SoundPool sp;
    int  mSoundId;
    int  mStreamId = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    mSoundId = sp.load(this, R.raw.dub1, 1);
}

public void dubstep1(View view) {
    if(mStreamId != 0) {
        sp.stop(mStreamId);
    }
    mStreamId = sp.play(mSoundId, 1, 1, 1, 0, 1f);
}