I'm just starting out with my first soundboard. Basically this is what I have so far (except I have 40 sounds). Does anyone know a better way to do this? I have to go to an appointment, but I will be back later today to respond. Thank you, anyone who can help.
-------------------------soundboard--------------
package com.soundboard.app;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class main extends Activity {
MediaPlayer sound1, sound2, sound3;
ImageButton button1, button2, button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sound1 = MediaPlayer.create(this, R.raw.sound1);
button1 = (ImageButton) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound1.start();
}
});
squeak3 = MediaPlayer.create(this, R.raw.squeak3);
dogsqueak = (ImageButton) findViewById(R.id.dogsqueak);
dogsqueak.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
squeak3.start();
}
});
sound2 = MediaPlayer.create(this, R.raw.sound2);
button2 = (ImageButton) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound2.start();
}
});
sound3 = MediaPlayer.create(this, R.raw.sound3);
button3= (ImageButton) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound3.start();
}
});
}
}
With forty buttons you need to arrange for this to happen in a loop.
Although there are even more clever ways to do this, you can start by building a Map:
Map<Integer, Integer> map = new HashMap<Integer, Integer>>();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
...
and then iterate:
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
final MediaPlayer sound = MediaPlayer.create(entry.getValue());
Button button = (ImageButton) findViewById(entry.getKey());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.start();
}
});
}
This will give you a taste of a looping solution. You also need to consider how you manage your MediaPlayer instances.
Related
I am making a sound board and I have about 12 media players, when I boot up the app in my emulator, and I start clicking some of the buttons, the other buttons that weren't clicked cease to produce the sound that they were set to produce.
package com.example.husse.randomsounds;
import android.graphics.Typeface;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;
import android.widget.ImageButton;
import android.content.Intent;
public class Board_one extends AppCompatActivity {
private TextView title1;
private ImageButton cat;
private ImageButton scream;
private ImageButton next;
private ImageButton time;
private ImageButton dogg;
private ImageButton crowd;
private ImageButton gun;
private ImageButton thunder;
private ImageButton forest;
private ImageButton faucet;
private ImageButton alarm;
private ImageButton lazer;
private ImageButton shatter;
public MediaPlayer mplayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board_one);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Finding Buttons
title1 = (TextView) findViewById(R.id.textView2);
lazer = (ImageButton)findViewById(R.id.imageButton1234);
shatter = (ImageButton) findViewById(R.id.imageButton8);
alarm = (ImageButton) findViewById(R.id.imageButton91);
scream = (ImageButton) findViewById(R.id.imageButton2);
faucet = (ImageButton) findViewById(R.id.imageButton94);
cat = (ImageButton) findViewById(R.id.imageButton);
next = (ImageButton) findViewById(R.id.imageButton3);
time = (ImageButton) findViewById(R.id.imageButton4);
dogg = (ImageButton) findViewById(R.id.imageButton5);
crowd = (ImageButton) findViewById(R.id.imageButton6);
gun = (ImageButton) findViewById(R.id.imageButton78);
thunder = (ImageButton) findViewById(R.id.imageButton80);
forest = (ImageButton) findViewById(R.id.imageButton90);
//Finding buttons
Typeface mytypeface = Typeface.createFromAsset(getAssets(), "AirstreamNF.otf");
title1.setTypeface(mytypeface);
//Media Players ------------------------------------------------------------------------------------
final MediaPlayer dog = MediaPlayer.create(this, R.raw.meow);
final MediaPlayer faucetsound = MediaPlayer.create(this, R.raw.waterfall);
final MediaPlayer thundersound = MediaPlayer.create(this, R.raw.thundersound);
final MediaPlayer gunshot = MediaPlayer.create(this, R.raw.gunshot);
final MediaPlayer scare = MediaPlayer.create(this, R.raw.screaming);
final MediaPlayer baby = MediaPlayer.create(this, R.raw.timesup);
final MediaPlayer bark = MediaPlayer.create(this, R.raw.dogbarking);
final MediaPlayer boo = MediaPlayer.create(this, R.raw.wow);
final MediaPlayer forestsounds = MediaPlayer.create(this, R.raw.rainforestsound);
final MediaPlayer alaramsound = MediaPlayer.create(this, R.raw.alarm);
final MediaPlayer Lazerso = MediaPlayer.create(this, R.raw.laserbeam);
final MediaPlayer shattersound = MediaPlayer.create(this, R.raw.shattering);
// Animations ---------------------------------------------------------------------
final Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(100);
//---------------------------------------------------------------------------
cat.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
cat.startAnimation(animation);
dog.start();
}
}
);
scream.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
scream.startAnimation(animation);
scare.start();
}
}
);
next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent dab = new Intent("com.example.husse.randomsounds.Board_two");
startActivity(dab);
}
}
);
time.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
time.startAnimation(animation);
baby.start();
}
}
);
dogg.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
dogg.startAnimation(animation);
bark.start();
}
}
);
crowd.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
crowd.startAnimation(animation);
boo.start();
}
}
);
gun.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
gun.startAnimation(animation);
gunshot.start();
}
});
thunder.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
thunder.startAnimation(animation);
thundersound.start();
}
}
);
forest.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
forest.startAnimation(animation);
forestsounds.start();
}
}
);
faucet.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
faucet.startAnimation(animation);
faucetsound.start();
}
}
);
alarm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
alarm.startAnimation(animation);
alaramsound.start();
}
}
);
shatter.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
shatter.startAnimation(animation);
shattersound.start();
}
}
);
lazer.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
lazer.startAnimation(animation);
Lazerso.start();
}
}
);
}
}
I would recomend you using only one mediaplayer:
MediaPlayer mp;
Then, inside onCreate method:
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
And on your onClick listeners:
mp.setDataSource(this,R.raw.gunshot);
mp.prepareAsync();
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.
I was wondering if you guys can help me on my app. It’s something really easy for you guys that I’m missing. I’m trying to link buttons on one layout to navigate to other layouts. These six buttons should go to their six different layouts…
Button 7 should go to the layout Number7
I already did Button 1 and it works to number1.
Here’s a screenshot of the layout and here is my code to my main.java: http://imgur.com/zLJ3cdE
package com.example.isthisyourluckyday;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Main.this, Number1.class));
}
});
}
}
Also….. Here is number7.java which should link to the number7 layout
package com.example.isthisyourluckyday;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Number7 extends Activity {
Button button7;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.number7);
}}
If you guys can please help me out I’d really appreciate it.
If the first button works, where is the problem with the others ?
Below all the code :
public class Main extends Activity implements View.OnClickListener {
private final Button button1;
private final Button button2;
private final Button button3;
private final Button button4;
private final Button button5;
private final Button button6;
private final Button button7;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.equals(button1))
startActivity(new Intent(Main.this, Number1.class));
else if(v.equals(button2))
startActivity(new Intent(Main.this, Number2.class));
else if(v.equals(button3))
startActivity(new Intent(Main.this, Number3.class));
else if(v.equals(button4))
startActivity(new Intent(Main.this, Number4.class));
else if(v.equals(button5))
startActivity(new Intent(Main.this, Number5.class));
else if(v.equals(button6))
startActivity(new Intent(Main.this, Number6.class));
else if(v.equals(button7))
startActivity(new Intent(Main.this, Number7.class));
}
}
The Main class can implement OnClickListener to have only one onClick() function.
I did not test this properly, but you get the idea. It is similar to the other answer, but the code is a bit cleaner and has less repetition in my opinion.
package com.example.isthisyourluckyday;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements View.OnClickListener {
final Button[] buttons = new Button[7];
final Class[] classes = { Number1.class, Number3.class, Number3.class,
Number4.class, Number5.class, Number6.class, Number7.class };
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttons[0] = (Button) findViewById(R.id.button1);
buttons[1] = (Button) findViewById(R.id.button2);
buttons[2] = (Button) findViewById(R.id.button3);
buttons[3] = (Button) findViewById(R.id.button4);
buttons[4] = (Button) findViewById(R.id.button5);
buttons[5] = (Button) findViewById(R.id.button6);
buttons[6] = (Button) findViewById(R.id.button7);
for(Button b : buttons){
b.setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
for(int i=0; i<buttons.length; i++){
if(buttons[i].equals(v)){
Intent intent = new Intent(Main.this, classes[i]);
startActivity(intent);
}
}
}
}
When you start an activity like this
startActivity(new Intent(Main.this, Number1.class));
you are telling Android to start the Number1 activity. So if you want to start another activity you just do the same thing you did:
final Button b7 = (Button) findViewById(R.id.button7);
b7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Number7.class));
}
});
This is not elegant, but will do the trick.
You could also define a "tag" on each button like
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:onClick="choose"/>
then in your activity add a method
public void choose(View view) {
String number = view.getTag().toString();
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.example.isthisyourluckyday", "com.example.isthisyourluckyday.Number"+number));
startActivity(intent);
}
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.
I'm trying to create an app that has many pages, every page has button.
When I click on the button it takes me to next page.
What i want to do is when I press on the button it plays the sound that i have included in "raw folder?
At this moment when I manipulated the code and I press on the button to change the page it close the app. Can anyone please help. Thanks
here is my code:
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button button;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp;
#Override
public void onClick(View view) {
mp.seekTo(0);
mp.start();
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.test);
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
}
#Override
public void onPause() {
super.onPause();
// Release the MediaPlayer if going into background
if(mp != null) mp.release();
} }
If this is how your code is laid out:
button.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp;
#Override
public void onClick(View view) {
mp.seekTo(0);
mp.start();
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.test);
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
Then the reason is because you're trying to use the MediaPlayer before actually initializing it. Try this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
final MediaPlayer mp = MediaPlayer.create(context, R.raw.test);
mp.start();
}
catch(Exception e) { e.printStackTrace(); } // Eat it
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
If that doesn't work, well, something is definitely wrong somewhere else.