布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ch_2013_4_9playsound"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ch_2013_4_9playsound.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>

Activity:

 package com.example.ch_2013_4_9playsound;

 import java.util.HashMap;

 import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; @SuppressLint("UseSparseArrays")
public class MainActivity extends Activity implements OnClickListener { Button btn_play;
Button btn_play1;
Button btn_stop;
Button btn_stop1; SoundPool sp;
HashMap<Integer, Integer> spMap; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); InitSound();
Init();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//http://www.cnblogs.com/sosoft/ public void Init() {
btn_play = (Button) findViewById(R.id.btn_play);
btn_play1 = (Button) findViewById(R.id.btn_play1);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_stop1 = (Button) findViewById(R.id.btn_stop1); btn_play.setOnClickListener(this);
btn_play1.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_stop1.setOnClickListener(this);
} public void InitSound() {
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
spMap = new HashMap<Integer, Integer>();
spMap.put(1, sp.load(this, R.raw.fire, 1));
spMap.put(2, sp.load(this, R.raw.hit, 1)); } public void playSound(int sound, int number) {
AudioManager am = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumnRatio = volumnCurrent / audioMaxVolumn; sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1f);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btn_play) {
playSound(1, 0);
Toast.makeText(this, "play1", Toast.LENGTH_SHORT).show();
}
if (v == btn_play1) {
playSound(2, 1);
Toast.makeText(this, "play2", Toast.LENGTH_SHORT).show();
}
if (v == btn_stop) {
sp.pause(spMap.get(1));
Toast.makeText(this, "stop", Toast.LENGTH_SHORT).show();
}
if (v == btn_stop1) {
sp.pause(spMap.get(2));
Toast.makeText(this, "stop1", Toast.LENGTH_SHORT).show();
}
} }

Android声音播放实例代码的更多相关文章

  1. 功能强大的图片截取修剪神器:Android SimpleCropView及其实例代码重用简析(转)

    功能强大的图片截取修剪神器:Android SimpleCropView及其实例代码重用简析 SimpleCropView是github上第一个第三方开源的图片修剪截取利器,功能强大,设计良好.我个人 ...

  2. Android音频播放实例

    MediaPlayer: 此类适合播放较大文件,此类文件应该存储在SD卡上,而不是在资源文件里,还有此类每次只能播放一个音频文件. 1.从资源文件中播放 MediaPlayer player = ne ...

  3. android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)

    如有错漏请不吝拍砖指正,转载请注明出处,很感谢 桌面便签软件是android上经常使用软件的一种,比方比較早的Sticky Note,就曾很流行, Sticky Note的介绍能够參见 http:// ...

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

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

  5. Android音频播放之SoundPool

    SoundPool 一.基本概念 在Android应用程序的开发过程中,经常需要播放多媒体文件,也许最先想到的会是MediaPlayer类了,该类提供了播放.暂停.停止及重复播放等功能性方法(该类位于 ...

  6. 【Android】播放音频的几种方式介绍

    接下来笔者介绍一下Android中播放音频的几种方式,android.media包下面包含了Android开发中媒体类,当然笔者不会依次去介绍,下面介绍几个音频播放中常用的类: 1.使用MediaPl ...

  7. Android 音频播放分析笔记

    AudioTrack是Android中比较偏底层的用来播放音频的接口,它主要被用来播放PCM音频数据,和MediaPlayer不同,它不涉及到文件解析和解码等复杂的流程,比较适合通过它来分析Andro ...

  8. android 音乐播放器

    本章以音乐播放器为载体,介绍android开发中,通知模式Notification应用.主要涉及知识点Notification,seekbar,service. 1.功能需求 完善音乐播放器 有播放列 ...

  9. Android VideoView播放视频

    今天介绍一下Android的视频播放控件VideoView,下面介绍一下VideoView的使用步骤: 1.在界面布局中定义VideoView组件,或者在程序中创建VideoView组件. 2.调用V ...

随机推荐

  1. 在iOS中使用ZXing库[转]

    前言 ZXing(Github镜像地址)是一个开源的条码生成和扫描库(开源协议为Apache2.0).它不但支持众多的条码格式,而且有各种语言的实现版本,它支持的语言包括:Java, C++, C#, ...

  2. .Net开发笔记(二十)创建一个需要授权的第三方组件

    在使用需要授权的软件时,注册付费的目标是软件的使用者,我们开发人员平时用到的一些第三方组件也是需要授权付费的,也就是说,付费者是开发人员,并不是系统(使用了该第三方组件)的最终使用者. 以上两者的区别 ...

  3. Eclipse安装ZooKeeper插件

    Eclipse在线安装插件奇慢的解决办法 安装ZooKeeper插件步骤如下:Step 1. 在 Eclipse 菜单打开Help -> Install New Software…Step 2. ...

  4. Android RatingBar 自定义样式

    Android RatingBar 自定义样式 1.先定义Style: <style name="RadingStyle" parent="@android:sty ...

  5. Android开发学习之路-Android Studio开发小技巧

    上一次发过了一个介绍Studio的,这里再发一个补充下. 我们都知道,Android Studio的功能是非常强大的,也是很智能的.如果有人告诉你学Android开发要用命令行,你可以告诉他Andro ...

  6. WCF 安全性 之 Windows

    案例下载 http://download.csdn.net/detail/woxpp/4113172 服务端配置代码 <system.serviceModel> <services& ...

  7. ASP.NET Core - ASP.NET Core MVC 的功能划分

    概述 大型 Web 应用比小型 Web 应用需要更好的组织.在大型应用中,ASP.NET MVC(和 Core MVC)所用的默认组织结构开始成为你的负累.你可以使用两种简单的技术来更新组织方法并及时 ...

  8. 《Qt Quick 4小时入门》学习笔记4

    http://edu.csdn.net/course/detail/1042/14806?auto_start=1 Qt Quick 4小时入门 第七章:处理鼠标与键盘事件 1.处理鼠标事件 鼠标信号 ...

  9. 【WP 8.1开发】How to 图像处理

    在今天的吹牛节目开始之前,先交代一件事: 关于玩WP 8.1开发所使用的VS版本问题.对版本的要求是2013的Update2,这是最低要求,只要是这个版本或以上都可以,而update3,update4 ...

  10. 深入学习jQuery的三种常见动画效果

    × 目录 [1]显隐效果 [2]高度变化 [3]淡入淡出 前面的话 动画效果是jQuery吸引人的地方.通过jQuery的动画方法,能够轻松地为网页添加视觉效果,给用户一种全新的体验.jQuery动画 ...