mercredi 29 octobre 2014

Previously Playing Music Doesn't Stops On Button Press.


Vote count:

0




I'm an absolute novice in programming, dared to start with Android Programming lately. Added 4 Image Buttons, every button plays a unique tune and displays a unique message; but the problem is that if I press another button after clicking one, the previous music continues to play while the present one is still on, in short both the music continue to run. If I play four of the buttons one after another, four different tunes play altogether. Couldn't find a way out. Please help, if possible in an elaborate manner. Another question, have I created the coding too much complicated?



package com.example.imagesonic;


import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



try{


ImageButton ib1 = (ImageButton) findViewById(R.id.imageButton1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.muzikone);

ib1.setOnClickListener (new View.OnClickListener() {



@Override
public void onClick(View v) {

Toast.makeText(MainActivity.this, "FIRST MUSIC!", Toast.LENGTH_LONG).show();
mp1.start();

}


});


ImageButton ib2 = (ImageButton) findViewById(R.id.imageButton2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.muziktwo);

ib2.setOnClickListener (new View.OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(MainActivity.this, "SECOND MUSIC!", Toast.LENGTH_LONG).show();
mp2.start();

}


});



ImageButton ib3 = (ImageButton) findViewById(R.id.imageButton3);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.muzikthree);

ib3.setOnClickListener (new View.OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(MainActivity.this, "THIRD MUSIC!", Toast.LENGTH_LONG).show();

mp3.start();

}


});


ImageButton ib4 = (ImageButton) findViewById(R.id.imageButton4);
final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.muzikfour);

ib4.setOnClickListener (new View.OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(MainActivity.this, "FOURTH MUSIC!", Toast.LENGTH_LONG).show();
ImageButton ib4 = (ImageButton) findViewById(R.id.imageButton1);
ib4.setImageResource(R.drawable.naomitan);
mp4.start();

}


});

}catch(Exception e) {

}
}
}


asked 1 min ago







Previously Playing Music Doesn't Stops On Button Press.

Aucun commentaire:

Enregistrer un commentaire