I made a project where two textviews are on screen.I set two audio for the two textview with Ontouch method.So when i touch any of the textview it play the sound.
Here is the code
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv = (TextView) findViewById(R.id.CustomFontText);
TextView tv1 = (TextView) findViewById(R.id.CustomFontText1);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/DroidSansFallback.ttf");
Typeface tf1 = Typeface.createFromAsset(getAssets(),
"fonts/DroidSans.ttf");
tv.setTypeface(tf);
tv1.setTypeface(tf1);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if( v == findViewById( R.id.CustomFontText ))
{
MediaPlayer mp=MediaPlayer.create(getBaseContext(), R.raw.robotrock);
mp.start();
}
return false;
}
return false;
}
});
tv1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if( v == findViewById( R.id.CustomFontText1 ))
{
MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.nam);
mp1.start();
}
}
return false;
}
});
}}
That is how i did it.The problem is when i touch both the textview,both audio play at the same time.
But I want when i touch one textview ,the audio will play and at that time if i touch another ,the previous audio need to stop and play the corresponding audio which textview is touched.So how can i handle them?Where i need to implement or edit my code?
Please HELP!!!
I think you have already created two instaces of media player. What you have to do is to check for which is playing and stop it.