android声音播放

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

程序运行效果如下:

点击按钮实验下吧~~!

原文地址:https://www.cnblogs.com/alwaysfirst/p/4004838.html