Having SERIOUS trouble trying to play sound by string labels

All your problems with Audio, Video and Images.

Having SERIOUS trouble trying to play sound by string labels

Postby Cypha Signals » Wed May 05, 2010 9:17 am

Basically, I have this externalized array setup and I followed a fellow board members instruction on setting up the usage of that array.

The problem is I have no clue as to how android wants me to call that sound as a string rather than a int...

Array of 7 sounds -> dropped into random generation -> String is created -> play "string"...here's where I'm stuck

My java so far...

package com.cyphasignals.jelloman;

import com.cyphasignals.jelloman.SoundManager;
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class JelloMan extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
private String[] myString;
private static final Random rgenerator = new Random();
public void onCreate(Bundle savedInstanceState) {

mSoundManager = new SoundManager();
mSoundManager.addSound(1, q);

ShakeListener MyShake = new ShakeListener((SensorManager) getSystemService(SENSOR_SERVICE));
MyShake.setOnShakeListener(new ShakeListener.OnShakeListener() {
public void onShake() {
ImageButton mouthbutton = (ImageButton)findViewById(R.id.billmouth);
mouthbutton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {



Outsourced "SoundManager.java"
package com.cyphasignals.jelloman;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;

public SoundManager()


public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
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(String q) {

int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(q), streamVolume, streamVolume, 1, 0, 1f);
public void stopSound(int Index, int SoundID) {


*If anyone knows a simpler way, it would be truly appreciated :)
I am the pop-locking app
Cypha Signals
Posts: 3
Joined: Mon May 03, 2010 10:43 pm
Location: California


Postby nstegg » Tue May 11, 2010 11:00 pm

Ignoring all else for a moment, is it really needed to play a sound via a String? I'm of course not sure of what you are implementing, but if its for the sake of being able to request a sound in your code via a nice readable tag, you could always use a list of statically defined integer tags as the keys:

public static int CRASH= 0;
public static int BEEP = 1;

That way in code, you could request to play a sound with the pre-defined tags as if they were strings. By the same token, you could always use String as the key type in the hashmap.

I apologize if this doesn't address your question / work with how you have your sound manager set up, thought I would post a quick response before I get another customer at work. ><
Posts: 6
Joined: Fri Oct 02, 2009 11:56 pm


Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 9 guests