Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Playing random sound from OnTouch with SoundManager
    primarykey
    data
    text
    <p>I am wanting a random sound to play whenever the user touches the screen. I have found and used these other posts to help set up my project and get me moving in the right direction.</p> <p><a href="https://stackoverflow.com/questions/13885420/play-random-sound-file-on-button-press-android">Play random sound file on button press - Android</a></p> <p><a href="https://stackoverflow.com/questions/10168826/issue-with-sound-manager-playing-random-files">Issue With Sound Manager Playing Random Files</a></p> <p><a href="https://stackoverflow.com/questions/10168826/issue-with-sound-manager-playing-random-files">Issue With Sound Manager Playing Random Files</a></p> <p><a href="https://stackoverflow.com/questions/10115238/play-a-random-sound-ontouch">Play A Random Sound onTouch</a></p> <p>The problem is that with my current code I am getting a NullPointerException. The LogCat is showing it coming from two spots: </p> <p>In the Main class at </p> <pre><code>int x = r.nextInt(1); </code></pre> <p>and in the SoundManager class at </p> <pre><code>mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); </code></pre> <p>I know this is just something I am overlooking, but can't seem to figure it out. Pretty new to Android and Java developing so that is why. I have included both the Main and SoundManager classes. Any help would be appreciated!</p> <p>MAIN CLASS</p> <pre><code>import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class Main extends Activity implements OnTouchListener { Random r = new Random(); int x = r.nextInt(1); private SoundManager mSoundManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView image = (ImageView) findViewById(R.id.mIll); image.setOnTouchListener(this); mSoundManager = new SoundManager(); mSoundManager.initSounds(getBaseContext()); mSoundManager.addSound(1, R.raw.m1); mSoundManager.addSound(2, R.raw.m2); mSoundManager.addSound(3, R.raw.m3); mSoundManager.addSound(4, R.raw.m4); mSoundManager.addSound(5, R.raw.m5); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { mSoundManager.playSound(x); } break; } return true; } } </code></pre> <p>SOUND MANAGER</p> <pre><code>import java.util.HashMap; import android.content.Context; import android.media.AudioManager; import android.media.SoundPool; public class SoundManager { private SoundPool mSoundPool; private HashMap&lt;Integer, Integer&gt; mSoundPoolMap; private AudioManager mAudioManager; private Context mContext; public static final int maxSounds = 1; public SoundManager() { } public void initSounds(Context theContext) { mContext = theContext; mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); mSoundPoolMap = new HashMap&lt;Integer, Integer&gt;(); mAudioManager = (AudioManager) mContext .getSystemService(Context.AUDIO_SERVICE); } public void addSound(int Index, int SoundID) { mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1)); } public void playSound(int index) { int streamVolume = mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC); mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); } } </code></pre>
    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.
 

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