java - Soundpool not playing correctly -
i'm creating little game have audio samples drumkit set play when image-buttons touched. working on 1 device not 2 others i've tried on. there in code makes inconsistent across android devices? cannot find reason inconsistency... genius out there solution??
activity code
public class gamemain extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.game_main); // initialise soundpool play drum sample sounds soundpooldrums = new soundpool (8, audiomanager.stream_music, 0); // create hashmap of sounds soundmap = new hashmap<integer, integer>(); } // constants , variables storing sounds drumkit public static final int hihat_sound_id=0; public static final int ride_sound_id=1; public static final int cymbal_sound_id=2; public static final int bass_sound_id=3; public static final int snare_sound_id=4; public static final int tomtoma_sound_id=5; public static final int tomtomb_sound_id=6; public static final int tomtomc_sound_id=7; private soundpool soundpooldrums; private map<integer, integer> soundmap; /* * next section manages playback of drum samples */ // high-hat sample player public void hatone (view view) { soundmap.put(hihat_sound_id, soundpooldrums.load(this, r.raw.drumhata, 1)); soundpooldrums.play(soundmap.get(hihat_sound_id), 1, 1, 1, 0, 1f); } // ride sample player public void hattwo (view view) { soundmap.put(ride_sound_id, soundpooldrums.load(this, r.raw.drumhatc, 1)); soundpooldrums.play(soundmap.get(ride_sound_id), 1, 1, 1, 0, 1f); } // crash sample player public void crash (view view) { soundmap.put(cymbal_sound_id, soundpooldrums.load(this, r.raw.drumcrash, 1)); soundpooldrums.play(soundmap.get(cymbal_sound_id), 1, 1, 1, 0, 1f); } // bass sample player public void kickdrum (view view) { vibrator vib = (vibrator) getsystemservice(context.vibrator_service); vib.vibrate(200); soundmap.put(bass_sound_id, soundpooldrums.load(this, r.raw.drumkick, 1)); soundpooldrums.play(soundmap.get(bass_sound_id), 1, 1, 1, 0, 1f); } // snare sample player public void snaredrum (view view) { vibrator vib = (vibrator) getsystemservice(context.vibrator_service); vib.vibrate(150); soundmap.put(snare_sound_id, soundpooldrums.load(this, r.raw.drumsnare, 1)); soundpooldrums.play(soundmap.get(snare_sound_id), 1, 1, 1, 0, 1f); } // high tom sample player public void tomone (view view) { vibrator vib = (vibrator) getsystemservice(context.vibrator_service); vib.vibrate(120); soundmap.put(tomtoma_sound_id, soundpooldrums.load(this, r.raw.drumtom1, 1)); soundpooldrums.play(soundmap.get(tomtoma_sound_id), 1, 1, 1, 0, 1f); } // mid tom sample player public void tomtwo (view view) { vibrator vib = (vibrator) getsystemservice(context.vibrator_service); vib.vibrate(120); soundmap.put(tomtomb_sound_id, soundpooldrums.load(this, r.raw.drumtom2, 1)); soundpooldrums.play(soundmap.get(tomtomb_sound_id), 1, 1, 1, 0, 1f); } // low tom sample player public void tomthree (view view) { vibrator vib = (vibrator) getsystemservice(context.vibrator_service); vib.vibrate(120); soundmap.put(tomtomc_sound_id, soundpooldrums.load(this, r.raw.drumtom3, 1)); soundpooldrums.play(soundmap.get(tomtomc_sound_id), 1, 1, 1, 0, 1f); } // end of sample players section // handlers buttons // if tactile button pressed user returned home screen @override public void onbackpressed(){ soundpooldrums.release(); soundpooldrums = null; // play sound mediaplayer mmediaplayer = mediaplayer.create(this, r.raw.soundbackbutton) ; mmediaplayer.start(); // short vibration vibrator vib = (vibrator) getsystemservice(context.vibrator_service); vib.vibrate(200); intent s = new intent (this, main.class); startactivity(s); } // handler button public void backbutton (view view) { soundpooldrums.release(); soundpooldrums = null; mediaplayer mmediaplayer = mediaplayer.create(this, r.raw.soundbackbutton) ; mmediaplayer.start(); vibrator vib = (vibrator) getsystemservice(context.vibrator_service); vib.vibrate(200); intent s = new intent (this, main.class); startactivity(s); } }
xml files
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/fabricblacksmr"> <imagebutton android:id="@+id/backbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:onclick="backbutton" android:src="@drawable/buttonhome" android:layout_margin="8dp" android:background="#00000000" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginleft="60dp" android:layout_marginright="20dp" android:layout_margintop="5dp" android:text="@string/gamemaintext" /> <tablerow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <imagebutton android:id="@+id/imagebuttongame1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:onclick="hatone" android:src="@drawable/dhihats" /> <imagebutton android:id="@+id/imagebuttongame2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:src="@drawable/dride" android:onclick="hattwo" android:background="#00000000"/> <imagebutton android:id="@+id/imagebuttongame3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:src="@drawable/dcrash" android:onclick="crash" android:background="#00000000"/> </tablerow> <tablerow android:id="@+id/tablerow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <imagebutton android:id="@+id/imagebuttongame4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onclick="kickdrum" android:src="@drawable/dbassdrum" android:background="#00000000" /> <imagebutton android:id="@+id/imagebuttongame5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/dsnare" android:onclick="snaredrum" android:background="#00000000"/> </tablerow> <tablerow android:id="@+id/tablerow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <imagebutton android:id="@+id/imagebuttongame6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:src="@drawable/dtom1" android:onclick="tomone" android:background="#00000000"/> <imagebutton android:id="@+id/imagebuttongame7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:src="@drawable/dtom2" android:onclick="tomtwo" android:background="#00000000"/> <imagebutton android:id="@+id/imagebuttongame8" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:src="@drawable/dtom3" android:onclick="tomthree" android:background="#00000000"/> </tablerow> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:text="@string/filsdrum"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="10dp" android:layout_marginleft ="120dp" android:text="@string/copyright"/> </tablelayout>
Comments
Post a Comment