Android 播放短提示音

Android提供了音频API MediaPlayer AudioTrack 

还有一种轻量级的叫SoundPool 它适合播放那些需要反复播放,但时间较短的音效。它支持同时播放多种声音,这些声音在系统开始时会加载到列表中,按照这些声音的id,我们可以调用这些音效。

我们可以看下创建方法和使用

SoundPool(int maxStreams, int streamType, int srcQuality)的参数

maxStreams:指定支持多少个声音,SoundPool对象中允许同时存在的最大流的数量

streamType:声音类型,流类型可以分为STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING,STREAM_MUSIC 和 STREAM_ALARM四种类型。在AudioManager

定义

srcQuality:声音品质(采样率变换质量),当前无效果,用0作为默认

SoundPool.Builder

play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

soundID:Load()返回的声音ID号

leftVolume:左声道音量设置

rightVolume:右声道音量设置

priority:播放声音的优先级,数值越高,优先级越大。

loop:是否循环:-1表示无限循环,0表示不循环,其他值表示要重复播放的次数

rate:播放速率:播放速率的取值范围是0.5至2.0,1.0为原始播放速率,2.0的播放速率为原始速率的两位。0.5的播放速率为原始速率的一半。

release()方法释放所有SoundPool对象占据的内存和资源,也可以指定要释放的ID


简单来说要五步

创建SoundPool 加载资源ID 播放资源ID  Stop资源ID 释放资源ID

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import static android.content.Context.AUDIO_SERVICE;

public class SoundPoolManager {

    private boolean playing = false;
    private boolean loaded = false;
    private float actualVolume;
    private float maxVolume;
    private float volume;
    private AudioManager audioManager;
    private SoundPool soundPool;
    private int ringingSoundId;
    private int ringingStreamId;
    private int disconnectSoundId;
    private static SoundPoolManager instance;
    /*Mny*/
    private int outgoingSoundId;

    private SoundPoolManager(Context context) {
        // AudioManager audio settings for adjusting the volume
        audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
        actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        volume = actualVolume / maxVolume;

        // Load the sounds
        //因为在5.0上new SoundPool();被弃用 5.0上利用Builder
        //创建SoundPool
        int maxStreams = 1;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool = new SoundPool.Builder()
                    .setMaxStreams(maxStreams)
                    .build();
        } else {
            soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
        }

        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                loaded = true;
            }

        });
        //加载资源ID
        ringingSoundId = soundPool.load(context, R.raw.incoming, 1);
        disconnectSoundId = soundPool.load(context, R.raw.disconnect, 1);
        outgoingSoundId = soundPool.load(context, R.raw.outgoing, 1);
    }

    public static SoundPoolManager getInstance(Context context) {
        if (instance == null) {
            instance = new SoundPoolManager(context);
        }
        return instance;
    }
    //播放
    public void playRinging() {
        if (loaded && !playing) {
            ringingStreamId = soundPool.play(ringingSoundId, volume, volume, 1, -1, 1f);
            playing = true;
        }
    }
    //Stop播放
    public void stopRinging() {
        if (playing) {
            soundPool.stop(ringingStreamId);
            playing = false;
        }
    }
    public void release() {
        if (soundPool != null) {
            soundPool.unload(ringingSoundId);
            soundPool.unload(disconnectSoundId);
            soundPool.release();
            soundPool = null;
        }
        instance = null;
    }
}

 
  








你可能感兴趣的:(解决方案)