Code example for SparseIntArray

Methods: indexOfKey, put

0
     * 
     * @param resId Resource ID of the sound to be loaded. 
     * @return The sound pool identifier for the resource. 
     */ 
    public int preloadSound(int resId) {
        if (mResourceIdToSoundMap.indexOfKey(resId) >= 0) {
            return mResourceIdToSoundMap.get(resId);
        } 
 
        final int soundPoolId;
        final String resType = mResources.getResourceTypeName(resId);
 
        if ("raw".equals(resType)) {
            soundPoolId = mSoundPool.load(mContext, resId, 1);
        } else if ("array".equals(resType)) {
            final int[] notes = mResources.getIntArray(resId);
            soundPoolId = loadMidiSoundFromArray(notes, false);
        } else { 
            LogUtils.log(this, Log.ERROR, "Failed to load sound: Unknown resource type");
            return -1; 
        } 
 
        if (soundPoolId < 0) {
            LogUtils.log(this, Log.ERROR, "Failed to load sound: Invalid sound pool ID");
            return -1; 
        } 
 
        mResourceIdToSoundMap.put(resId, soundPoolId);
        return soundPoolId;
    } 
 
    /** 
     * Convenience method for playing different sounds based on a boolean 
     * result. 
     * 
     * @param result The conditional that controls which sound resource is 
     *            played. 
     * @param trueResId The resource to play if the result is {@code true}. 
     * @param falseResId The resource to play if the result is {@code false}. 
     * @see #playSound(int) 
     */ 
    public void playSoundConditional(boolean result, int trueResId, int falseResId) {
        final int resId = (result ? trueResId : falseResId);
 
        if (resId > 0) {
            playSound(resId);
        } 
    } 
 
    /** 
     * Plays the sound file specified by the given resource identifier at the 
     * default rate. 
     * 
     * @param resId The sound file's resource identifier. 
     * @return {@code true} if successful 
     */ 
    public boolean playSound(int resId) {
        return playSound(resId, DEFAULT_RATE, DEFAULT_VOLUME);
    } 
 
    /** 
     * Plays the sound file specified by the given resource identifier on the 
     * given stream 
     * 
     * @param resId The resource identifier of the sound to play 
     * @param streamId The {@link AudioManager} identifier of the stream on 
     *            which to play the sound 
     * @return {@code true} if successful 
     */ 
    public boolean playSoundOnStream(int resId, int streamId, float volume) {
        if (!mAuditoryEnabled) {
            return false; 
        } 
 
        final MediaPlayer mp = new MediaPlayer();
        mp.setOnPreparedListener(new OnPreparedListener() {
            @Override 
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            } 
        }); 
 
        mp.setOnCompletionListener(new OnCompletionListener() {
            @Override 
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            } 
        }); 
 
        final AssetFileDescriptor fd = mContext.getResources().openRawResourceFd(resId);
        mp.setAudioStreamType(streamId);
        try { 
            mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
            mp.setVolume(volume, volume);
            mp.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
            return false; 
        } 
 
        return true; 
    } 
 
    /** 
     * Plays the sound file specified by the given resource identifier at the 
     * specified rate. 
     * 
     * @param resId The sound file's resource identifier. 
     * @param rate The rate at which to play back the sound, where 1.0 is normal 
     *            speed and 0.5 is half-speed. 
     * @param volume The volume at which to play the sound, where 1.0 is normal 
     *            volume and 0.5 is half-volume. 
     * @return {@code true} if successful 
     */ 
    public boolean playSound(int resId, float rate, float volume) {
        if (!mAuditoryEnabled) {
            return false; 
        } 
 
        if (mResourceIdToSoundMap.indexOfKey(resId) < 0) {
            final int soundPoolId = preloadSound(resId);
            mPostLoadPlayables.add(soundPoolId);
 
            // Since we'll play the sound immediately after it loads, just 
            // assume it will play successfully.