I want to play music when a button is clicked, but I want to play different music with different button then the first music stopped and the second music would play.
code:
charge = MediaPlayer.create(this, R.raw.charge);
santai1 = MediaPlayer.create(this, R.raw.santai1);
}
public void charge(View view) {
charge.start();
}
public void santai1(View view) {
santai1.start();
}
Say, I clicked charge button and then played the charge.mp3. Then I want to play santai1.mp3 but when I clicked the santai1 button the charge.mp3 will stop playing and the santai1.mp3 will play.
I want this to be able the opposite and it can be done continuously.
When I clicked santai1 button then santai1.mp3 will be played, while the santai.mp3 still playing I clicked the charge button. I want the santai1.mp3 stopped and then play the chage.mp3.
When I clicked on charge button, then I clicked on santai1 button, both music started together.
if you need it for many
create a mediaplayer object and a method.
private MediaPlayer media;
private void stopMusic() {
if (media != null) {
media.stop();
media.release();
media= null;
}
}
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopMusic();
media= MediaPlayer.create("you acivityname".this, "mp3file directory");
media.start();
}
});
try this:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.start();
}
});
and do the same for other button
well it's simple you need 2 if statments for the buttons to check if one of them is playing.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(santail.isPlaying()){
santail.stop();
}
charge.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.start();
}
});
Related
I am playing some audio via a button is pressed. The audio plays fine, but i want to be able to spam the button and let the audio play play play play. Even stop where its playing and play again if i press the button. Now when i press the button, the audio just plays all the way through and nothing happens if i press the button while the audio is playing. Here is my code:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
mp.setLooping(true);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
Replace this code with your existing code. if you want to repeat your audio then you have to set the looping property to true.
OK - I hope I got this right:
You want to play the audio clip for every time you press the button and the clip should be played completely, before the next clip starts.
Here's what I'd do:
Create a new integer attribute.private int i = 0;
Increment it every time you press the button.
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
IUP();
}
});
public synchronized void IUP(){
i++;
}
public synchronized void IDOWN(){
i--;
}
public synchronized int getI(){ return i; }
public void startAudio(){ mp.start(); }
public boolean isPlaying(){ return mp.isPlaying(); }
Use a seperate thread for the player.
Thread player_thread = new Thread(new Runnable(){
#Override
public void run(){
while(true){
if(getI() > 0 && !isPlaying()){
IDOWN();
startAudio();
}
}
}
}).start();
Or use an OnCompletionListener on the MediaPlayer.
Tell me if this is what you were looking for.
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(play_button.getText().toString().equals("Stop")
{
mp.stop();
play_button.setText("Play");
}
else
{
mp.start();
play_button.setText("Stop");
}
}
});
How can I start the song from where I pressed pause button, help me out am new to media in android and am unaware from the media properties all the buttons are working fine , but I want to start the song where from at which point I stopped it -
public class MainActivity extends Activity {
MediaPlayer mediaPlayer;
Button play, stop,pause;
Uri path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
pause = (Button) findViewById(R.id.pause);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
path = Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.chelseafc);
mediaPlayer = MediaPlayer.create(MainActivity.this,path);
mediaPlayer.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
});
}
#Override
protected void onDestroy() {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer= null;
}
super.onDestroy();
}
}
You have a start button, pause button and stop button. There is no Resume button. Also when you click start after pressing pause, media loads again and starts from beginning (length = 0). You have to seek it to the paused position and resume it :
Define a global variable :
int length = 0;
In pause listener:
length=mediaPlayer.getCurrentPosition();
In resume (create a resume listener):
mediaplayer.seekTo(length);
mediaPlayer.start();
UPDATE
Ideally pause and resume button would be same except you would toggle on-off with changing image resource between play and pause pngs and execute relevant code with help of a boolean variable :
In pause button listener:
if(play)
{
//paused
play =false;
pause.setImageResource(R.id.pause);
length=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}else {
//resumed
play = true;
pause.setImageResource(R.id.resume);
mediaplayer.seekTo(length);
mediaPlayer.start();
}
Correct wherever necessary I typed in the editor not in IDE.
Use following code -
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.reset();
I think it will help you.
I want to attach two click listener in a button and both onclicklistener should be different. Is there any way to do this.? Can't use function to call from first to second. When I use this then I got output from second one only. I want both output.
I am working on some screening task, so whenever a use click on a button it tell me that user clicked on this in Logcat and also button do its normal task.
First is :
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
}
});
Second is :
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2) {
if (v2 instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) v2).getText().toString());
}
}
});
You can't do that. Setting your second OnClickListener removes the first one.
That's why the function is called setOnClickListener() instead of addOnClickListener()
As you say, you can call two functions from the onClick()
Edit:
On your edit, you say that you want to show a message when your button is clicked.
You can add the loggging functionality that you need before doing anything else on the event, simply doing
#Override
public void onClick(View v) {
// Log something
// Your functionality
}
Or you can create a class implementing View.OnClickListener
public class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Log something
}
}
And then, on your Activity:
btn.setOnClickListener(new MyOnClickListener() {
#Override
public void onClick(View v) {
super.onClick(v);
// Your functionality
}
});
You can use function in OnClickListener like-
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
run1();
run2();
}
});
private void run1(){
//Your first functionality
}
private void run2(){
//Your second functionality
}
If I understood you correctly, you could do this:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dothings(); //what you are trying to achieve with this button click
showViewInLogCat(v); //to show you the view in the logcat
}
}
where showViewInLogCat() is a function that show you which view was clicked in your logcat:
public void showViewInLogCat(View view) {
if (view instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) view).getText().toString());
}
//and add the other if
}
You can call this function in every OnClick event on other views
Probably if you want to do something like this..!
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
// add a boolean here to check if you want to do the task or not.
doTask = true;
doGeneralTask(doTask); //to show you the view in the logcat
}
}
and in doGeneralTask(doTask) do something like this.
public void doGeneralTask(boolean doTask) {
if (doTask) {
// do whatever generalized tasks you need.
}
}
i have coded for media player and it is playing song .In the front end only image button is used and hardcoded as play button
when i click again it has to change image to pause state but the button which i have used is playing the song two times simultaneously whenever i click on it which is wrong
but i need the song to be paused so whereever i used
mp.start();
function which will be start to play the music from the url i added the code as
if(!mp.isPlaying()){
mp.start();
buttonPlayPause.setBackgroundResource(R.drawable.pause);
}else {
mp.pause();
buttonPlayPause.setBackgroundResource(R.drawable.play);}
but when i click the play button it starts playing and again if i click the button again it starts playing two times simultaneously
please help me how should i start and pause the mp3 file which i have displayed it in url it is not going to else block itself should i use different function
the full code of the project is
public class MainActivity5 extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {
MediaPlayer mp;
ProgressDialog pd;
Button bt;
ImageButton iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iv = (ImageButton)findViewById(R.id.play);
iv.setOnClickListener(this);}
#Override
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
//mp.start();
/*if(mp.isPlaying()== true)
{
mp.start();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.pause();
mp.release();
iv.setImageResource(R.drawable.play);
}*/
}
#Override
public void onClick(View v) {
try
{
pd = new ProgressDialog(this);
pd.setMessage("Buffering.....");
//pd.show();
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
mp.setDataSource("http://192.168.1.138/Android/music/vande.mp3");
mp.prepareAsync();
mp.setOnCompletionListener(this);
}
catch(Exception e)
{
Log.e("StreamAudioDemo", e.getMessage());
}
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
pd.dismiss();
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();
}}
thanks for your help in advance
i have commented some code above i have to re correct it
you can have a try at your onclickListener,when this player is playing,if you want to pause it ,you should to call MediaPlayer.pause() ,if you want to release it data,you can call MediaPlayer.release(),then ,you can play next music.
use this
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
// mp.start(); - this should be removed
if(mp.isPlaying()== true)
{
mp.pause();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.start();
mp.release();
iv.setImageResource(R.drawable.play);
}
}
My custom audio application starts to play when the Main activity begins, but I'd like it to start playing when the user hits the "Play" button. Just for the record I'm not using the Android MediaPlayer, but my own player.
When I simply use startPlayer() it runs fine, when the Activity starts, but when I put the startPlayer() invokation inside the setOnClickListener() it doesn't work.
The method startPlayer() in being called (it's logging fine), but the audio player doesn't fire up. Why?
Here is the code:
play = (Button) findViewById(R.id.playbutton);
play.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startPlayer();
}
});
}
private void startPlayer() {
try {
player.playAudio();
} catch (Exception e) {
e.printStackTrace();
finish();
}
}
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startPlayer();
}
});