Android适用于IM通知音频的SoundPool(免通知栏权限)

前言:因为系统对Android通知栏的音频控制,导致系统通知音没有办法出现,所以在通知栏出现的时候调用SoundPool进行音频播放。音频源已经写死,具体使用者根据情况对代码进行改动
使用方式如下:

播放:
SoundPoolUtils.getInstance(this).playMayWait();

销毁:
SoundPoolUtils.getInstance(this).releaseSoundPool();

源代码如下:

package com.dq.im.util;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;

import com.dq.im.R;

/**
 * 提示音播放工具类
 * */
public class SoundPoolUtils implements SoundPool.OnLoadCompleteListener {
    private static final int DEFAULT_INVALID_SOUND_ID = -Integer.MAX_VALUE;
    private static final int DEFAULT_INVALID_STREAM_ID = -Integer.MAX_VALUE;

    private int mSoundId = DEFAULT_INVALID_SOUND_ID;
    private int mStreamID = DEFAULT_INVALID_STREAM_ID;
    private float mCruLeftVolume = 1.0f;
    private float mCurRightVolume = 1.0f; // 用于设置sound pool的左右volume值 0~1f
    private SoundPool mSoundPool;
    private static SoundPoolUtils soundPoolUtils;
    private Context context;
    private SoundPoolUtils(Context context) {
        this.context = context;
    }

    public static SoundPoolUtils getInstance(Context context){
        if (null == soundPoolUtils){
            soundPoolUtils = new SoundPoolUtils(context);
        }
        return soundPoolUtils;
    }
    /**
     * 播放:如果资源还没有加载,则可能会有一小段等待时间
     */
    public void playMayWait() {
        releaseSoundPool();
        createSoundPoolIfNeeded();
        if (mSoundId == DEFAULT_INVALID_SOUND_ID) {  // mSoundId is invalid ,load from res raw for once before mSoundPool is released
            mSoundId = mSoundPool.load(context, R.raw.notification, 1); // 加载音频资源
        } else {
            // reuse the loaded res
            if (mStreamID == DEFAULT_INVALID_STREAM_ID)
                onLoadComplete(mSoundPool, 0, 0);  // manually call this method when there is a valid mSoundId
        }
    }
    /**
     * 创建SoundPool
     */
    private void createSoundPoolIfNeeded() {
        if (mSoundPool == null) {
            // 5.0 及 之后
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                AudioAttributes audioAttributes = null;
                audioAttributes = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .build();

                mSoundPool = new SoundPool.Builder()
                        .setMaxStreams(16)
                        .setAudioAttributes(audioAttributes)
                        .build();
            } else { // 5.0 以前
                mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);  // 创建SoundPool
            }

            mSoundPool.setOnLoadCompleteListener(this);  // 设置加载完成监听
        }
    }

    /**
     * 释放资源
     */
    public void releaseSoundPool() {
        if (mSoundPool != null) {
            mSoundPool.autoPause();
            mSoundPool.unload(mSoundId);
            mSoundId = DEFAULT_INVALID_SOUND_ID;
            mStreamID = DEFAULT_INVALID_STREAM_ID;
            mSoundPool.release();
            mSoundPool = null;
        }
    }

    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

        if (mSoundPool != null) {
            if (mStreamID == DEFAULT_INVALID_STREAM_ID)
                mStreamID = mSoundPool.play(mSoundId, mCruLeftVolume, mCurRightVolume, 16, 1, 1.0f);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值