Play audio sound on button click with no repeating - android

I want to either be able to have a single button that you click to start and click again to stop or a start button that does not start multiple sounds each time it clicks. I have an audio file that if you click start 3 times, it has 3 different tracks going all at once. And the stop button stops only one, the other tracks keep playing until I kill the app.
Here is my code. I am a noob
package com.example.mitch_000.button;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player=MediaPlayer.create(MyActivity.this,R.raw.lw);
player.start();}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
}

When you call player=MediaPlayer.create(MyActivity.this,R.raw.lw); in your OnClickListener, you are creating a new instance of the audio file and then playing it every time the button is clicked.
You need to create a single instance of the player object outside of the OnClickListener (in OnCreate) and then you can use this reference to play, pause and stop the one audio file.
Here is the code you can use:
package com.example.mitch_000.button;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
player=MediaPlayer.create(MyActivity.this,R.raw.lw);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}

put
MediaPlayer mp = MediaPlayer.create(this, R.raw.mamacita_zero);
outside of setOnClickListener
like this
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
MediaPlayer mp = MediaPlayer.create(MyActivity.this,R.raw.lw);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
}
You seem to be doing per click than you need to. You're creating and adding a new onClickListener for every click in the Activity's View, not the Button. You only need to set the listener once, and for the Button rather than the overarching View; I tend to do that in the constructor of the Activity.

Related

click listener is not working

Hi I am new in android learning and I'm practice it. I Want to implement click listener on my button but it is giving me error. even i imported android.view.View.OnClickListener; and other all libraries.
please have look at my code check where is my mistake
Cannot Resolve symbol onClickelistner.
package com.example.nexus.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
}
button.OnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
Try this and put it in onCreate method
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
Combine R2R and Toan Tran answers will give you the best solution:
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
This should work.
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
}
});
Try button click inside Oncreate()
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
}

MediaPlayer not working at all

I am a very beginner in Android, I am trying to make a MediaPlayer works but I have some errors.
1-If I click on play again , start playing twice at the same time.
2-If click on puse, doesn't happen anything.
3-My intention is to do a reproduction list with play, pause and stop.
Thanks in advance.
package com.example.android.allmusic;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.media.MediaPlayer;
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (!mp.isPlaying()) {mp.start();}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (mp.isPlaying()) { mp.pause(); }
}
});
}
}
Try declaring your MediaPlayer as a member variable, it should be enough to set it on your onCreate, but just declare it as an attribute of your class to test.
Since you are creating a new reference to the MediaPlayer every time you click on the ImageView or the TextView, you don't the old reference to pause it or to know if it has already been started.
Code:
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
//MediaPlayer as a member variable
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mp.isPlaying()) {
mp.start();
}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mp.isPlaying()) {
mp.pause();
}
}
});
}
}

When music doesn't Start, Stop button crashing the project

When I push the next button, I want to go next layout and stop still playing music.
If I push the play button and push the next button app is working.
But, if music doesn't play and I push to the next layout button, the app is crashing because my code try to stopped even though music isn't playing. What can i do ?
package com.gamedom.relaxingmusic;
import gif.decoder.GifRun;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
public class Music1 extends Activity {
public static MediaPlayer mp1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music1);
final ImageButton Start1 =(ImageButton) findViewById(R.id.btnStart1);
final ImageButton Stop1 =(ImageButton) findViewById(R.id.btnStop1);
ImageButton SOL1 =(ImageButton) findViewById(R.id.btnSol1);
ImageButton SAG1 =(ImageButton) findViewById(R.id.btnSag1);
Stop1.setVisibility(View.INVISIBLE);
Start1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1=MediaPlayer.create(Music1.this, R.raw.firefurnace);
mp1.start();
mp1.setLooping(true);
Start1.setVisibility(View.INVISIBLE);
Stop1.setVisibility(View.VISIBLE);
}
});
Stop1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.stop();
mp1.release();
Stop1.setVisibility(View.INVISIBLE);
Start1.setVisibility(View.VISIBLE);
}
});
//////NEXT LAYOUT BUTTON \\\\\\\\\
SAG1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC2"));
}
});
////// BACK LAYOUt BUTTON \\\\\\\\\
SOL1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC4"));
}
});
}
Problem is here
SOL1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//It try to stoping the music but it's already stopped program is crashes
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC4"));
}
});
It looks like you initialize your media player object in an onClick. So, if that event is not fired, your mp1 variable will be null. I believe you are getting NullPointerException.

Android TextView and Buttons not working in my app

I am learning Android and trying one simple Android app development, I got one demo code from my lecture and the teacher simply do the following:
There are 2 buttons, 1 textview. When touching button A, it will show "text A" in the textview, while touching button B, it will present "text B" in textview.
I followed the code and rewrote it, but i can't get the correct result when I ran with emulator.
When I touch either button, there's no content in the TextView. But my teacher's reference code works:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
How is report(View v) called? I can't understand how this class is called. Could someone please help me out?
You need to let your button know that, when pressed, report() should be called. This may be done through the android:onClick attribute of your button on your layout's XML:
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:onClick="report" />
Or by code, attaching an OnClickListener to the button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
report(v);
}
});
}
Hope it helps.
You should implements the View.OnClickListener, and override that OnClick method. You also need to setOnClickListener on your buttons. So your code should be like:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity implements View.OnClickListener{
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 =(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
button2 =(Button)this.findViewById(R.id.button2):
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}

how to call which button to work in android

I have an activity class with many buttons. If i click one button then it will go to next page then go back to main class.If I click another button in main class, it will go to next page together with data. Do anyone know how to write the function in activity class?
Can I write like this in a class? But when i run it only one button is working , when i clicked other i get error. I am new to android ,so please give me suggestion.
public class MyClass extends Activity {
private Button button,button1,button2;
public void onCreate(){.... initControl();}
public void initControl() { button=(Button)findViewById(R.id.button); .....
button.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button1.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button2.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
}
thanks for help.
First initialize button1, button2 before overriding onCreate(). Then assign the values in oncreateMethod call initializemthod
Have a look on the following code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class WebViewTest extends Activity {
Button button1 = null;
Button button2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.firstbutton);
button2 = (Button) findViewById(R.id.secondbutton);
initControl();
}
public void initControl() {
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
}
Thanks
Deepak
You have to add on click method

Categories

Resources