For Playing different sounds I have made a class SoundManager using
soundpool now I can play a wanted sound on button click by my
activities.But I want a back ground sound which will play through out
my whole game ,from one activity to other.So how can I do it using
SoundPool and where I have to write code to play it in my Activities.
Actually I am new to Android if is there any other better way for
playing background sounds through out all activities please tell me

Here is my SoundManger class Code.

package com.Tutorial.Sound;

import java.util.HashMap;

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


public class SoundManager {

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

    private SoundManager()
    {
    }


    static synchronized public SoundManager getInstance()
    {
        if (_instance == null)
          _instance = new SoundManager();
        return _instance;
     }


    public static  void initSounds(Context theContext)
    {
         mContext = theContext;
         mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
         mSoundPoolMap = new HashMap<Integer, Integer>();
         mAudioManager =
(AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
    }


    public static void addSound(int Index,int SoundID)
    {
        mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID,
1));
    }


    public static void loadSounds()
    {
        mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.starwars,
1));
        mSoundPoolMap.put(2, mSoundPool.load(mContext,
R.raw.terminator, 1));
        mSoundPoolMap.put(3, mSoundPool.load(mContext,
R.raw.soundfcgo, 1));
        mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.soundfc1,
1));
        mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.soundfc2,
1));
    }


    public static void playSound(int index,float speed)
    {
             float streamVolume =
mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
             streamVolume = streamVolume /
mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
             mSoundPool.play(mSoundPoolMap.get(index), streamVolume,
streamVolume, 1, 0, speed);
    }


    public static void stopSound(int index)
    {
        mSoundPool.stop(mSoundPoolMap.get(index));
        mSoundPool.pause(mSoundPoolMap.get(index));
    }

    public static void cleanup()
    {
        mSoundPool.release();
        mSoundPool = null;
        mSoundPoolMap.clear();
        mAudioManager.unloadSoundEffects();
        _instance = null;

    }
}

If you have any example on this back ground sound topic , it will be
great full to me

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to