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;
}
}