Android------Button 添加聲音效果(兩種方式)

來源:互聯網
上載者:User

標籤:cte   底部導航   數字   play   底部導覽列   lis   build   new   pil   

我在先前的案例《Android 的底部導覽列 BottomNavigationBar》中添加以底部

的4個按鈕切換添加聲音

下來看看案例

             

 

使用添加依賴

 

compile ‘com.ashokvarma.android:bottom-navigation-bar:1.3.0‘

 

方式1:

private void PlayMusic(int MusicId) {        music = MediaPlayer.create(this, MusicId);        music.start();    }

 

方式2:

 @SuppressLint("NewApi")    private void initSound() {        soundPool = new SoundPool.Builder().build();        soundID = soundPool.load(this, R.raw.qipao, 1);    }    private void playSound() {        soundPool.play(                soundID,                0.9f,   //左耳道音量【0~1】                0.9f,   //右耳道音量【0~1】                0,     //播放優先順序【0表示最低優先順序】                0,     //迴圈模式【0表示迴圈一次,-1表示一直迴圈,其他表示數字+1表示當前數字對應的迴圈次數】                1     //播放速度【1是正常,範圍從0~2】        );    }

 

使用只需在mBottomNavigationBar的onTabSelected方法中調用:

 mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){            @Override            public void onTabSelected(int position) {                //PlayMusic(R.raw.qipao);//方式1                playSound();//方式二                showFragment(position);            }            @Override            public void onTabUnselected(int position) {            }            @Override            public void onTabReselected(int position) {            }        });

 

 源碼傳送門

Android------Button 添加聲音效果(兩種方式)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.