柔城

SOSOFT articles

Android声音播放实例代码

布局文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.ch_2013_4_9playsound"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.example.ch_2013_4_9playsound.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25     </application>
26 
27 </manifest>

Activity:

 1 package com.example.ch_2013_4_9playsound;
 2 
 3 import java.util.HashMap;
 4 
 5 import android.media.AudioManager;
 6 import android.media.SoundPool;
 7 import android.os.Bundle;
 8 import android.annotation.SuppressLint;
 9 import android.app.Activity;
10 import android.content.Context;
11 import android.view.Menu;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.widget.Button;
15 import android.widget.Toast;
16 
17 @SuppressLint("UseSparseArrays")
18 public class MainActivity extends Activity implements OnClickListener {
19 
20     Button btn_play;
21     Button btn_play1;
22     Button btn_stop;
23     Button btn_stop1;
24 
25     SoundPool sp;
26     HashMap<Integer, Integer> spMap;
27 
28     @Override
29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.activity_main);
32         
33         InitSound();
34         Init();
35     }
36 
37     @Override
38     public boolean onCreateOptionsMenu(Menu menu) {
39         // Inflate the menu; this adds items to the action bar if it is present.
40         getMenuInflater().inflate(R.menu.main, menu);
41         return true;
42     }
43         //http://www.cnblogs.com/sosoft/
44 
45     public void Init() {
46         btn_play = (Button) findViewById(R.id.btn_play);
47         btn_play1 = (Button) findViewById(R.id.btn_play1);
48         btn_stop = (Button) findViewById(R.id.btn_stop);
49         btn_stop1 = (Button) findViewById(R.id.btn_stop1);
50         
51         btn_play.setOnClickListener(this);
52         btn_play1.setOnClickListener(this);
53         btn_stop.setOnClickListener(this);
54         btn_stop1.setOnClickListener(this);
55     }
56 
57     public void InitSound() {
58         sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
59         spMap = new HashMap<Integer, Integer>();
60         spMap.put(1, sp.load(this, R.raw.fire, 1));
61         spMap.put(2, sp.load(this, R.raw.hit, 1));
62 
63     }
64 
65     public void playSound(int sound, int number) {
66         AudioManager am = (AudioManager) this
67                 .getSystemService(Context.AUDIO_SERVICE);
68         float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
69         float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
70         float volumnRatio = volumnCurrent / audioMaxVolumn;
71 
72         sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number,  1f);
73     }
74 
75     @Override
76     public void onClick(View v) {
77         // TODO Auto-generated method stub
78         if (v == btn_play) {
79             playSound(1, 0);
80             Toast.makeText(this, "play1", Toast.LENGTH_SHORT).show();
81         }
82         if (v == btn_play1) {
83             playSound(2, 1);
84             Toast.makeText(this, "play2", Toast.LENGTH_SHORT).show();
85         }
86         if (v == btn_stop) {
87             sp.pause(spMap.get(1));
88             Toast.makeText(this, "stop", Toast.LENGTH_SHORT).show();
89         }
90         if (v == btn_stop1) {
91             sp.pause(spMap.get(2));
92             Toast.makeText(this, "stop1", Toast.LENGTH_SHORT).show();
93         }
94     }
95 
96 }

 

posted on 2014-01-14 14:08  柔城  阅读(13679)  评论(0编辑  收藏  举报

导航