Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In semi psuedo code:</p> <pre><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]); </code></pre> <p>If all the sound files you have are small and you want low latency consider using SoundPool instead of MediaPlayer.</p> <p><strong>EDIT:</strong> 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.</p> <pre><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; 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; } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload