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


布局文件(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í)有所幫助,也希望大家多多支持腳本之家。
- Android SoundPool實(shí)現(xiàn)簡短小音效
- Android使用SoundPool播放短音效
- Android使用SoundPool實(shí)現(xiàn)播放音效
- Android使用SoundPool播放音效實(shí)例
- Android使用SoundPool實(shí)現(xiàn)播放音頻
- Android中SoundPool的使用步驟實(shí)例
- Android使用SoundPool播放音效
- Android多媒體應(yīng)用使用SoundPool播放音頻
- android使用SoundPool播放音效的方法
- Android編程實(shí)現(xiàn)使用SoundPool播放音樂的方法
相關(guān)文章
Android列表動(dòng)圖展示的實(shí)現(xiàn)策略
這篇文章主要給大家介紹了關(guān)于Android列表動(dòng)圖展示的實(shí)現(xiàn)策略的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理
這篇文章主要為大家介紹了詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
讓Android中RadioGroup不顯示在輸入法上面的辦法
在Android開發(fā)中,發(fā)現(xiàn)一個(gè)問題,打開輸入法導(dǎo)致下面的radioGroup的位置發(fā)生了變化,被頂?shù)搅溯斎敕ǖ纳厦?,那么該如何解決呢?下面來看看。2016-08-08
淺扒Android動(dòng)態(tài)設(shè)置字體大小的示例
本篇文章主要介紹了淺扒Android動(dòng)態(tài)設(shè)置字體大小的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
詳解Java編程中的反射在Android開發(fā)中的應(yīng)用
這篇文章主要介紹了詳解Java編程中的反射在Android開發(fā)中的應(yīng)用,主要來獲取安卓系統(tǒng)的屬性值,需要的朋友可以參考下2015-07-07
詳解Android中實(shí)現(xiàn)Redux方法
本篇文章給大家通過代碼實(shí)例教學(xué)Android中實(shí)現(xiàn)Redux的方法,有需要的朋友跟著參考下吧。2018-01-01
Android?OkHttp庫簡單使用和封裝教程助你快速掌握網(wǎng)絡(luò)請求技能
OkHttp是一個(gè)高效的HTTP客戶端庫,適用于Android和Java應(yīng)用程序。它支持HTTP/2和SPDY協(xié)議,提供了同步和異步請求API、請求和響應(yīng)攔截器、連接池和多路復(fù)用器、緩存支持、GZIP和DEFLATE壓縮等功能,可以大大提高網(wǎng)絡(luò)請求的性能和可擴(kuò)展性2023-04-04

