如何一次播放一个声音?

时间:2015-09-08 15:27:32

标签: android eclipse audio media imagebutton

当我点击显示器上的imgniatp时,音频播放效果非常好,但是如果我也点击imgbukap,在imgniatp完成音频之前,imgbukap也会播放音频。所以两个音频一起播放。如何一次播放一个声音?

package sodik.com.ramadhan;

import sodik.com.ramadhan.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.TextView;


public class Puasa3Activity extends Activity implements OnClickListener{
MediaPlayer mp;
ImageButton imgniatp1, imgbukap2;

String p1, p2, p3, p4, p5, p6, p7, p8, p9;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.puasa3);

    TextView p1tk = (TextView) findViewById(R.id.p1tk);
    TextView p2tk = (TextView) findViewById(R.id.p2tk);

    imgniatp1=(ImageButton) findViewById(R.id.imgniatp);
    imgniatp1.setOnClickListener(this);
    imgbukap2=(ImageButton) findViewById(R.id.imgbukap);
    imgbukap2.setOnClickListener(this);
}
    public void onClick(View v){
        switch (v.getId()){
        case R.id.imgniatp:
            mp= MediaPlayer.create(this, R.raw.bukapuasa);
            mp.setVolume(1,1);
            mp.start();
            Toast.makeText(this, " Memainkan Buka Puasa", Toast.LENGTH_SHORT).show();
        break;
        case R.id.imgbukap:
            mp = MediaPlayer.create(this, R.raw.niatpuasa);
            mp.setVolume(1,1);
            mp.start();
            Toast.makeText(this, " Mainkan Niat Puasa ", Toast.LENGTH_SHORT).show();
        break;
        }


            }

`

1 个答案:

答案 0 :(得分:0)

您可以查看MediaPlayer是否已在播放某些内容,并在onCompletionListener中启动剪辑

if(mp != null && mp.isPlaying()) {
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp = MediaPlayer.create(this,R.raw.bukapuasa);
            mp.start();
        }
    });
} else {
    mp = MediaPlayer.create(this,R.raw.bukapuasa);
    mp.start();
}