Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 780011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:00:43+00:00 2026-05-14T20:00:43+00:00

I’ve literally looked everywhere on the net and found very little clarification on how

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:00:43+00:00Added an answer on May 14, 2026 at 8:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.