Android 即時音效SoundPool

先貼上代碼
package com.example.voice;

import java.util.HashMap;
import java.util.Map;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private Button bt_start_one;
	private Button bt_start_two;
	private Button bt_pause_one;
	private Button bt_pause_two;
	private SoundPool sp;
	private Map<Integer, Integer> map;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initSoundpool();
		bt_start_one = (Button) findViewById(R.id.bt_start_one);
		bt_start_two = (Button) findViewById(R.id.bt_start_two);
		bt_pause_one = (Button) findViewById(R.id.bt_pause_one);
		bt_pause_two = (Button) findViewById(R.id.bt_pause_two);
		bt_start_one.setOnClickListener(this);
		bt_start_two.setOnClickListener(this);
		bt_pause_one.setOnClickListener(this);
		bt_pause_two.setOnClickListener(this);

	}

	/**
	 * 初始化
	 */
	private void initSoundpool() {
		sp = new SoundPool(5,// 同時播放的音效
				AudioManager.STREAM_MUSIC, 0);
		map = new HashMap<Integer, Integer>();
		map.put(1, sp.load(this, R.raw.attack02, 1));
		map.put(2, sp.load(this, R.raw.attack14, 1));
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_start_one:
			playSound(1, 10);// 播放第一首音效,播放一遍
			Toast.makeText(this, "播放第一首音效", 0).show();
			break;
		case R.id.bt_start_two:
			playSound(2, 10);
			Toast.makeText(this, "播放第二首音效", 0).show();
			break;
		case R.id.bt_pause_one:
			sp.pause(map.get(1));
			Toast.makeText(this, "暫停第一首音效", 0).show();
			break;
		case R.id.bt_pause_two:
			sp.pause(map.get(2));
			Toast.makeText(this, "暫停第二首音效", 0).show();
			break;

		default:
			break;
		}

	}

	/**
	 * 
	 * @param sound 文件
	 * @param number 循環次數
	 */
	private void playSound(int sound, int number) {
		AudioManager am = (AudioManager) getSystemService(this.AUDIO_SERVICE);// 實例化
		float audioMaxVolum = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 音效最大值
		float audioCurrentVolum = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		float audioRatio = audioCurrentVolum / audioMaxVolum;
		sp.play(map.get(sound), 
				audioRatio,// 左聲道音量
				audioRatio,// 右聲道音量
				1, // 優先級
				number,// 循環播放次數
				1);// 回放速度,該值在0.5-2.0之間 1爲正常速度
	}

}
界面代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt_start_one"
        android:layout_marginBottom="10dip"
        android:text="播放即時音效1"
        
        />
    
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt_pause_one"
        android:layout_marginBottom="10dip"
         android:text="暫停即時音效1"
        />
    
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt_start_two"
        android:layout_marginBottom="10dip"
         android:text="播放即時音效2"
        />
    
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt_pause_two"
        android:layout_marginBottom="10dip"
         android:text="暫停即時音效1"
        />
</LinearLayout>






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章