`
yidongkaifa
  • 浏览: 4038301 次
文章分类
社区版块
存档分类
最新评论

【Android开发学习03】短时间的音效播放实现SoundPool

 
阅读更多
音效: 快,短。(持续时间小于7秒


实现技术: android.media.SoundPool实现 (管理和播放应用程序的声音资源,直接加载到内存)。


一.基础知识:

1. 创建一个SoundPool :
我们先看看SoundPool函数的定义,如下:

  public SoundPool(
	int maxStream, // 同时播放的流的最大数量
	int streamType,// 流的类型,一般为STREAM_MUSIC
	int srcQuality // 采样率转化质量,当前无效果,使用0作为默认值
	) 
eg.
  SoundPool soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
  创建了一个最多支持3个流同时播放的,类型标记为音乐的SoundPool。


2. 加载一个音频文件:
以下方法,取其一即可:

	int  load(Context context, int resId, int priority)  //从APK资源载入
  	int  load(FileDescriptor fd, long offset, long length, int priority)  //从FileDescriptor对象载入
  	int  load(AssetFileDescriptor afd, int priority)  //从Asset对象载入
  	int  load(String path, int priority)  //从完整文件路径名载入 
我目前使用的是第一种,从APK资源载入:
	int  load(
		Context context,	// 应用程序的上下文,即当前的Activity,可理解为谁来调用这个方法。
		int resId, 		// 资源的ID
		int priority	   	// 优先级,我们设置为1即可。
	)  


3. 播放音效:

	int play(
		int soundID, 		// 播放的音乐ID
		float leftVolume,	// 左声道音量
		float rightVolume, 	// 右声道音量
		int priority, 	// 优先级,0为最低
		int loop, 		// 循环次数,0为不循环,-1为永远循环
		float rate		// 回放速度,该值在0.5-2.0之间,1为正常速度。
	)


4. 暂停音效:

	void pause(int streamID)	// 参数为 音效的ID。


二.编程实现:

1. 界面编辑(res\layout\main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<Button 
		android:text="播放音效1" 
		android:id="@+id/Button01" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</Button>										<!-- 添加一个Button控件 -->
	<Button 
		android:text="播放音效2" 
		android:id="@+id/Button02" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</Button>										<!-- 添加一个Button控件 -->
	<Button 
		android:text="暂停音效1" 
		android:id="@+id/Button1Pause" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</Button>										<!-- 添加一个Button控件 -->
	<Button 
		android:text="暂停音效2" 
		android:id="@+id/Button2Pause" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</Button>										<!-- 添加一个Button控件 -->
</LinearLayout>

LinearLayout 定义了一个 vertical(垂直方向) 上的线性布局。

在这个布局之内,填充四个按钮控件,其text, id, layout_width, 和layout_height 参数在里面有填充说明。

界面布局效果如下:


2. 代码编辑(\src\wyf\zcl\MyActivity.java):

package wyf.zcl;

import java.util.HashMap;				// 引入相关包

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyActivity extends Activity {
    /** Called when the activity is first created. */
	SoundPool sp;							//得到一个声音池引用
	HashMap<Integer,Integer> spMap;			//得到一个map的引用
	Button b1;								//声音播放控制按钮
	Button b1Pause;							//声音暂停控制按钮
    Button b2;								//声音播放控制按钮
    Button b2Pause;							//声音暂停控制按钮
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initSoundPool();					   	//初始化声音池
        b1=(Button)findViewById(R.id.Button01);	//声音播放控制按钮实例化
        b2=(Button)findViewById(R.id.Button02);	//声音播放控制按钮实例化
        b1Pause=(Button)findViewById(R.id.Button1Pause);
        b2Pause=(Button)findViewById(R.id.Button2Pause);
        b1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				playSound(1,1);		//播放第一首音效,循环一遍
				Toast.makeText(MyActivity.this, "播放音效1", Toast.LENGTH_SHORT).show();
		}});
        b1Pause.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				sp.pause(spMap.get(1));
				Toast.makeText(MyActivity.this, "暂停音效1", Toast.LENGTH_SHORT).show();
		}});
        b2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				playSound(2,1);		//播放第二首音效,循环一遍
			Toast.makeText(MyActivity.this, "播放音效2", Toast.LENGTH_SHORT).show();
		}});
        b2Pause.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				sp.pause(spMap.get(2));
				Toast.makeText(MyActivity.this, "暂停音效2", Toast.LENGTH_SHORT).show();
		}});
    }
    public void initSoundPool(){			//初始化声音池
    	sp=new SoundPool(
    			5, 				//maxStreams参数,该参数为设置同时能够播放多少音效
    			AudioManager.STREAM_MUSIC,	//streamType参数,该参数设置音频类型,在游戏中通常设置为:STREAM_MUSIC
    			0				//srcQuality参数,该参数设置音频文件的质量,目前还没有效果,设置为0为默认值。
    	);
    	spMap=new HashMap<Integer,Integer>();
    	spMap.put(1, sp.load(this, R.raw.attack02, 1));
    	spMap.put(2, sp.load(this, R.raw.attack14, 1));
    }
    public void playSound(int sound,int number){	//播放声音,参数sound是播放音效的id,参数number是播放音效的次数
    	AudioManager am=(AudioManager)this.getSystemService(this.AUDIO_SERVICE);//实例化AudioManager对象
    	float audioMaxVolumn=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);	//返回当前AudioManager对象的最大音量值
    	float audioCurrentVolumn=am.getStreamVolume(AudioManager.STREAM_MUSIC);//返回当前AudioManager对象的音量值
    	float volumnRatio=audioCurrentVolumn/audioMaxVolumn;
    	sp.play(
    			spMap.get(sound), 					//播放的音乐id
    			volumnRatio, 						//左声道音量
    			volumnRatio, 						//右声道音量
    			1, 									//优先级,0为最低
    			number, 							//循环次数,0无不循环,-1无永远循环
    			1									//回放速度 ,该值在0.5-2.0之间,1为正常速度
    	);
    }
}

前排几个import用来导入程序依赖的相关包;

然后是Activity类,相当于注册我们界面的回调函数,@Override了Activity的 onCreate()函数,当创建MyActivity(即我们的窗口时)调用该方法。

initSoundPool()函数 初始化音效池;

findViewById 将按钮控件实例化;

@Override 按钮的按键监听函数 setOnKeyListener()。


=============================================================================================================

Eclipse显示行号: 【Ctrl+F10】-->>【Show Line Numbers】





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics