使用soundPool播放音訊

never123450發表於2014-07-11

fragment_main.xml

<LinearLayout 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: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.xwy.ShengYin.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="風鈴聲" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="布穀鳥叫聲" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="門鈴聲" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="電話聲" />

</LinearLayout>

MainActivity.java

package com.xwy.ShengYin;

import java.util.HashMap;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends Activity {

	private SoundPool soundPool;// 宣告一個SoundPool物件
	private HashMap<Integer, Integer> soundMap = new HashMap<Integer, Integer>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		Button chimes = (Button) findViewById(R.id.button1); // 獲取“風鈴聲”按鈕
		Button enter = (Button) findViewById(R.id.button2); // 獲取“布穀鳥叫聲”按鈕
		Button notify = (Button) findViewById(R.id.button3); // 獲取“門鈴聲”按鈕
		Button ringout = (Button) findViewById(R.id.button4); // 獲取“電話聲”按鈕

		soundPool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 0);// 建立一個SoundPool物件,該物件可以容納5個音訊流
		// 將要播放的音訊流儲存到HashMap物件中
		soundMap.put(1, soundPool.load(this, R.raw.chimes, 1));
		soundMap.put(2, soundPool.load(this, R.raw.enter, 1));
		soundMap.put(3, soundPool.load(this, R.raw.notify, 1));
		soundMap.put(4, soundPool.load(this, R.raw.ringout, 1));
		soundMap.put(5, soundPool.load(this, R.raw.ding, 1));
		// 為各按鈕新增單擊事件監聽器
		chimes.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1);
			}
		});
		enter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1);
			}
		});
		notify.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1);// 播放指定的音訊

			}
		});
		ringout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				soundPool.play(soundMap.get(4), 1, 1, 0, 0, 1);// 播放指定的音訊
				soundPool.play(
						soundPool.load(MainActivity.this, R.raw.notify, 1), 1,
						1, 0, 0, 1);
			}
		});

	}
	//重寫鍵被按下的事件
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		soundPool.play(soundMap.get(5), 1, 1, 0, 0, 1);
		return true;
	}

	@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;
	}

	@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();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}


相關文章