Android SoundPool播放实例和方法详解

SoundPool:用于短暂播放和对反应速度要求较高的音乐(如游戏音、按键音)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
    android:id="@+id/strat"
    android:layout_width="match_parent"
    android:layout_height=
    "wrap_content" 
    android:text="开始播放"
    />
    <Button
    android:id="@+id/pause"
    android:layout_width="match_parent"
    android:layout_height=
    "wrap_content" 
    android:text="暂停播放"
    />
    <Button
    android:id="@+id/resume"
    android:layout_width="match_parent"
    android:layout_height=
    "wrap_content" 
    android:text="继续播放"
    />
    <Button
    android:id="@+id/stop"
    android:layout_width="match_parent"
    android:layout_height=
    "wrap_content" 
    android:text="停止播放"
    />
</LinearLayout>

MainActivity

package com.sun.activity;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    SoundPool soundPool;
    HashMap<Integer, Integer> map;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonStrat = (Button) findViewById(R.id.strat);
        Button buttonStop = (Button) findViewById(R.id.stop);
        Button buttonPause = (Button) findViewById(R.id.pause);
        Button buttonResume = (Button) findViewById(R.id.resume);
        buttonStop.setOnClickListener(this);
        buttonStrat.setOnClickListener(this);
        buttonPause.setOnClickListener(this);
        buttonResume.setOnClickListener(this);
        /* 这里只是初始对象
         * 第一个参数:设置同时播放流的个数
         * 第二个参数:流类型
         *      1.AudioManager.STREAM_ALARM:警报的音频流
         *      2.AudioManager.STREAM_DTMF :DTMF(双音多频)音调的音频流
         *      3.AudioManager.STREAM_MUSIC:音乐播放的音频流    
         *      4.AudioManager.STREAM_NOTIFICATION:通知的音频流
         *      5.AudioManager.STREAM_RING:电话的音频流环
         *      6.AudioManager.STREAM_SYSTEM:系统声音的音频流
         *      7.AudioManager.STREAM_VOICE_CALL:电话的音频流
         * 第三个参数:采样率转换器质量。就是声音的品质
         *      源码是这样注释的
         *   the sample-rate converter quality.
         *   Currently has no effect.
         *    Use 0 for the default.
         */
        soundPool = new SoundPool(4, AudioManager.STREAM_VOICE_CALL, 100);

        map = new HashMap<Integer, Integer>();
        /*
         *  int  load(Context context, int resId, int priority)  //从raw资源载入
            int  load(FileDescriptor fd, long offset, long length, int priority)  //从FileDescriptor对象载入
            int  load(AssetFileDescriptor afd, int priority)  //从Asset对象载入
            int  load(String path, int priority)  //从完整文件路径名载入  
            最后一个参数为优先级源代码注释是这样说的
            the priority of the sound. Currently has no effect. Use a value of  1 for future compatibility.     
            声音的优先级。目前没有影响。为以后的兼容性请设置为1。
         */
        map.put(1, soundPool.load(this, R.raw.ee, 1));



    }


    int falg;
    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.strat:

                AudioManager mgr = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
                //最大音量
                float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                //当前音量
                float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
                float volume = streamVolumeCurrent/streamVolumeMax;

                /**
                * @param soundID返回的soundID负载()函数
                * @param leftVolume左声道(范围= 0.0 - 1.0)
                * @param rightVolume右声道(范围= 0.0 - 1.0)
                * @param 流的优先级(0 =最低优先级)
                * @param 循环模式(0 =没有循环,-1=永远循环,其他次数[1,2,3,4,5...])
                * @param 播放速度(1.0 =正常播放,范围0.5 - 2.0)
                * @return 非零streamID如果成功,零是失败了
                */
                 falg=soundPool.play(map.get(1), volume,volume, 1, 0, 1.0f);//播放1次音乐
                if(falg==0){
                Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT).show();
                }
                Log.v("aaaaaa",falg+"");
                break;
            case R.id.stop:
                soundPool.stop(falg);
                break;
            case R.id.pause:

                soundPool.pause(falg);
                break;  
            case R.id.resume:

                soundPool.resume(falg);
                break;      

        }

        /*
         * 
        final void pause(int streamID) 
        暂停指定播放流的音效(streamID 应通过play()返回)。
        final void resume(int streamID) 
        继续播放指定播放流的音效(streamID 应通过play()返回)。
        final void stop(int streamID) 
        终止指定播放流的音效(streamID 应通过play()返回)。
        这几个方法官方api都有一个这样的注释
        This is the value returned by the play() function.
        */

    }
}

运行效果
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智海深渊

对您有帮助给点鼓励吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值