android soundPool封装

public class AudioTool {

private static final String TAG = "UponAudio";

    private static ConcurrentHashMap<Integer, Integer> resId2SoundId = new ConcurrentHashMap<Integer, Integer>();
    private static ConcurrentHashMap<Integer, Integer> resId2StreamId = new ConcurrentHashMap<Integer, Integer>();
    private static ConcurrentHashMap<Integer, MediaPlayer> resId2Player = new ConcurrentHashMap<Integer, MediaPlayer>();

    private static SoundPool soundPool = new SoundPool(5/* 最多同时播放5个音效 */,AudioManager.STREAM_MUSIC, 0/* 默认优先级为最低 */);

    public static void preloadEffect(Context context, int resId) {
            if (!resId2SoundId.containsKey(resId)) {
                    int soundId = soundPool.load(context, resId, 0);
                    if (soundId > 0) {
                            resId2SoundId.put(resId, soundId);
                    } else {
                            Log.d(TAG, "preload effect " + resId + " failed");
                    }
            }
    }

    public static void playEffect(Context context, int resId) {
            if (!resId2SoundId.containsKey(resId)) {// 未load
                    int soundId = soundPool.load(context, resId, 0);
                    if (soundId > 0) {
                            resId2SoundId.put(resId, soundId);
                    } else {
                            Log.d(TAG, "load effect " + resId + " failed");
                    }
            }
            int streamId = soundPool.play(resId2SoundId.get(resId), 1.0f, 1.0f, 0, 0, 1.0f);
            if (streamId > 0) {// play successful
                    resId2StreamId.put(resId, streamId);
            } else {// play failed
                    Log.d(TAG, "play effect " + resId + " failed");
            }
    }

    /**
     * @param resId
     */
    public static void pauseEffect(int resId) {
            if (!resId2StreamId.containsKey(resId)) {// 并未播放
                    return;
            }
            soundPool.pause(resId2StreamId.get(resId));
    }

    /**
     * @param resId
     */
    public static void stopEffect(int resId) {
            if (!resId2StreamId.containsKey(resId)) {
                    return;
            }
            soundPool.stop(resId2StreamId.get(resId));
    }

    /**
     * @param resId
     */
    public static void unloadEffect(int resId) {
            if (!resId2SoundId.containsKey(resId)) {
                    return;
            }
            soundPool.unload(resId2SoundId.get(resId));
            resId2SoundId.remove(resId);
            resId2StreamId.remove(resId);
    }

    /**
     * 释放所有资源,在程序退出是调用
     */
    public static void release() {
            soundPool.release();
            resId2SoundId.clear();
            resId2StreamId.clear();
            if (resId2Player.size() > 0) {
                    for (Map.Entry<Integer, MediaPlayer> entry : resId2Player.entrySet()) {
                            if (entry.getValue() != null) {
                                    entry.getValue().release();
                            }
                    }
            }
            resId2Player.clear();
    }

    /**
     * @param resId  播放背景音效
     */
    public static void playBkgMusicSync(final Context context, final int resId) {
            if (resId2Player.containsKey(resId)) {
                    MediaPlayer player = resId2Player.get(resId);
                    if (!player.isPlaying()) {// 如果是stop状态,即进入ErrorListener处理
                            player.start();
                    }
            } else {
                    final MediaPlayer player = MediaPlayer.create(context, resId);
                    if (player != null) {
                            resId2Player.put(resId, player);// 添加到map中
                            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                            player.setLooping(true);
                            int volume = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE))
                                            .getStreamVolume(AudioManager.STREAM_MUSIC);
                            player.setVolume(volume, volume);
                            player.setOnErrorListener(new OnErrorListener() {
                                    @Override
                                    public boolean onError(MediaPlayer mp, int what, int extra) {
                                            mp.release();
                                            resId2Player.remove(resId);
                                            return true;
                                    }
                            });
                            player.start();
                    }
            }
    }

    /**
     * @param resId     
     */
    public static void playBkgMusicAsync(final Context context, final int resId) {
            if (resId2Player.containsKey(resId)) {
                    MediaPlayer player = resId2Player.get(resId);
                    if (!player.isPlaying()) {// 如果是stop状态,即进入ErrorListener处理
                            player.start();
                    }
            } else {
                    final MediaPlayer mp = new MediaPlayer();
                    try {
                            AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
                            if (afd == null) {
                                    Log.d(TAG, "can not open raw resource " + resId);
                                    return;
                            }

                            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                            mp.setLooping(true);
                            int volume = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE))
                                            .getStreamVolume(AudioManager.STREAM_MUSIC);
                            mp.setVolume(volume, volume);

                            mp.setOnPreparedListener(new OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mp) {
                                            Log.d(TAG, "background music " + resId + " prepared");
                                            resId2Player.put(resId, mp);// 添加到map中
                                            mp.start();
                                    }
                            });

                            mp.setOnErrorListener(new OnErrorListener() {
                                    @Override
                                    public boolean onError(MediaPlayer mp, int what, int extra) {
                                            mp.release();
                                            resId2Player.remove(resId);
                                            return true;
                                    }
                            });
                            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                            afd.close();
                            mp.prepareAsync();
                    } catch (IOException ex) {
                            Log.d(TAG, "create failed:", ex);
                    } catch (IllegalArgumentException ex) {
                            Log.d(TAG, "create failed:", ex);
                    } catch (SecurityException ex) {
                            Log.d(TAG, "create failed:", ex);
                    }
            }
    }

    public static void pauseBkgMusic(int resId) {
            if (resId2Player.containsKey(resId)) {
                    MediaPlayer mp = resId2Player.get(resId);
                    if (mp.isPlaying()) {
                            mp.pause();
                    }
            }
    }

    public static void stopBkgMusic(int resId) {
            if (resId2Player.containsKey(resId)) {
                    MediaPlayer mp = resId2Player.get(resId);
                    mp.stop();
            }
    }

    public static void releaseBkgMusic(int resId) {
            if (resId2Player.containsKey(resId2Player)) {
                    MediaPlayer mp = resId2Player.get(resId);
                    if (mp != null) {
                            mp.release();
                            resId2Player.remove(resId);
                            mp = null;
                    }
            }
    }
}

你可能感兴趣的:(SoundPool)