1

我试图在按钮单击时多次播放两个不同的 .wav 文件。我想播放 sound1 然后 sound2 然后 sound1 等等..这是代码片段。

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_box);
        mp = MediaPlayer.create(Box.this, R.drawable.sound1);
        mp2 = MediaPlayer.create(Box.this, R.drawable.sound2);
        handler = new Handler();

        sButton.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                rounds = new Integer(et.getText().toString());
                for(int j = 0; j < rounds; j ++)
                {
                    f();
                    g();
                }

            }
        });
    }

    public void f()
    {
        cdt1 =  new CountDownTimer(5000, 1000)
        {
             public void onTick(long millisUntilFinished) 
             {

             }
             public void onFinish() 
             {   
                 mp.setLooping(true);
                 mp.setVolume(1.0f, 1.0f);
                 mp.start();
             }}.start();

    }
    public void g()
    {
        cdt =  new CountDownTimer(7000, 1000)
        {
             public void onTick(long millisUntilFinished) 
             {

             }
             public void onFinish() 
             {   
                 mp2.setLooping(true);
                 mp2.setVolume(1.0f, 1.0f);
                 mp2.start();
             }}.start();     
    }

每次播放声音时都要重置它们吗?提前致谢。

4

1 回答 1

0

you can check the mediaplayer if its still playing with something like:

if (mp.isPlaying()) {
    //do nothing
} else {
    //start new sound
}
于 2013-04-25T16:24:47.980 回答