Android : Play audio upon each button click

on Sunday, February 1, 2015


My project has 2 audio files, 'dog.mp3' and 'cat.mp3' and so the code below enables me to play either sound based on particular list view item clicks however how can I modify the code so that I want to achieve the following?:



  • When 'cat.mp3' is playing, if I tap the list view item for 'dog.mp3', I want the 'cat.mp3' file to stop playing and the 'dog.mp3' audio to play.

  • When 'dog.mp3' is playing, if I tap the list view item for 'cat.mp3', I want the 'dog.mp3' file to stop playing and the 'cat.mp3' audio to play.


All help will be appreciated.



mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
mp = MediaPlayer.create(Test.this, R.raw.cat);
mp.start();
}

if (position == 1) {
mp = MediaPlayer.create(Test.this, R.raw.dog);
mp.start();
}
}
});

0 comments:

Post a Comment