018_04音效播放之MediaPlayer与SoundPool

  音频资源文件一般放在Android应用下的/res/raw目录下。

  如果应用程序经常需要播放密集,短促的音效,这时还用mediaPlayer就显得有些不合适。MediaPlayer存在如下缺点:

--资源占用量较高,延迟时间较长

--不支持多个音效同时播放

  所以Android提供了SoundPool来播放音效,SoundPool使用音效池的概念来管理多个短促的音效。

下面源代码将使用MediaPlayer播放游戏背景音乐,使用SoundPool播放射击音效

 1 package com.example.day18_05gameaudiodemo;
 2 
 3 import android.app.Activity;
 4 import android.media.AudioManager;
 5 import android.media.MediaPlayer;
 6 import android.media.SoundPool;
 7 import android.os.Bundle;
 8 import android.os.Environment;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 
13 public class MainActivity extends Activity implements OnClickListener {
14 
15     private SoundPool sp;
16     private int sound1;
17     private int sound2;
18     private int sound3;
19     private MediaPlayer mediaPlayer;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         
26         Button bt_gb = (Button) findViewById(R.id.bt_bg);
27         Button bt_shoot1 = (Button) findViewById(R.id.bt_shoot1);
28         Button bt_shoot2 = (Button) findViewById(R.id.bt_shoot2);
29         Button bt_shoot3 = (Button) findViewById(R.id.bt_shoot3);
30 
31         
32         bt_gb.setOnClickListener(this);
33         bt_shoot1.setOnClickListener(this);
34         bt_shoot2.setOnClickListener(this);
35         bt_shoot3.setOnClickListener(this);
36 
37         
38         sp = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
39         sound1 = sp.load(this, R.raw.shoot1, 1);
40         sound2 = sp.load(this, R.raw.shoot2, 1);
41         sound3 = sp.load(this, R.raw.shoot3, 1);
42 
43         mediaPlayer = new MediaPlayer();
44         mediaPlayer.reset();
45         try {
46             mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()
47                         .getAbsolutePath() + "/bgmusic.mp3");
48             mediaPlayer.prepare();
49         } catch ( Exception e) {
50             // TODO Auto-generated catch block
51             e.printStackTrace();
52         }
53         
54     }
55 
56     @Override
57     public void onClick(View v) {
58         // TODO Auto-generated method stub
59         switch (v.getId()) {
60         case R.id.bt_bg:            
61             if (mediaPlayer!=null) {
62                 mediaPlayer.start();
63             }
64             break;
65         case R.id.bt_shoot1:
66             sp.play(sound1, 1, 1, 0, 0, 1);
67             break;
68         case R.id.bt_shoot2:
69             sp.play(sound2, 1, 1, 0, 0, 1);
70 
71                  break;
72         case R.id.bt_shoot3:
73             sp.play(sound3, 1, 1, 0, 0, 1);
74                  break;
75         default:
76             break;
77         }
78     }
79 }
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="wrap_content"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.day18_05gameaudiodemo.MainActivity" 
10     android:background="@drawable/bg">
11 
12     <Button
13         android:id="@+id/bt_bg"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="背景音乐" />
17     
18     
19     <LinearLayout 
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"   
22         android:layout_alignParentRight="true"
23         android:layout_alignParentBottom="true"
24         android:orientation="vertical"
25         >
26     <Button
27         android:id="@+id/bt_shoot1"
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:text="shoot1" />
31     
32     <Button
33         android:id="@+id/bt_shoot2"        
34         android:layout_width="wrap_content"
35         android:layout_height="wrap_content"
36         android:text="shoot2" />
37     
38     <Button       
39          android:id="@+id/bt_shoot3"
40         android:layout_width="wrap_content"
41         android:layout_height="wrap_content"
42         android:text="shoot3" />
43     </LinearLayout>
44 
45 </RelativeLayout>

物随心转,境由心造,一切烦恼皆由心生。
原文地址:https://www.cnblogs.com/woodrow2015/p/4542662.html