Code example for SparseIntArray

Methods: get, indexOfKey, put

0
     * @param id An identifier associated with a loaded sound. 
     * @return {@code true} if the sound was unloaded, or {@code false} if it 
     *         could not be found. 
     */ 
    public boolean unload(int id) {
        final int soundId = mSoundPoolMap.get(id);
        if (soundId == 0) {
            return false; 
        } 
 
        final int streamType = mStreamTypeMap.get(id);
        final SoundPool soundPool = mSoundPoolStreams.get(streamType);
        if (soundPool == null) {
            return false; 
        } 
 
        return soundPool.unload(soundId);
    } 
 
    /** 
     * Plays the previously loaded sound associated with the specified 
     * {@code id}. 
     * 
     * @param id The identifier associated with the sound. 
     * @param rate The playback rate modifier, range {0...2}. 
     * @param volume The volume level modifier, range {0...1}. 
     * @param pan The panning value, range {-1...1} where 0 is center. 
     * @return Whether sound playback started successfully. 
     */ 
    public boolean play(int id, float rate, float volume, float pan) {
        final int soundId = mSoundPoolMap.get(id);
        if (soundId == 0) {
            return false; 
        } 
 
        final int streamType = mStreamTypeMap.get(id);
        final SoundPool soundPool = mSoundPoolStreams.get(streamType);
        if (soundPool == null) {
            return false; 
        } 
 
        final float leftVolume = DEFAULT_VOLUME * Math.min(1.0f, (1.0f - pan)) * volume;
        final float rightVolume = DEFAULT_VOLUME * Math.min(1.0f, (1.0f + pan)) * volume;
        final float playRate = DEFAULT_RATE * rate;
 
        final int resultSoundId = soundPool.play(soundId, leftVolume, rightVolume, 0, 0, playRate);
        if (resultSoundId == 0) {
            LogUtils.log(this, Log.ERROR, "Failed to play sound id %s", id);
            return false; 
        } 
 
        return true; 
    } 
 
    /** 
     * Stops all active sound playback. 
     */ 
    public void interrupt() { 
        // TODO: Stop all active streams. 
    } 
 
    /** 
     * Releases all sound resources. After calling this method, no calls should 
     * be made to this object. 
     */ 
    public void shutdown() { 
        for (int i = (mSoundPoolStreams.size() - 1); i >= 0; i--) {
            mSoundPoolStreams.valueAt(i).release();
        } 
 
        mSoundPoolStreams.clear();
    } 
 
    /** 
     * Returns a {@link SoundPool} for the specified stream type, creating a new 
     * pool if necessary. 
     * 
     * @param streamType The playback stream type. 
     * @return A {@link SoundPool} for the specified stream type. 
     */ 
    private SoundPool getOrCreateSoundPool(int streamType) {
        final SoundPool soundPool = mSoundPoolStreams.get(streamType);
        if (soundPool != null) {
            return soundPool;
        } 
 
        final SoundPool newPool = new SoundPool(MAX_STREAMS, streamType, 0);
        mSoundPoolStreams.put(streamType, newPool);
        return newPool;
    } 
 
    /** 
     * Associates a loaded sound identifier on a particular stream with a 
     * client-specified identifier. If the identifier was already assigned, 
     * unloads the previously assigned sound. 
     * 
     * @param id The client-specified identifier. 
     * @param soundId The sound identifier. 
     * @param streamType The client-specified playback stream. 
     * @return Whether the sound was assigned successfully. 
     */ 
    private boolean assign(int id, int soundId, int streamType) {
        if (soundId <= 0) {
            LogUtils.log(this, Log.ERROR, "Failed to assign sound for %d", id);
            return false; 
        } 
 
        if (mSoundPoolMap.indexOfKey(id) >= 0) {
            unload(id);
        } 
 
        mSoundPoolMap.put(id, soundId);
        mStreamTypeMap.put(id, streamType);
        return true; 
    } 
 
    /**