Android使用SoundPool播放音效實例
更新時間:2019年11月29日 10:23:11 作者:安東尼肉店
這篇文章主要為大家詳細介紹了Android使用SoundPool播放音效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
使用場景
SoundPool一般用來 播放密集,急促而又短暫的音效,比如特技音效:Duang~,游戲用得較多,你也可以為你的 APP添加上這個音效,比如酷狗音樂進去的時候播放"哈嘍,酷狗" 是不是提起了對于SoundPool的興趣了呢
ok,廢話不多說 詳細的參數(shù)解釋請看注釋
public class SoundPlayer extends AppCompatActivity { private SoundPool mSoundPool; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sound_player); initState(); } private void initState() { //sdk版本21是SoundPool 的一個分水嶺 if (Build.VERSION.SDK_INT >= 21) { SoundPool.Builder builder = new SoundPool.Builder(); //傳入最多播放音頻數(shù)量, builder.setMaxStreams(1); //AudioAttributes是一個封裝音頻各種屬性的方法 AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder(); //設置音頻流的合適的屬性 attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC); //加載一個AudioAttributes builder.setAudioAttributes(attrBuilder.build()); mSoundPool = builder.build(); } else { /** * 第一個參數(shù):int maxStreams:SoundPool對象的最大并發(fā)流數(shù) * 第二個參數(shù):int streamType:AudioManager中描述的音頻流類型 *第三個參數(shù):int srcQuality:采樣率轉換器的質量。 目前沒有效果。 使用0作為默認值。 */ mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); } //可以通過四種途徑來記載一個音頻資源: //context:上下文 //resId:資源id // priority:沒什么用的一個參數(shù),建議設置為1,保持和未來的兼容性 //path:文件路徑 // FileDescriptor:貌似是流吧,這個我也不知道 //:從asset目錄讀取某個資源文件,用法: AssetFileDescriptor descriptor = assetManager.openFd("biaobiao.mp3"); //1.通過一個AssetFileDescriptor對象 //int load(AssetFileDescriptor afd, int priority) //2.通過一個資源ID //int load(Context context, int resId, int priority) //3.通過指定的路徑加載 //int load(String path, int priority) //4.通過FileDescriptor加載 //int load(FileDescriptor fd, long offset, long length, int priority) //聲音ID 加載音頻資源,這里用的是第二種,第三個參數(shù)為priority,聲音的優(yōu)先級*API中指出,priority參數(shù)目前沒有效果,建議設置為1。 final int voiceId = mSoundPool.load(this, R.raw.duang, 1); //異步需要等待加載完成,音頻才能播放成功 mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { if (status == 0) { //第一個參數(shù)soundID //第二個參數(shù)leftVolume為左側音量值(范圍= 0.0到1.0) //第三個參數(shù)rightVolume為右的音量值(范圍= 0.0到1.0) //第四個參數(shù)priority 為流的優(yōu)先級,值越大優(yōu)先級高,影響當同時播放數(shù)量超出了最大支持數(shù)時SoundPool對該流的處理 //第五個參數(shù)loop 為音頻重復播放次數(shù),0為值播放一次,-1為無限循環(huán),其他值為播放loop+1次 //第六個參數(shù) rate為播放的速率,范圍0.5-2.0(0.5為一半速率,1.0為正常速率,2.0為兩倍速率) soundPool.play(voiceId, 1, 1, 1, 0, 1); } } }); } }
非常簡單的使用。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android編程動態(tài)修改RelativeLayout寬高的方法
這篇文章主要介紹了Android編程動態(tài)修改RelativeLayout寬高的方法,涉及Android動態(tài)布局的相關技巧,需要的朋友可以參考下2015-12-12Android使用Intent.ACTION_SEND分享圖片和文字內容的示例代碼
這篇文章主要介紹了Android使用Intent.ACTION_SEND分享圖片和文字內容的示例代碼的實例代碼,具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧2018-05-05使用Android WebSocket實現(xiàn)即時通訊功能
即時通訊(Instant Messaging)最重要的毫無疑問就是即時,不能有明顯的延遲,要實現(xiàn)IM的功能其實并不難,目前有很多第三方,比如極光的JMessage,都比較容易實現(xiàn)。本文通過實例代碼給大家分享Android WebSocket實現(xiàn)即時通訊功能,一起看看吧2019-10-10