Example usage for android.content.res AssetFileDescriptor getLength

List of usage examples for android.content.res AssetFileDescriptor getLength

Introduction

In this page you can find the example usage for android.content.res AssetFileDescriptor getLength.

Prototype

public long getLength() 

Source Link

Document

Returns the total number of bytes of this asset entry's data.

Usage

From source file:com.mitre.holdshort.MainActivity.java

private void playAlert() {

    // Check if the user wants aural alerts
    if (auralAlerts) {

        // Check and make sure counter is less than the size of the list
        // If not, clear sounds and reset counter
        if (soundNum < sounds.size()) {
            try {
                // Go through each of the sounds and see if the current
                // sound matches
                // If it does then set the dataSource for the player and
                // play
                // When the sound is done playing the finish callback
                // increments soundNum
                // and sends it back to this method.

                if (sounds.get(soundNum).equalsIgnoreCase("HOLD_SHORT")) {

                    AssetFileDescriptor afd = getAssets().openFd("holdShortOfRunway.mp3");
                    mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    mp.prepare();/*w  w  w . ja va  2 s.  co m*/
                    mp.start();
                }

                if (sounds.get(soundNum).equalsIgnoreCase("CROSSING")) {
                    AssetFileDescriptor afd = getAssets().openFd("crossingRunway.mp3");
                    mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    mp.prepare();
                    mp.start();
                }
                if (sounds.get(soundNum).equalsIgnoreCase("NO_CLEARANCE")) {
                    AssetFileDescriptor afd = getAssets().openFd("holdShortNoClearanceForRunway.mp3");
                    mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    mp.prepare();
                    mp.start();
                }

                if (sounds.get(soundNum).equalsIgnoreCase("WRONG_RUNWAY")) {

                    AssetFileDescriptor afd = getAssets().openFd("noTakeoffClearance.mp3");
                    mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    mp.prepare();
                    mp.start();
                }

                if (settings.getBoolean("announceRWY", true)) {

                    if (sounds.get(soundNum).equalsIgnoreCase("1")) {
                        AssetFileDescriptor afd = getAssets().openFd("1.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("2")) {
                        AssetFileDescriptor afd = getAssets().openFd("2.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("3")) {
                        AssetFileDescriptor afd = getAssets().openFd("3.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("4")) {
                        AssetFileDescriptor afd = getAssets().openFd("4.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("5")) {
                        AssetFileDescriptor afd = getAssets().openFd("5.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("6")) {
                        AssetFileDescriptor afd = getAssets().openFd("6.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("7")) {
                        AssetFileDescriptor afd = getAssets().openFd("7.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("8")) {
                        AssetFileDescriptor afd = getAssets().openFd("8.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("9")) {
                        AssetFileDescriptor afd = getAssets().openFd("9.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("0")) {
                        AssetFileDescriptor afd = getAssets().openFd("0.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("L")) {
                        AssetFileDescriptor afd = getAssets().openFd("left.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("C")) {
                        AssetFileDescriptor afd = getAssets().openFd("center.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                    if (sounds.get(soundNum).equalsIgnoreCase("R")) {
                        AssetFileDescriptor afd = getAssets().openFd("right.mp3");
                        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.start();
                    }
                }
            } catch (IOException e) {

            }

        } else {
            sounds.clear();
            soundNum = 0;
        }
    }
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

/**
 * Plays the specified sound from the application asset folder.
 * //from   www  .j a  va 2 s  .  com
 * @param context
 * @param assetSoundPath   Path to the sound in the assets folder.
 */
public static void media_soundPlayFromAssetFolder(Context context, String assetSoundPath) {
    try {
        AssetFileDescriptor afd = context.getAssets().openFd(assetSoundPath);

        MediaPlayer player = new MediaPlayer();
        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        player.prepare();
        player.start();

    } catch (Exception e) {
        if (LOG_ENABLE) {
            Log.e(TAG, "Error playing sound: '" + assetSoundPath + "' (" + e.getMessage() + ")", e);
        }
    }
}

From source file:com.haomee.chat.activity.ChatActivity.java

private void playBackgroundMusic(int resId) {
    try {/*from   w w w . ja va2s.  c  om*/
        if (mediaPlayer_background == null) {
            mediaPlayer_background = new MediaPlayer();
        }
        mediaPlayer_background.reset();
        AssetFileDescriptor afd = this.getResources().openRawResourceFd(resId);
        if (afd != null) {
            mediaPlayer_background.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
                    afd.getLength());
            afd.close();
            mediaPlayer_background.setLooping(false);
            mediaPlayer_background.prepare();
            mediaPlayer_background.start();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java

private void playBeep(boolean error) {

    if (!isAdded()) {
        return;/*w w  w  . ja v a 2s  .com*/
    }

    try {
        MediaPlayer beepPlayer = new MediaPlayer();

        String file = "beep.mp3";

        if (error) {
            file = "error.mp3";
        }

        AssetFileDescriptor descriptor = context.getAssets().openFd(file);
        beepPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
                descriptor.getLength());
        descriptor.close();

        beepPlayer.prepare();

        beepPlayer.start();
    } catch (Exception io) {
        //We'll manage without the beep :)
    }
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

private void playBeep(boolean error) {

    if (!isAdded()) {
        return;//from w ww .  j  a  va  2s.  c  om
    }

    try {
        MediaPlayer beepPlayer = new MediaPlayer();

        String file = "beep.mp3";

        if (error) {
            file = "error.mp3";
        }

        AssetFileDescriptor descriptor = context.getAssets().openFd(file);
        beepPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
                descriptor.getLength());
        descriptor.close();

        beepPlayer.prepare();

        beepPlayer.start();
    } catch (Exception io) {
        // We'll manage without the beep :)
    }
}