I am running into problems with use of multiple mediaplayes - android

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();

Related

Opening Specific Card item in RecycleAdapter from different Activity

I will make this as specific as possible. I've created a Page Which uses RecyclerAdapter to show card items in grid.And I have set a Default Activity for each card using OnclickListner ,So when you click any card item it will take you to that Default Activity Like this Image.And mine one Look Like this click here.My question is how can I open a perticular card in recyler Adapter from a different activity.In my app I want that when I press Go button on popup It will it toggle lvl2 or the next card to open the game activity.
Java file for RecyclerAdapter
package com.example.apptuzzle;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mcontext;
private List<InGameContent> mData;
public RecyclerViewAdapter(Context mcontext, List<InGameContent> mData){
this.mcontext = mcontext;
this.mData = mData;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mcontext);
view = mInflater.inflate(R.layout.cardview_items1,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.Title.setText(mData.get(position).getTitle());
holder.Title.setTag(position);
holder.cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (int) getItemId(position) + 1;
Intent intent = new Intent(mcontext,InGame_wrkfunction.class);
intent.putExtra("pos", pos);
//passing data to the InGame_wrkfunction.class
intent.putExtra("Answrr",mData.get(position).getAnswrr());
intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
intent.putExtra("Description",mData.get(position).getDescription());
// start the activity
mcontext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title;
CardView cardview;
public MyViewHolder(View itemView) {
super(itemView);
Title = (TextView) itemView.findViewById(R.id.LvL_id1);
cardview = (CardView) itemView.findViewById(R.id.cardview_id);
}
}
}
Default Game Activity
package com.example.apptuzzle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class InGame_wrkfunction extends AppCompatActivity {
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btndot; //Buttons Declaration
private Button btnclr, btnsubm, popup_g_button1;
private TextView txtv1, txtv2, popup_g_msg1, popup_g_des1,highscore1;
int scores = 0;
private LinearLayout Popup_layout11;
private Animation pop_animation;
private ImageView img,popup_g_img1,popup_b_img; //Question Declaration
private TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_game_wrkfunction);
btn0 = (Button) findViewById(R.id.Btn0);
btn1 = (Button) findViewById(R.id.Btn1);
btn2 = (Button) findViewById(R.id.Btn2);
btn3 = (Button) findViewById(R.id.Btn3);
btn4 = (Button) findViewById(R.id.Btn4);
btn5 = (Button) findViewById(R.id.Btn5);
btn6 = (Button) findViewById(R.id.Btn6);
btn7 = (Button) findViewById(R.id.Btn7);
btn8 = (Button) findViewById(R.id.Btn8);
btn9 = (Button) findViewById(R.id.Btn9);
btndot = (Button) findViewById(R.id.Btndot);
btnclr = (Button) findViewById(R.id.BtnClr);
btnsubm = (Button) findViewById(R.id.BtnSubm);
img = (ImageView) findViewById(R.id.gamethumbnail);
txtv1 = (TextView) findViewById(R.id.TxtV1);
txt = (TextView) findViewById(R.id.TxtV2);
Popup_layout11 = findViewById(R.id.Popup_layout11);
txt = (TextView) findViewById(R.id.TxtV2);
popup_g_button1 = findViewById(R.id.Popup_G_button1);
popup_g_msg1 = findViewById(R.id.Popup_G_msg1);
popup_g_des1 = findViewById(R.id.Popup_G_des1);
popup_g_img1 = findViewById(R.id.Popup_G_Img12);
highscore1 = findViewById(R.id.ScoreCounter1);
pop_animation = AnimationUtils.loadAnimation(this, R.anim.pop_up);
btn0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "0");
}
});
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + "9");
}
});
btndot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
txtv1.setText(txtv1.getText() + ".");
}
});
btnclr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtv1.setBackground(null);
txtv1.setText(null);
}
});
// Receive data
Intent intent = getIntent();
String Description = intent.getExtras().getString("Description"); //It ll fetch data from Description()
int image = intent.getExtras().getInt("Thumbnail");
int pos = intent.getExtras().getInt("pos");
final String answrr1 = intent.getExtras().getString("Answrr");
//Setting Values
img.setImageResource(image); ////It ll replace the image that was fetched previously
txt.setText(Description);
//Load Scores
SharedPreferences myscores = this.getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
scores = myscores.getInt("scores", 0); //0 tha phehele
highscore1.setText("Scores:" + scores);
btnsubm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String a = txtv1.getText().toString();
if (a.equals(answrr1)) {
scores += 30;
//Save scores
SharedPreferences myscores = getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myscores.edit();
editor.putInt("scores", scores);
editor.commit();
v.setOnClickListener(null);//Remove setOnClickListener
highscore1.setText("Scores:" + scores);
Popup_layout11.setVisibility(LinearLayout.VISIBLE);
Popup_layout11.setAnimation(pop_animation);
Popup_layout11.animate();
pop_animation.start();
} else {
scores -= 30;
//Save scores
SharedPreferences myscores = getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myscores.edit();
editor.putInt("scores", scores);
editor.commit();
v.setOnClickListener(null);//Remove setOnClickListener
highscore1.setText("Scores:" + scores);
popup_g_img1.setImageResource(R.drawable.sadface11);
popup_g_msg1.setText("Ohh Snap!");
popup_g_des1.setText("Don't worry Kid you just tap the button to move on to the next lesson. REMEMBER 'never waste time'.");
Popup_layout11.setVisibility(LinearLayout.VISIBLE);
Popup_layout11.setAnimation(pop_animation);
Popup_layout11.animate();
pop_animation.start();
}
}
});
}
}
/*Two Ways of Putting One Animation In an activity
* 1st One
* Anim1 = AnimationUtils.loadAnimation(this, R.anim.new_animfile1);
// pop_img1.setVisibility(View.GONE);
Btn1.setOnClickListener(new View.OnClickListener() { <-----In this type you can put animation inside On Click Listener
#Override
public void onClick(View v) {
Layout1.setVisibility(LinearLayout.VISIBLE);
Anim1.setDuration(500);
Layout1.setAnimation(Anim1);
Layout1.animate();
Anim1.start();
}
});
*
*
* 2nd One
* public class MyActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); <-----In this type you can set another class outside the OnCreate Bundle
}
public void animate(View view){
LinearLayout dialog = (LinearLayout)findViewById(R.id.dialog);
dialog.setVisibility(LinearLayout.VISIBLE);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
animation.setDuration(500);
dialog.setAnimation(animation);
dialog.animate();
animation.start();
}
}
* */
What I understand is you want to open different activities on each card click.If it's correct then,
use switch inside your onClick and open your activity on a particular item click
#Override
public void onClick(View v) {
final Intent intent;
switch (getAdapterPostion()){
case 0:
intent = new Intent(context, FirstActivity.class);
break;
case 1:
intent = new Intent(context, SecondActivity.class);
break;
...
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
context.startActivity(intent);
}
Or Use:-
#Override
public void onClick(View v) {
final Intent intent;
if (getAdapterPosition() == sth){
intent = new Intent(context, OneActivity.class);
} else if (getPosition() == sth2){
intent = new Intent(context, SecondActivity.class);
} else {
intent = new Intent(context, DifferentActivity.class);
}
context.startActivity(intent);
}

I can not pass the parameter i to the button[i]

I am trying to play a sound on button click and I am trying to make it call the .start() function only but the problem is I set the i parameter in the onclick function and so it does not pass the i to the onclicklistner function any ideas how to fix that ??
here is my code
package com.example.buttonsdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
//Creating Sound arrays
int i=0;
MediaPlayer[] mediaplayer = new MediaPlayer[120];
Button button[] = new Button [120];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize the mediaplayer
for(int z=0;z<120;z++)
{
mediaplayer[z]=null;
}
//Initialize Button Array
for(int x=0;x<120;x++)
{
button[x]=new Button(this);
button[x].setOnClickListener(this);
}
//Creating Media player array
mediaplayer[0]= MediaPlayer.create(this,R.raw.akali);
mediaplayer[1]= MediaPlayer.create(this,R.raw.alistar);
button[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.akali:
i=0;
break;
case R.id.alistar:
i=1;
break;
}
mediaplayer[i].start();
}
} );
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Inside your Listener, you want to use a variable which is declared inside your Listener.
You are trying to use a variable (i) which is declared in code which is not run when the Listener is run.
new View.OnClickListener() {
#Override
public void onClick(View v) {
int idx;
switch(v.getId()) {
case R.id.akali:
idx=0;
break;
case R.id.alistar:
idx=1;
break;
}
mediaplayer[idx].start();
}
}
Or invoke the respective start() method right in the case.
Dump the 'i' declared. Replace method with this (the "Button theButton" needs to stay 'final').
for(int z=0;z<120;z++)
{
final Button theButton = button[z];
theButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
// do what you want.
}
});
}
I believe your onCreate should be created like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize the mediaplayer
for(int z=0;z<120;z++)
{
mediaplayer[z]=null;
}
mediaplayer[0]= MediaPlayer.create(this,R.raw.akali);
mediaplayer[1]= MediaPlayer.create(this,R.raw.alistar);
//Initialize Button Array
for( x=0;x<120;x++)
{
button[x]=new Button(this);
button[x].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaplayer[x].start();
}
} );
}
}
So basically you just need to make several button on the fly which produces different sound when clicked. Try this code:
Button myButton[] = new Button[100];
MediaPlayer mySound = new MediaPlayer();
for (int i=0; i<100; i++) {
myButton[i] = new Button(this);
myButton[i].setId(i+1);
myButton[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Resources res = context.getResources();
int soundId = res.getIdentifier("sound" + v.getId(), "raw", context.getPackageName());
mySound.create(getApplicationContext(), soundId);
mySound.start();
}
});
}

How to change the state of song from stop to start?

I have make one application in android in which i take three buttons in xml and in java i set the onclick event,
when play button is clicked song is prepared and play,
when stop button is clicked song is stopped,
when pause button is clicked song is paused.
But my problem is that when I clicked play button song is played after i click on stop button then song is stopped after I clicked on play button, but at this time song is not played,
the code I created for this app. is given below.
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(position > 0)
{
mp.seekTo(position);
mp.start();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
see this Updated code:
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp.isPlaying() == false)
{
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
Edit ::
you need to apply condition in play event like ::
if(mp.isplaying == false)
{
//play the song
}
bStop.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
mp.release();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp.isplaying == false)
{
mp = MediaPlayer.create(this, R.raw.fluet);
}
}
});

Android Soundboard wont play sounds

I have created a soundboard and everytime i click on a button to play a certain sound nothing happens. Here is the sound manager code:
package com.androidbook.ufgsoundboard;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager extends Activity {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
}
And here is the soundboard code:
package com.androidbook.ufgsoundboard;
import com.androidbook.ufgsoundboard.SoundManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Soundboard extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.petera);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);
mSoundManager.addSound(2, R.raw.sound2);
mSoundManager.addSound(3, R.raw.sound3);
mSoundManager.addSound(5, R.raw.sound5);
mSoundManager.addSound(7, R.raw.sound7);
mSoundManager.addSound(9, R.raw.sound9);
mSoundManager.addSound(10, R.raw.sound10);
mSoundManager.addSound(11, R.raw.sound11);
mSoundManager.addSound(13, R.raw.sound13);
mSoundManager.addSound(18, R.raw.sound18);
mSoundManager.addSound(19, R.raw.sound19);
mSoundManager.addSound(20, R.raw.sound20);
mSoundManager.addSound(21, R.raw.sound21);
mSoundManager.addSound(22, R.raw.sound22);
mSoundManager.addSound(23, R.raw.sound23);
mSoundManager.addSound(24, R.raw.sound24);
mSoundManager.addSound(28, R.raw.sound28);
mSoundManager.addSound(30, R.raw.sound30);
Button SoundButton1 = (Button)findViewById(R.id.button3);
SoundButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button SoundButton2 = (Button)findViewById(R.id.button4);
SoundButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
Button SoundButton3 = (Button)findViewById(R.id.button5);
SoundButton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(30);
}
});
Button SoundButton4 = (Button)findViewById(R.id.button6);
SoundButton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(3);
}
});
Button SoundButton5 = (Button)findViewById(R.id.button7);
SoundButton5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(5);
}
});
Button SoundButton6 = (Button)findViewById(R.id.button8);
SoundButton6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(28);
}
});
Button SoundButton7 = (Button)findViewById(R.id.button9);
SoundButton7.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(9);
}
});
Button SoundButton8 = (Button)findViewById(R.id.button10);
SoundButton8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(10);
}
});
Button SoundButton9 = (Button)findViewById(R.id.button11);
SoundButton9.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(11);
}
});
Button SoundButton10 = (Button)findViewById(R.id.button12);
SoundButton10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(13);
}
});
Button SoundButton11 = (Button)findViewById(R.id.button13);
SoundButton11.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(18);
}
});
Button SoundButton12 = (Button)findViewById(R.id.button14);
SoundButton12.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(19);
}
});
Button SoundButton13 = (Button)findViewById(R.id.button15);
SoundButton13.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(20);
}
});
Button SoundButton14 = (Button)findViewById(R.id.button16);
SoundButton14.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(21);
}
});
Button SoundButton15 = (Button)findViewById(R.id.button17);
SoundButton15.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(9);
}
});
Button SoundButton16 = (Button)findViewById(R.id.button18);
SoundButton16.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(23);
}
});
Button SoundButton17 = (Button)findViewById(R.id.button19);
SoundButton17.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(22);
}
});
Button SoundButton18 = (Button)findViewById(R.id.button20);
SoundButton18.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(24);
}
});
}};
I cant work out why it wont load. The button highlights when pressed but no sound. Am I missing something obvious?
Thanks
Your code is so complicated ,Use this instead it worked for me !!
This is an example for a single button that plays sound
package com.example.soundtest;
import android.app.Activity;
import android.media.SoundPool;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button white;
SoundPool spool;
int soundID;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.anysoundfile, 1);
white = (Button)findViewById(R.id.button1);
white.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Sound();
}
});
}
public void Sound(){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
spool.play(soundID, volume, volume, 1, 0, 1f);
};
}

ImageButton Soundboard android app

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.

Categories

Resources