2014-01-11 162 views
1

我有2 SoundPool实例,如果我运行我的应用程序,soundPool2运行与soundPool1相同的声音,我不知道为什么这会发挥相同的声音。任何人都可以帮我找到问题吗?SoundPool没有播放?

public class MainActivity extends Activity implements OnTouchListener { 
private SoundPool soundPool; 
private SoundPool soundPool2; 
private int soundID; 
private int soundID2; 
boolean loaded = true; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    View view = findViewById(R.id.button1); 
    view.setOnTouchListener(this); 

    // Set the hardware buttons to control the music 
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    // Load the sound 
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() 
    { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, 
       int status) { 
      loaded = true; 
     } 
    }); 

    soundID = soundPool.load(this, R.raw.sound1, 1); 

    View view2 = findViewById(R.id.button2); 
    view2.setOnTouchListener(this); 
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    soundPool2.setOnLoadCompleteListener(new OnLoadCompleteListener() 
    { 
     @Override 
     public void onLoadComplete(SoundPool soundPool2, int sampleId, 
       int status) { 
      loaded = true; 


     } 
    }); 


    soundID2 = soundPool.load(this, R.raw.bird, 1); 

} 

public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     // Getting the user sound settings 
     AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
     float actualVolume = (float) audioManager 
       .getStreamVolume(AudioManager.STREAM_MUSIC); 
     float maxVolume = (float) audioManager 
       .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     float volume = actualVolume/maxVolume; 
     // Is the sound loaded already? 
     if (loaded) { 
      soundPool.play(soundID, volume, volume, 1, 0, 1f); 
      Log.e("Test", "Played "); 
     } 
    } 
    return false; 
} 

public boolean onTouch2(View v, MotionEvent event) { 
if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    // Getting the user sound settings 
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
    float actualVolume = (float) audioManager 
      .getStreamVolume(AudioManager.STREAM_MUSIC); 
    float maxVolume = (float) audioManager 
      .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
    float volume = actualVolume/maxVolume; 
    // Is the sound loaded already? 
    if (loaded) { 
     soundPool2.play(soundID2, volume, volume, 1, 0, 1f); 
     Log.e("Test", "Played sound"); 
    } 
} 
return false; 
}} 

回答

4

首先,你只需要一个SoundPool而不是两个实例。

此外,您的onTouchonTouch2对我来说没有多大意义,这里也可能存在问题,所以我重新编写了该部分。

请尝试下面的代码。

public class MainActivity extends Activity implements OnTouchListener { 
    private SoundPool mSoundPool; 
    private int mSoundID; 
    private int mSoundID2; 
    private boolean mSoundLoaded = false; 
    private float mVolume = 0f; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     View view = findViewById(R.id.button1); 
     view.setOnTouchListener(this); 
     View view2 = findViewById(R.id.button2); 
     view2.setOnTouchListener(this); 

     // Set the hardware buttons to control the music 
     this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 

     // Load the sound 
     mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
     mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
      @Override 
      public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
       if (sampleId == R.raw.bird) { 
        mSoundLoaded = true; 
       } 
      } 
     }); 

     mSoundID = mSoundPool.load(this, R.raw.sound1, 1); 
     mSoundID2 = mSoundPool.load(this, R.raw.bird, 1); 

     // Getting the user sound settings 
     AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
     float actualVolume = (float) audioManager 
       .getStreamVolume(AudioManager.STREAM_MUSIC); 
     float maxVolume = (float) audioManager 
       .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     mVolume = actualVolume/maxVolume; 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      // Is the sound loaded already? 
      if (mSoundLoaded) { 
       if (v.getId() == R.id.button1) { 
        playSound(mSoundID); 
        Log.e("Test", "Played mSoundID"); 
       } else if (v.getId() == R.id.button2) { 
        playSound(mSoundID2); 
        Log.e("Test", "Played mSoundID2"); 
       } 
      } 
     } 
     return false; 
    } 

    private void playSound(int soundId) { 
     mSoundPool.play(soundId, mVolume, mVolume, 1, 0, 1f); 
    } 
} 

注意:可能有错误,它是未经测试的代码。

+0

非常感谢老师,它的作品,我测试过它,完美! :),并运行找到..再次感谢,即时新手我必须学习更多。他@Tigger –

+0

高兴得到了帮助。 – Tigger

+0

@Tigger嘿触发我需要你的声音池的帮助。这是一个简单的代码,请邀请我去聊天室。谢谢! –