こんにちは私はここで1つのゲームをやっています私は何百もの画像をギャラリーに表示する必要があります同時に画像はその画像関連のサウンドを再生する必要があることを考慮しています私は音楽再生にsounpoolを使用しています。私は正しくサウンドを再生しましたしかし、私の問題は、1つの画像を2つの画像に移動するときに、ギャラリーのスクローリングの時間にあります。そのとき、両方の画像が1つの画像、2つの画像に関連する音がその時間を重ねています。その問題を解決する方法は、誰かが私に提案してください。
Gallerynew .class:
public class Gallerynew extends Activity implements OnItemSelectedListener{
public boolean Visibility=true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoundManager.getInstance();
SoundManager.initSounds(this);
SoundManager.playSound(1, 1);
Gallery g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setSpacing(10);
g.setOnItemSelectedListener(this);
}
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = {
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04,
R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06,
R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08,
R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12,
R.drawable.bokstavslottet13
};
public ImageAdapter(Context c) { this.myContext = c; }
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(700, 400));
Integer e = myImageIds.length;
Log.i("","length-------"+e);
return i;
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg2==0){
SoundManager.playSound(1, 1);
}
if(arg2==1)
{
SoundManager.playSound(2, 1);
}
if(arg2==2)
{
SoundManager.playSound(3, 1);
}
if(arg2==3)
{
SoundManager.playSound(4, 1);
}
if(arg2==4)
{
SoundManager.playSound(5, 1);
}
if(arg2==5)
{
SoundManager.playSound(6, 1);
}
if(arg2==6)
{
SoundManager.playSound(7, 1);
}
if(arg2==7)
{
SoundManager.playSound(8, 1);
}
if(arg2==8)
{
SoundManager.playSound(9, 1);
}
if(arg2==9)
{
SoundManager.playSound(10, 1);
}
if(arg2==10)
{
SoundManager.playSound(11, 1);
}
if(arg2==11)
{
SoundManager.playSound(12, 1);
}
if(arg2==12)
{
SoundManager.playSound(13, 1);
}
}
@Override
public void onDestroy()
{
super.onDestroy();
SoundManager.cleanup();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
SoundManager .class:
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()
{
}
/**
* Requests the instance of the Sound Manager and creates it
* if it does not exist.
*
* @return Returns the single instance of the SoundManager
*/
static synchronized public SoundManager getInstance()
{
if (_instance == null)
_instance = new SoundManager();
return _instance;
}
/**
* Initialises the storage for the sounds
*
* @param theContext The Application context
*/
public static 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);
}
/**
* Add a new Sound to the SoundPool
*
* @param Index - The Sound Index for Retrieval
* @param SoundID - The Android ID for the Sound asset.
*/
public static void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
/**
* Loads the various sound assets
* Currently hardcoded but could easily be changed to be flexible.
*/
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.bokstavslottet_3, 1));
mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.bokstavslottet_4, 1));
mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.bokstavslottet_5, 1));
mSoundPoolMap.put(6, mSoundPool.load(mContext, R.raw.bokstavslottet_6, 1));
mSoundPoolMap.put(7, mSoundPool.load(mContext, R.raw.bokstavslottet_7, 1));
mSoundPoolMap.put(8, mSoundPool.load(mContext, R.raw.bokstavslottet_8, 1));
mSoundPoolMap.put(9, mSoundPool.load(mContext, R.raw.bokstavslottet_9, 1));
mSoundPoolMap.put(10, mSoundPool.load(mContext, R.raw.bokstavslottet_10, 1));
mSoundPoolMap.put(11, mSoundPool.load(mContext, R.raw.bokstavslottet_11, 1));
mSoundPoolMap.put(12, mSoundPool.load(mContext, R.raw.bokstavslottet_12, 1));
mSoundPoolMap.put(13, mSoundPool.load(mContext, R.raw.babycooing05, 1));
}
/**
* Plays a Sound
*
* @param index - The Index of the Sound to be played
* @param speed - The Speed to play not, not currently used but included for compatibility
*/
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);
}
/**
* Stop a Sound
* @param index - index of the sound to be stopped
*/
public static void stopSound(int index)
{
mSoundPool.stop(mSoundPoolMap.get(index));
}
public static void cleanup()
{
mSoundPool.release();
mSoundPool = null;
mSoundPoolMap.clear();
mAudioManager.unloadSoundEffects();
_instance = null;
}
}
i tried using mediaplyer its working good.but here each sound i need to set one mediaplyer object.in my app i need play some 100'of sounds.so is there any alternative solutoin please suggest me..