0

嗨,我在这里做一个游戏,我需要在画廊中显示 100 张图像,同时在这个图像中,我需要播放与图像相关的声音,我使用 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..
4

1 回答 1

0

如果您使用的是媒体播放器,则可以使用 mp.isplaying() 轻松停止它,然后执行一些操作。

但即使我有同样的问题,我也可以建议你一件事:

在那里添加一个标志,然后你可以检查它 flag=true 做其他事情做某事

Ex: sound1 = mSoundPool.load(this, R.raw.abc, 1);
    mSoundPool.play(sound1, 1, 1, 1, time - 1, 1);
    loaded = true;

现在对于旗帜你可以这样做

if(loaded == true)
{
    mSoundPool.stop(sound1);            
}

你可以试试这个。

于 2012-04-27T09:37:06.633 回答