views:

241

answers:

3

I've literally looked everywhere on the net and found very little clarification on how to do this. Pretty much, I have 8 sound files laid out in an array.xml file and I need to play a randomly chosen file ONCE per or onClick or onShake. First off, what technique should I use to achieve this? ARRAY->RANDOM-

STRING->PLAY? RANDOM INT->PLAY? RANDOM INT->STRING->PLAY? Any kind

of direction will help greatly cause I'm almost 3 weeks worth of research into this.

*NOTE: MediaPlayer mp = MediaPlayer.create(JelloMan.this, R.raw.sound) ...is what I'm stuck on being you can't replace the "R.raw" part with a string...

Here is the whole code.

package com.cyphasignals.jelloman;

import java.util.Random;

import android.app.Activity;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class JelloMan extends Activity {
/** Called when the activity is first created. */

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        

    private final int NUM_SOUND_FILES = 3;
    //Modifier invalid here 
    private int mfile[] = new mfile[NUM_SOUND_FILES];
    //Modifier invalid here and SECOND "mfile" is wanting to create a class
    private Random rnd = new Random(3);
    //Modifier invalid here 
        mfile[0] = R.raw.sound1;
        mfile[1] = R.raw.sound2;
        mfile[2] = R.raw.sound3;
    int sndToPlay = rnd.nextInt(NUM_SOUND_FILES);

   ShakeListener MyShake = new ShakeListener((SensorManager) 
getSystemService(SENSOR_SERVICE)); 
    MyShake.setForceThreshHold(4.0); 
    MyShake.setOnShakeListener(new ShakeListener.OnShakeListener() {

        MediaPlayer mp = MediaPlayer.create(JelloMan.this, mfile[sndToPlay]);
        //[sndToPlay] wants me to change the modifier
         public void onShake() {
             mp.seekTo(0);
             mp.start();                 
         } 
    });
    ImageButton mouthbutton = (ImageButton)findViewById(R.id.billmouth);
    mouthbutton.setOnClickListener(new OnClickListener() {

        MediaPlayer mp = MediaPlayer.create(JelloMan.this, 
                mfile[sndToPlay]);
            //[sndToPlay] wants me to change the modifier
        public void onClick(View v) {
            mp.seekTo(0);
            mp.start(); 
            }
    });

} 
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        finish();
        return true;
    };
    return false;
}

}

+1  A: 

well.. based on what I understood out of your question....

R.raw.sound is integer so, of course, you can't replace it with a string value.. why don't you create an int array and put each of the sound files in it... such as below...

file[0] = R.raw.sound_0 file[1] = R.raw.sound_1 : : : file[n] = R.raw.sound_n

and now, all you have to do is to get a random value between 0 to n....

MediaPlayer mp = MediaPlayer.create(JelloMan.this, file[random_value]);

optimystery
Excuse my noobness in Java but I didn't even know that " file[random_value]" existed...THANK YOU! Now I'm trying to fit the example below to generate a random value now while retaining the "private" modifiers :P
Aaron
+1  A: 

In semi psuedo code:

private final int NUM_SOUND_FILES = 3;

private int mSndFiles[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();   //import java.util.Random for this

mSndFiles[0] = R.raw.sound1;
mSndFiles[1] = R.raw.sound2;
mSndFiles[2] = R.raw.sound3;

int sndToPlay = rnd.nextInt(NUM_SOUND_FILES);

MediaPlayer mp = MediaPlayer.create(JelloMan.this, mSndFiles[sndToPlay]);

If all the sound files you have are small and you want low latency consider using SoundPool instead of MediaPlayer.

EDIT: I didn't mean for you to just copy and paste the code above into your app, i assumed you'd place things in the right places. Anyway, try this, note my comments in the code. I didn't test this and assume you also have defined the "ShakeListener" class somewhere else, but this should work.

package com.cyphasignals.jelloman;

import java.util.Random;
import android.app.Activity;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.cyphasignals.R;

public class JelloMan extends Activity {

    private final int NUM_SOUND_FILES = 3;  //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE*****

    private int mfile[] = new int[NUM_SOUND_FILES];
    private Random rnd = new Random();
    private MediaPlayer mp;

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        mfile[0] = R.raw.sound1;  //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
        mfile[1] = R.raw.sound2;  //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
        mfile[2] = R.raw.sound3;

       ShakeListener MyShake = new ShakeListener((SensorManager.getSystemService(SENSOR_SERVICE)); 
       MyShake.setForceThreshHold(4.0); 
       MyShake.setOnShakeListener(new ShakeListener.OnShakeListener() {
          public void onShake() {
             mp = MediaPlayer.create(JelloMan.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
             mp.seekTo(0);
             mp.start();                 
       }});

       ImageButton mouthbutton = (ImageButton)findViewById(R.id.billmouth);
       mouthbutton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
             mp = MediaPlayer.create(JelloMan.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
             mp.seekTo(0);
             mp.start(); 
       }});

   } 
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)  {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
         finish();
         return true;
      }
   return false;
   }
}

Structurally you need to think about how this work if someone continuously shakes the device. As it is right now it'll constantly skip back to the beginning of the sound.

Mark
Everything seems like it will work except the "private Random rnd = new Random();" line and some others are now getting the syntax error...any ideas? t5hanks for the help big time!!!
Aaron
did you import java.util.Random? You can just push ctrl-shift-O in eclipse and it will fix your imports.
Mark
Yes I did...also, been reworking it it numerous times can't get it to randomize onshake or onclick...want my full code? Also, I tried to Up both of you guys but I have to have 15 rep first I guess...so as I get it, I will. :)
Aaron
yes, it would be easier to figure out if you edited the question to include the code
Mark
added edit to include whole java code now. I'm having issues with the modifiers not working.
Aaron
@aaron check my edit
Mark
THANK YOU!!! IT WORKS!!! I OWE YOU A DONATION! I've really new to Android development. Now I can finally move forward with this project!
Aaron
A: 

Store the sounds like this:

    private static final int[] SOUNDS = new int[] {
    R.drawable.abort,  R.drawable.aliens, R.drawable.annoying, R.drawable.better, 
    R.drawable.birth_control, R.drawable.bitchin, R.drawable.book_em, R.drawable.clean_up, 
    R.drawable.come_on, R.drawable.cry, R.drawable.damn_it, R.drawable.damn, R.drawable.game_over,
    R.drawable.good, R.drawable.gotta_hurt, R.drawable.hail, R.drawable.holy_cow, R.drawable.holy_sh,
    R.drawable.let_god, R.drawable.name, R.drawable.play, R.drawable.terminated,
    R.drawable.this_sux, R.drawable.ugly, R.drawable.wasted, R.drawable.you_suck,
    R.drawable.you_suck2, R.drawable.you_will_die
};

and you can play it:

 int sndToPlay = rnd.nextInt(SOUNDS.length);
           MediaPlayer mp = MediaPlayer.create(main.this, SOUNDS[sndToPlay] );
         mp.seekTo(0);
         mp.start();  

that's work for me

Balázs Lakat