Play sound randomly from an array?

All your problems with Audio, Video and Images.

Play sound randomly from an array?

Postby Cypha Signals » Mon May 10, 2010 6:50 pm

So I have a array of around 8 files in array.clm specified like this :

<?xml version="1.0" encoding="utf-8"?>
<integer-array name="myArray">

and basically I want to play a different sound randomly based on an onClick or onShake event...I having the Listeners setup and working and I'm using MediaPlayer mp.start() to play them successfully (which seems it will only take one int at a time...The problem is

1. I'm having a bit of trouble converting the whole array to be specified by one string such as:
String soundArrayString = getArray(R.array.myArray); (I know this will not work, just trying to paint a picture :P)

2. Getting one of the items from the array randomly as the randomocity is continuously cycling through (if necessary)...

3. Using that randomly called item in the array to play the corresponding sound (String to int technique I just can't seem to understand)

Here's my code so far without any random or array codes (I've tried a lot and nothing is working):

ImageButton testbutton = (ImageButton)findViewById(;
testbutton.setOnClickListener(new OnClickListener() {
MediaPlayer mp = MediaPlayer.create(Project1.this,
public void onClick(View v) {

Does this even seem like I'm heading the right direction for what I'm trying to accomplish?
I am the pop-locking app
Cypha Signals
Posts: 3
Joined: Mon May 03, 2010 10:43 pm
Location: California


Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 7 guests