• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Multiple Audio Buttons

**********************************************************************
!!Before you read!! In the end i switched over to sound pool. Its much better for small sound files, and the only way to go if you have lots of them.
**********************************************************************

This is the code i am using, But if I try to add more audio buttons it force closes. How do I add lots of audio buttons, and not get this problem?

I want 80 audio buttons spread out on two activities. If you could help me get 3 working in one activity in a way that i can add more, i'd really appreciate it!! :)



Code:
package com.justin.guru;






import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class GurusoundbroardActivity extends Activity {
   
	MediaPlayer mp; 
    final Context mContext = this; 
	
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
            
             
                    
                
                 
                Button yes = (Button) findViewById(R.id.yes); 
                yes.setOnClickListener(new View.OnClickListener() { 
                    public void onClick(View v) { 
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.yes); 
                        mp.start(); 
                    } 
                });        

    
    }
}
 
Code:
package com.justin.audiotest;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AudioTestActivity extends Activity implements OnClickListener {
   MediaPlayer mp;
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
        
      Button no = (Button)findViewById(R.id.no);
      no.setOnClickListener(this);
      
      
      Button yes = (Button)findViewById(R.id.yes);
      yes.setOnClickListener(this);
      
      
      
   }

   @Override
   public void onClick(View v) {
      
      mp = MediaPlayer.create(this, R.raw.alldownhillfromhere);
      mp.start();
      while (mp.isPlaying()) { 
      };
      mp.release();
   }










};
 
Upvote 0
You can create an ArrayList and store your buttons in it but not sure that is what you want but I give my suggestion anyway.
Here is the code:

<code>
LinearLayout l;
ArrayList<Button> arrButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l = (LinearLayout)findViewById(R.id.radio_linear);

arrButton = new ArrayList<Button>();
for(int i=0;i<80;i++) {
Button b = new Button(this);
b.setOnClickListener(this);
b.setText("Button "+i);
// set more button settings here like color, coordinates etc.
arrButton.add(b);
l.addView(b);
}

}
}
</code>

And to see all buttons you need scrollview in xml file.
<code>
<ScrollView android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</code>
 
Upvote 0
You can create an ArrayList and store your buttons in it but not sure that is what you want but I give my suggestion anyway.

I have no problem with the buttons I have the xml done. I just want to know how to have each button play a different sound. What i try keeps crashing.
XML code
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:orientation="horizontal" android:background="@drawable/namek"
	android:id="@+id/no">
	<LinearLayout android:orientation="vertical"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
		<Button android:layout_height="wrap_content" android:id="@+id/yes"
			android:text="@string/yes" android:layout_width="fill_parent"></Button>
		<Button android:id="@+id/Button29" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button33" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button34" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button35" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button36" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button30" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button31" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="vertical"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
		<Button android:id="@+id/Button19" android:layout_height="wrap_content"
			android:text="@string/no" android:layout_width="fill_parent"></Button>
		<Button android:id="@+id/Button20" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button24" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button25" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button26" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button27" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button21" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button22" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="vertical"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
		<Button android:id="@+id/Button10" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button11" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button15" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button16" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button17" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button18" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button12" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button13" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="vertical"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
		<Button android:id="@+id/Button01" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button02" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button06" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button07" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button08" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button09" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button03" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/Button04" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="vertical"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_gravity="right">
		<Button android:id="@+id/button1" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:id="@+id/button2" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:text="Button"></Button>
		<Button android:text="Button" android:id="@+id/button3"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button" android:id="@+id/button4"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button" android:id="@+id/button5"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button" android:id="@+id/button6"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button" android:id="@+id/button7"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button" android:id="@+id/button8"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	</LinearLayout>
</LinearLayout>

I know this code words to go to a new page

Code:
Button greenButton = (Button)findViewById(R.id.green_button);
        greenButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(
						RedflashlightActivity.this,
						GreenflashlightActivity.class
				);
				
				startActivity(intent);
				
			}
		});

But is there a way that i can do the same kind of thing with audio? Is there some way i can get this to work?

Code:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AudioTestActivity extends Activity implements OnClickListener {
   MediaPlayer mp;
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
        
      Button greenButton = (Button)findViewById(R.id.yes);
      greenButton.setOnClickListener(new View.OnClickListener() {
    	  

    	  
      }
      @Override
      public void onClick(View v) {      
         mp = MediaPlayer.create(this, R.raw.alldownhillfromhere);
         mp.start();
         while (mp.isPlaying()) { 
         };
         mp.release();
      }
   };

:thinking::thinking::thinking:
 
Upvote 0
Try doing this:

Code:
// create a class

public class Music {
	private static MediaPlayer mp = null;
	private static MediaPlayer mpEffect = null;
	public static void play(Context context, int resource) {
		stop(context);
		if (Prefs.getMusic(context)) {
			mp = MediaPlayer.create(context, resource);
			mp.setLooping(true); // remove this if don't want to play the song continuously
			mp.start();
			}
	}
	
	public static void playEffect(Context context, int resource) {
		if(Prefs.getEffect(context)) {
			mpEffect = MediaPlayer.create(context, resource);
			mpEffect.start();
		}
	}
	
	public static void stop(Context context) {
		if (mp != null) {
			mp.stop();
			mp.release();
			mp = null;
			}
		}

}

// When calling from activity class
Music.playEffect(context, R.raw.nameOfSong);
 
  • Like
Reactions: VTNotInCtrl
Upvote 0
I am have trouble understanding what is going on with your code raptiye;

So if I want to make a class that when called only plays one sound how would I go about that. I have played with the code you posted but can not seem to get it to work?

classs name is Yes

The audio file is R.raw.yes

I would like to call it with

Music.playEffect(yes, R.raw.yes);

If some one can bear with me, and help me understand this Ill owe you big time.

Can you help me out
 
Upvote 0
I have one working but when i try to add two the app does not work. The app crashes on start up =(


The page that will call the class that plays the sound.
Code:
public class GurusoundbroardActivity extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        Button yes = (Button) findViewById(R.id.yes); 
        yes.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
            	Yes.playEffect(getBaseContext(), R.raw.yes); 
            } 
        }); 
       Button no = (Button) findViewById(R.id.no); 
       no.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
            	No.playEffect(getBaseContext(), R.raw.no); 
            } 
        }); 
	
	
	}}

The Class that plays sound when called
Code:
public class Yes {
	private static MediaPlayer mp = null;
	private static MediaPlayer mpEffect = null;
	public static void play(Context context, int resource) {
		stop(context);
		 {
			mp = MediaPlayer.create(context, R.raw.yes);
			mp.start();
			}
	}
	
	public static void playEffect(Context context, int resource) {
		 {
			mpEffect = MediaPlayer.create(context, R.raw.yes);
			mpEffect.start();
		}
	}
	
	public static void stop(Context context) {
		if (mp != null) {
			mp.stop();
			mp.release();
			mp = null;
			}
		}

}

This class is the second audio file

Code:
public class No {
	private static MediaPlayer mp = null;
	private static MediaPlayer mpEffect = null;
	public static void play(Context context, int resource) {
		stop(context);
		 {
			mp = MediaPlayer.create(context, R.raw.no);
			mp.start();
			}
	}
	
	public static void playEffect(Context context, int resource) {
		 {
			mpEffect = MediaPlayer.create(context, R.raw.no  );
			mpEffect.start();
		}
	}
	
	public static void stop(Context context) {
		if (mp != null) {
			mp.stop();
			mp.release();
			mp = null;
			}
		}

}
 
Upvote 0
Your app works on my emulator but I added this lines under manifest file and inside application tag.

Code:
<activity android:name=".Yes"
              android:label="Play">
        </activity>
<activity android:name=".No"
              android:label="Play">
        </activity>

Now I want to add a few more things, you can do playing the sound with only one class and you need only one method.
Code:
public class Yes {
	private static MediaPlayer mp = null;
	
	public static void play(Context context, int resource) {
		mp = MediaPlayer.create(context, resource);
		mp.start();
	}
	
	public static void stop(Context context) {
		if (mp != null) {
			mp.stop();
			mp.release();
			mp = null;
			}
		}

}
 
  • Like
Reactions: VTNotInCtrl
Upvote 0
This was very educational. I didn't know you could call media files to play from their own activity. I was wondering if you intended on each button to stop the previously clicked sound and begin a new sound, because that is what I was shooting for and this code doesn't do that. All the sounds overlap as I press multiple buttons in a row.

Also, I am interested in seeing what you ended up doing with SoundPool. Yesterday was the first time I read about that function.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones