Android利用SoundPool實現(xiàn)音樂池
本文實例為大家分享了Android利用SoundPool實現(xiàn)音樂池的具體代碼,供大家參考,具體內(nèi)容如下
運行效果圖如下:


布局文件(activity_sound_pool.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_sound_pool"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.g150825_android26.SoundPoolActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音效雞"
android:onClick="playKFC"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音效TWO"
android:onClick="playTWO"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音效Three"
android:onClick="playThree"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音效Four"
android:onClick="playFour"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音效狗"
android:onClick="playDog"
/>
</LinearLayout>
Java代碼
package com.example.g150825_android26;
import android.app.AlarmManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class SoundPoolActivity extends AppCompatActivity {
private SoundPool soundPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_pool);
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int i, int i1) {
soundPool.play(i,1,1,1,-1,1);
}
});
}
public void playKFC(View view){
soundPool.load(this,R.raw.rooster,1);
}
public void playTWO(View view){
soundPool.load(this,R.raw.chimp,1);
}
public void playThree(View view){
soundPool.load(this,R.raw.crickets,1);
}
public void playFour(View view){
soundPool.load(this,R.raw.roar,1);
}
public void playDog(View view){
soundPool.load(this,R.raw.dogbark,1);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (soundPool!=null){
soundPool.release();
soundPool=null;
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解flutter如何實現(xiàn)局部導(dǎo)航管理
這篇文章主要為大家介紹了詳解flutter如何實現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
讓Android中RadioGroup不顯示在輸入法上面的辦法
在Android開發(fā)中,發(fā)現(xiàn)一個問題,打開輸入法導(dǎo)致下面的radioGroup的位置發(fā)生了變化,被頂?shù)搅溯斎敕ǖ纳厦?,那么該如何解決呢?下面來看看。2016-08-08
淺扒Android動態(tài)設(shè)置字體大小的示例
本篇文章主要介紹了淺扒Android動態(tài)設(shè)置字體大小的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
詳解Java編程中的反射在Android開發(fā)中的應(yīng)用
這篇文章主要介紹了詳解Java編程中的反射在Android開發(fā)中的應(yīng)用,主要來獲取安卓系統(tǒng)的屬性值,需要的朋友可以參考下2015-07-07
Android?OkHttp庫簡單使用和封裝教程助你快速掌握網(wǎng)絡(luò)請求技能
OkHttp是一個高效的HTTP客戶端庫,適用于Android和Java應(yīng)用程序。它支持HTTP/2和SPDY協(xié)議,提供了同步和異步請求API、請求和響應(yīng)攔截器、連接池和多路復(fù)用器、緩存支持、GZIP和DEFLATE壓縮等功能,可以大大提高網(wǎng)絡(luò)請求的性能和可擴展性2023-04-04

