Android利用SoundPool实现音乐池
程序员文章站
2022-03-02 21:03:19
本文实例为大家分享了android利用soundpool实现音乐池的具体代码,供大家参考,具体内容如下运行效果图如下:布局文件(activity_sound_pool.xml)
本文实例为大家分享了android利用soundpool实现音乐池的具体代码,供大家参考,具体内容如下
运行效果图如下:
布局文件(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; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 女皇武则天野史(浅析武则天的风流史)