1. Check out our companion app, Forums for Android! Download from Google Play

Multiple Audio Buttons

Discussion in 'Application Development' started by VTNotInCtrl, Jul 6, 2011.

  1. VTNotInCtrl

    VTNotInCtrl Member
    Thread Starter
    5

    Jul 6, 2011
    8
    0
    5
    **********************************************************************
    !!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 (Text):
    1.  
    2. package com.justin.guru;
    3.  
    4.  
    5.  
    6.  
    7.  
    8.  
    9. import android.app.Activity;
    10. import android.content.Context;
    11. import android.media.MediaPlayer;
    12. import android.os.Bundle;
    13. import android.view.View;
    14. import android.widget.Button;
    15.  
    16. public class GurusoundbroardActivity extends Activity {
    17.    
    18.     MediaPlayer mp;
    19.     final Context mContext = this;
    20.    
    21.    
    22.    
    23.     @Override
    24.     public void onCreate(Bundle savedInstanceState) {
    25.         super.onCreate(savedInstanceState);
    26.         setContentView(R.layout.main);
    27.          
    28.            
    29.              
    30.                    
    31.                
    32.                  
    33.                 Button yes = (Button) findViewById(R.id.yes);
    34.                 yes.setOnClickListener(new View.OnClickListener() {
    35.                     public void onClick(View v) {
    36.                         mp = MediaPlayer.create(getApplicationContext(), R.raw.yes);
    37.                         mp.start();
    38.                     }
    39.                 });        
    40.  
    41.    
    42.     }
    43. }    
    44.  
    45.  
     

    Advertisement

  2. VTNotInCtrl

    VTNotInCtrl Member
    Thread Starter
    5

    Jul 6, 2011
    8
    0
    5
    Code (Text):
    1.  
    2. package com.justin.audiotest;
    3.  
    4. import android.app.Activity;
    5. import android.media.MediaPlayer;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10.  
    11. public class AudioTestActivity extends Activity implements OnClickListener {
    12.    MediaPlayer mp;
    13.    
    14.    @Override
    15.    public void onCreate(Bundle savedInstanceState) {
    16.       super.onCreate(savedInstanceState);
    17.       setContentView(R.layout.main);
    18.        
    19.       Button no = (Button)findViewById(R.id.no);
    20.       no.setOnClickListener(this);
    21.      
    22.      
    23.       Button yes = (Button)findViewById(R.id.yes);
    24.       yes.setOnClickListener(this);
    25.      
    26.      
    27.      
    28.    }
    29.  
    30.    @Override
    31.    public void onClick(View v) {
    32.      
    33.       mp = MediaPlayer.create(this, R.raw.alldownhillfromhere);
    34.       mp.start();
    35.       while (mp.isPlaying()) {
    36.       };
    37.       mp.release();
    38.    }
    39.  
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49. };
    50.  
    51.  
     
  3. raptiye

    raptiye Member
    16

    Jun 20, 2011
    22
    3
    16
    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>
     
  4. VTNotInCtrl

    VTNotInCtrl Member
    Thread Starter
    5

    Jul 6, 2011
    8
    0
    5
     
  5. raptiye

    raptiye Member
    16

    Jun 20, 2011
    22
    3
    16
    Try doing this:

    Code (Text):
    1.  
    2. // create a class
    3.  
    4. public class Music {
    5.     private static MediaPlayer mp = null;
    6.     private static MediaPlayer mpEffect = null;
    7.     public static void play(Context context, int resource) {
    8.         stop(context);
    9.         if (Prefs.getMusic(context)) {
    10.             mp = MediaPlayer.create(context, resource);
    11.             mp.setLooping(true); // remove this if don't want to play the song continuously
    12.             mp.start();
    13.             }
    14.     }
    15.    
    16.     public static void playEffect(Context context, int resource) {
    17.         if(Prefs.getEffect(context)) {
    18.             mpEffect = MediaPlayer.create(context, resource);
    19.             mpEffect.start();
    20.         }
    21.     }
    22.    
    23.     public static void stop(Context context) {
    24.         if (mp != null) {
    25.             mp.stop();
    26.             mp.release();
    27.             mp = null;
    28.             }
    29.         }
    30.  
    31. }
    32.  
    33. // When calling from activity class
    34. Music.playEffect(context, R.raw.nameOfSong);
    35.  
    36.  
    37.  
     
    VTNotInCtrl likes this.
  6. VTNotInCtrl

    VTNotInCtrl Member
    Thread Starter
    5

    Jul 6, 2011
    8
    0
    5
    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
     
  7. VTNotInCtrl

    VTNotInCtrl Member
    Thread Starter
    5

    Jul 6, 2011
    8
    0
    5
    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 (Text):
    1.  
    2. public class GurusoundbroardActivity extends Activity {
    3.     @Override
    4.     public void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.         setContentView(R.layout.main);
    7.          
    8.         Button yes = (Button) findViewById(R.id.yes);
    9.         yes.setOnClickListener(new View.OnClickListener() {
    10.             public void onClick(View v) {
    11.                 Yes.playEffect(getBaseContext(), R.raw.yes);
    12.             }
    13.         });
    14.        Button no = (Button) findViewById(R.id.no);
    15.        no.setOnClickListener(new View.OnClickListener() {
    16.             public void onClick(View v) {
    17.                 No.playEffect(getBaseContext(), R.raw.no);
    18.             }
    19.         });
    20.    
    21.    
    22.     }}
    23.    
    24.  
    25.  
    The Class that plays sound when called
    Code (Text):
    1.  
    2.  
    3. public class Yes {
    4.     private static MediaPlayer mp = null;
    5.     private static MediaPlayer mpEffect = null;
    6.     public static void play(Context context, int resource) {
    7.         stop(context);
    8.          {
    9.             mp = MediaPlayer.create(context, R.raw.yes);
    10.             mp.start();
    11.             }
    12.     }
    13.    
    14.     public static void playEffect(Context context, int resource) {
    15.          {
    16.             mpEffect = MediaPlayer.create(context, R.raw.yes);
    17.             mpEffect.start();
    18.         }
    19.     }
    20.    
    21.     public static void stop(Context context) {
    22.         if (mp != null) {
    23.             mp.stop();
    24.             mp.release();
    25.             mp = null;
    26.             }
    27.         }
    28.  
    29. }
    30.  
    This class is the second audio file

    Code (Text):
    1.  
    2.  
    3. public class No {
    4.     private static MediaPlayer mp = null;
    5.     private static MediaPlayer mpEffect = null;
    6.     public static void play(Context context, int resource) {
    7.         stop(context);
    8.          {
    9.             mp = MediaPlayer.create(context, R.raw.no);
    10.             mp.start();
    11.             }
    12.     }
    13.    
    14.     public static void playEffect(Context context, int resource) {
    15.          {
    16.             mpEffect = MediaPlayer.create(context, R.raw.no  );
    17.             mpEffect.start();
    18.         }
    19.     }
    20.    
    21.     public static void stop(Context context) {
    22.         if (mp != null) {
    23.             mp.stop();
    24.             mp.release();
    25.             mp = null;
    26.             }
    27.         }
    28.  
    29. }
    30.  
    31.  
    32.  
     
  8. raptiye

    raptiye Member
    16

    Jun 20, 2011
    22
    3
    16
    Your app works on my emulator but I added this lines under manifest file and inside application tag.

    Code (Text):
    1. <activity android:name=".Yes"
    2.               android:label="Play">
    3.         </activity>
    4. <activity android:name=".No"
    5.               android:label="Play">
    6.         </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 (Text):
    1.  
    2. public class Yes {
    3.     private static MediaPlayer mp = null;
    4.    
    5.     public static void play(Context context, int resource) {
    6.         mp = MediaPlayer.create(context, resource);
    7.         mp.start();
    8.     }
    9.    
    10.     public static void stop(Context context) {
    11.         if (mp != null) {
    12.             mp.stop();
    13.             mp.release();
    14.             mp = null;
    15.             }
    16.         }
    17.  
    18. }
    19.  
     
    VTNotInCtrl likes this.
  9. VTNotInCtrl

    VTNotInCtrl Member
    Thread Starter
    5

    Jul 6, 2011
    8
    0
    5
    Thank you! How did I forget the android:label="Play"> tag >.<

    Its all running great now =)
     
  10. nuwud

    nuwud New Member
    5

    Jan 1, 2011
    1
    0
    5
    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.
     

Share This Page

Loading...