Back Ground Sound in Android -
for playing different sounds have made class soundmanager using soundpool can play wanted sound on button click activities.but want ground sound play through out whole game ,from 1 activity other.so how can using soundpool , have write code play in activities. new android if there other better way playing background sounds through out activities please tell me
here 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 have example on ground sound topic , great full me
take @ media player class resolve problem. gives possibility play music , sounds in background, whatever activity in foreground. hope helps.
Comments
Post a Comment