android播放聲音,一種是soundPool,一種是mediaplayer
soundpool 適合播放反映速度要求較高的聲效,比如,游戲中的爆炸音效
mediaplay 適合播放時間比較長的聲效,比如,游戲中的背景音樂
我們來做個例子,一個是chang.ogg,一個是duan.wav
這兩個聲效文件,我是從我的游戲目錄中的笑傲江湖OL中搜索出來的。你也可以到你的游戲文件夾下搜索 *.ogg,*.wav
將這兩個文件放到res/raw目錄下,如果不存在raw目錄,請創建它
定義activity_main.xml,里邊放一個TextView和4個Button
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.ssln.sound.MainActivity" > <TextView android:id="@+id/tvMsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="沒有音效被播放" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SoundPlayer播放" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SoundPlayer停止" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="MediaPlayer播放" /> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="MediaPlayer停止" /> </LinearLayout>
然后我們在mainActivity.java中實現點擊按鈕進行播放和停止音效
package com.ssln.sound; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements View.OnClickListener { // 4個按鈕和一個TextView private Button btnSoundStart, btnSoundStop, btnMediaStart, btnMediaStop; private TextView tvMsg; private MediaPlayer mPlayer; private SoundPool mSound; private HashMap<Integer, Integer> soundPoolMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); InitSounds(); setContentView(R.layout.activity_main); btnSoundStart = (Button) findViewById(R.id.button1); btnSoundStop = (Button) findViewById(R.id.button2); btnMediaStart = (Button) findViewById(R.id.button3); btnMediaStop = (Button) findViewById(R.id.button4); tvMsg = (TextView) findViewById(R.id.tvMsg); btnSoundStart.setOnClickListener(this); btnSoundStop.setOnClickListener(this); btnMediaStart.setOnClickListener(this); btnMediaStop.setOnClickListener(this); } @Override public void onClick(View v) { if (v == btnSoundStart) { this.PlaySound(1, 0); tvMsg.setText("SoundPool播放"); } else if (v == btnSoundStop) { mSound.pause(1); tvMsg.setText("SoundPool暫停"); }else if(v==btnMediaStart){
if(!mPlayer.isPlaying()) mPlayer.start(); tvMsg.setText("MediaPlayer播放"); }else if(v==btnMediaStop){
if(mPlayer.isPlaying()) mPlayer.pause(); tvMsg.setText("MediaPlayer暫停"); } } /** * 初始化聲音 */ private void InitSounds() { // 設置播放音效 mPlayer = MediaPlayer.create(this, R.raw.chang); // 第一個參數為同時播放數據流的最大個數,第二數據流類型,第三為聲音質量 mSound = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); soundPoolMap = new HashMap<Integer, Integer>(); soundPoolMap.put(1, mSound.load(this, R.raw.duan, 1)); //可以在后面繼續put音效文件 } /** * soundPool播放 * * @param sound * 播放第一個 * @param loop * 是否循環 */ private void PlaySound(int sound, int loop) { AudioManager mgr = (AudioManager) this .getSystemService(Context.AUDIO_SERVICE); // 獲取系統聲音的當前音量 float currentVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); // 獲取系統聲音的最大音量 float maxVolume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 獲取當前音量的百分比 float volume = currentVolume / maxVolume; // 第一個參數是聲效ID,第二個是左聲道音量,第三個是右聲道音量,第四個是流的優先級,最低為0,第五個是是否循環播放,第六個播放速度(1.0 =正常播放,范圍0.5 - 2.0) mSound.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f); } }
程序運行效果如下:
點擊按鈕實驗下吧~~!