MainActivity.java

package com.example.pengdonglin.soundpool_demo;

import android.annotation.SuppressLint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.SparseIntArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button; import java.util.HashMap; public class MainActivity extends ActionBarActivity implements View.OnClickListener { Button btn1, btn2, btn3;
SoundPool soundpool;
//HashMap<Integer, Integer> soundMap = new HashMap<Integer, Integer>();
SparseIntArray soundMap = new SparseIntArray(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.button);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3); soundpool = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
soundMap.put(1, soundpool.load(this, R.raw.carbon, 1));
soundMap.put(2, soundpool.load(this, R.raw.cesium, 1));
soundMap.put(3, soundpool.load(this, R.raw.fermium, 1)); btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
} @Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button:
soundpool.play(soundMap.get(1), 1, 1, 0, 0, 1);
break;
case R.id.button2:
soundpool.play(soundMap.get(2), 1, 1, 0, 0, 1);
break;
case R.id.button3:
soundpool.play(soundMap.get(3), 1, 1, 0, 0, 1);
break;
}
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"/> </RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pengdonglin.soundpool_demo" > <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

Android简单的利用SoundPool进行播放铃声的实例代码的更多相关文章

  1. Android简单的利用MediaRecorder进行录音的实例代码

    MainActivity.java package com.example.soundrecord_demo; import java.io.IOException; import android.m ...

  2. Android单片机与蓝牙模块通信实例代码

    Android单片机与蓝牙模块通信实例代码 参考路径:http://www.jb51.net/article/83349.htm 啦啦毕业了,毕业前要写毕业设计,需要写一个简单的蓝牙APP进行交互,通 ...

  3. android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动

    android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动            1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...

  4. Android平台的音乐资源管理与播放

    Android平台基于Linux和开放手机联盟(OHA)系统,经过中国移动的创新研发,设计出拥有新颖独特的用户操作界面,增强了浏览器能力和WAP 兼容性,优化了多媒体领域的OpenCORE.浏览器领域 ...

  5. Android简单的ListViewDemo及每个控件的点击事件

    ListView是什么? ListView是一个 数据控件,可以展示从数据库中读取的数据.是.net3.5的新控件. 它比gridview更灵活,而且支持多种模板,支持分页. 文章地址 http:// ...

  6. Android开发教程AnimationDrawable逐帧播放动画

    下面我们一起来看篇Android开发AnimationDrawable控制逐帧播放动画实现过程,希望文章对各位朋友带不一些帮助. 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的 ...

  7. 也发一个自己实现的android简单文件选择器代码。支持多卡,排序

    一个很简单的文件选择器对话框,支持双sd卡,当然前提是要有sd卡..并且实现了排序效果. 只有100多行的代码,基本的思路就是用listview显示目录下的所有子文件,再判断是文件还是目录. 利用Co ...

  8. SoundPool 音频播放 详解 示例

    简介 如果应用程序经常播放密集.急促而又短暂的音效(如游戏音效)那么使用MediaPlayer显得有些不太适合了.因为MediaPlayer存在如下缺点: 1) 延时时间较长,且资源占用率高. 2) ...

  9. Android应用--简、美音乐播放器增加音量控制

    Android应用--简.美音乐播放器增加音量控制 2013年6月26日简.美音乐播放器继续完善中.. 题外话:上一篇博客是在6月11号发的,那篇博客似乎有点问题,可能是因为代码结构有点乱的原因,很难 ...

随机推荐

  1. Android记事本10

    昨天: 从Activity中返回数据. 请求码和结果码的作用. 今天: Activity的启动模式. 遇到的问题: 无.

  2. 【POJ3294】 Life Forms(SA)

    ...又是TLE,对于单组数据肯定TLE不了,问题是多组的时候就呵呵了... 按height分组去搞,然后判一下是否不属于同一个串... ; var x,y,rank,sa,c,col,h,rec:. ...

  3. POJ 3348 Cows | 凸包模板题

    题目: 给几个点,用绳子圈出最大的面积养牛,输出最大面积/50 题解: Graham凸包算法的模板题 下面给出做法 1.选出x坐标最小(相同情况y最小)的点作为极点(显然他一定在凸包上) 2.其他点进 ...

  4. 洛谷树剖模板题 P3384 | 树链剖分

    原题链接 对于以u为根的子树,后代节点的dfn显然比他的dfn大,我们可以记录一下回溯到u的dfn,显然这两个dfn构成了一个连续区间,代表u及u的子树 剩下的就和树剖一样了 #include< ...

  5. BZOJ4032 [HEOI2015]最短不公共子串 【后缀自动机 + 序列自动机 + dp】

    题目链接 BZOJ4032 题解 首先膜\(hb\) 空手切神题 一问\(hash\),二问枚举 三问\(trie\)树,四问\(dp\) 南二巨佬神\(hb\) 空手吊打自动机 \(orz orz ...

  6. 牛客 2018NOIP 模你赛2 T2 分糖果 解题报告

    分糖果 链接:https://www.nowcoder.com/acm/contest/173/B 来源:牛客网 题目描述 \(N\) 个小朋友围成一圈,你有无穷个糖果,想把其中一些分给他们. 从某个 ...

  7. 公共文件js加载

    头部:例如 <header id="header" class="clearfix"> <a class="col-xs-9&quo ...

  8. url为什么要编码及php中的中文字符urlencode基本原理

    首先了解以下中文字符在使用urlencode的时候运用的基本原理: urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%. 此字符串中除了 -_. 之外的所 ...

  9. Codeforces 932.B Recursive Queries

    B. Recursive Queries time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. php 字符串重要函数

    1.chop() 从字符串右端移除字符 chop(string,charlist) $str="hello world~"; echo chop($str,"ld~&qu ...