2014-04-27 52 views
1

如何播放下面代码中放置的同一音乐的多个实例?另外,如果我一次播放两种不同的声音,我应该在哪里放置该代码?在soundpool中播放多个实例

public class SoundManager { 

private SoundPool msoundPool; 
private HashMap<Integer, Integer> mSoundPoolMap; 
private AudioManager maudioManager; 
private Context mContext; 

public SoundManager() 
{ 

} 

public 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); 

} 

public void addSounds(int Index,int soundId) 
{ 
    mSoundPoolMap.put(1, msoundPool.load(mContext, soundId,1)); 
} 

public void playSound(int index) 
{ 
    int streamVolume=maudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    msoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
} 

public void playLoopedSound(int index) 
{ 
    int streamVolume=maudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    msoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1,1f); 
} 
} 

我的主要方法如下:我在onCreate方法称为本

 m=new SoundManager(); 
    m.initSounds(getBaseContext()); 
    m.addSounds(1, R.raw.sound); 
    Button button=(Button)findViewById(R.id.button11); 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      m.playSound(1); 

     } 
    }); 
} 

回答

0

示例代码:

Music music = MusicFactory.createMusicFromAsset(pMusicManager, pContext, pAssetPath); 
Music music2 = MusicFactory.createMusicFromAsset(pMusicManager, pContext, pAssetPath); 

music.play; 
music2.play;