Android SoundPool启动/停止错误

时间:2014-02-12 02:44:47

标签: android audio switch-statement soundpool

我是Stack OverFlow的新手,我非常感谢您的时间和帮助。

我在Android中使用SoundPool,我编写的代码可以在单击按钮时播放声音(循环播放)。另外,我有一个按钮来停止循环。

第一次单击停止按钮时工作正常,但是,如果我再次尝试启动声音,则停止按钮不会执行任何操作,声音会继续循环。

再次感谢,

阿诺德

public class MainActivity extends Activity implements OnClickListener {

SoundPool mSoundPool;
Button b, bStart, bStop;
int mStream = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bStart = (Button) findViewById(R.id.bStart);
    bStop = (Button) findViewById(R.id.bStop);

    bStart.setOnClickListener(this);
    bStop.setOnClickListener(this);

    mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);

    mStream = mSoundPool.load(this, R.raw.notification, 1);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.bStart:
        mSoundPool.play(mStream, 1, 1, 0, -1, 1);
        break;
    case R.id.bStop:
        mSoundPool.stop(mStream);
        break;

    }
}


}

0 个答案:

没有答案