0

I have multiple audio file on res/raw folder, and the audio name is save on a database. So when the program wants to play audio, audio named will called from database with a function. FYI, I build an app for ITP TOEFL test. And I made this part for listening section. And the problem is when I want to go to the next question, the audio will still playing, I wish to add stop function to same button that I use to start play the audio.

Here's the part of my codes that play the audio

variable declare:

private static MediaPlayer mp;

component initialitations:

mainkan.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
            mainkan.setEnabled(false);
                go(audioSoal);
            }
    });

*mainkan means "play"

and this is the main funciton to play audio, which is "string audio" comes from database and pointing to audio files on res/raw folder

public void go(String audio){
    int resId = getResources().getIdentifier("raw/"+audio, null, this.getPackageName());
    mp=MediaPlayer.create(section3db.this, resId);
        try {
            mp.prepare();
        }
        catch(IllegalStateException e){
            // TODO auto-generated catch block
            e.printStackTrace();
        }
        catch(IOException e){
            // TODO auto-generated catch block
            e.printStackTrace();
        }
    mp.start();
    mp.setOnCompletionListener(new OnCompletionListener(){
        public void onCompletion(MediaPlayer arg0){
            mainkan.setEnabled(true);
        }
});
}

can someone help me? Thanks in advance :)

4

1 回答 1

0

在任何按钮单击事件上调用此方法:

mp.stop();

这将停止播放器。

如果您想暂停,请执行以下操作:

mp.pause();
于 2013-11-16T13:27:30.167 回答