【问题标题】:How to add sound to an onClick event in an Android application? [duplicate]如何为 Android 应用程序中的 onClick 事件添加声音? [复制]
【发布时间】:2013-10-02 19:00:14
【问题描述】:

我希望能够在用户点击应用程序中的按钮时添加点击声音,关于如何使行为适用于整个应用程序的任何建议? 干杯!

【问题讨论】:

  • 你试过googling it吗?
  • 长音/短音?自定义还是来自 Android?
  • @dymmeh,我在最初的几首热门歌曲中没有发现这一点,我的错。

标签: android audio onclicklistener


【解决方案1】:

在按钮的onClick中,添加View.playSoundEffect(SoundEffectConstants.CLICK)

myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            v.playSoundEffect(SoundEffectConstants.CLICK);
        }
    });

【讨论】:

    【解决方案2】:

    或者您可以使用SoundPool进行更复杂的设置(例如您可以设置左右音量值)

    这只是一个例子:

    private void playSound() {
            // TODO Auto-generated method stub      
            SoundPool pl = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
            // 5 indicates the maximum number of simultaneous streams for this SoundPool object
    
            int waterSound = pl.load(this, R.raw.water_sound_01, 0);
            // is the audio file I have imported in my project as resource
    
            pl.setOnLoadCompleteListener(new OnLoadCompleteListener() {             
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                    // The onLoadComplet method is called when a sound has completed loading.
                    // TODO Auto-generated method stub
                    soundPool.play(sampleId, 1f, 1f, 0, 0, 1);
                    // second and third parameters indicates left and right value (range = 0.0 to 1.0)
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多