zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    2021,6,10 xjzx 模拟考试
    平衡树(二)——Treap
    AtCoder Beginner Contest 204 A-E简要题解
    POJ 2311 Cutting Game 题解
    Codeforces 990G GCD Counting 题解
    NOI2021 SDPTT D2T1 我已经完全理解了 DFS 序线段树 题解
    第三届山东省青少年创意编程与智能设计大赛总结
    Luogu P6042 「ACOI2020」学园祭 题解
    联合省选2021 游记
    Codeforces 1498E Two Houses 题解 —— 如何用结论吊打标算
  • 原文地址:https://www.cnblogs.com/sosoft/p/3518919.html
Copyright © 2011-2022 走看看