Example usage for android.content.res AssetFileDescriptor getStartOffset

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

Introduction

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

Prototype

public long getStartOffset() 

Source Link

Document

Returns the byte offset where this asset entry's data starts.

Usage

From source file:com.intel.xdk.player.Player.java

public void startAudio(String strRelativeFileURL, boolean shouldLoop) {
    if (isPlayingPodcast()) {
        injectJS(/*from  ww w.j a  va  2 s  . c o m*/
                "javascript:var ev = document.createEvent('Events');ev.initEvent('intel.xdk.player.audio.busy',true,true);document.dispatchEvent(ev);");
        return;
    } else if (isPlayingAudio()) {
        stopAudio();
    }

    soundName = strRelativeFileURL;
    Object path = pathToFile(strRelativeFileURL);

    try {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.release();
                mediaPlayer = null;
            }
        } catch (Exception e) {
        }
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setLooping(shouldLoop);
        mediaPlayer.setOnErrorListener(soundOnError);
        mediaPlayer.setOnCompletionListener(soundOnComplete);

        //set data source based on type
        if (path instanceof AssetFileDescriptor) {
            try {
                AssetFileDescriptor afd = ((AssetFileDescriptor) path);
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            File file = new File((String) path);
            try {
                FileInputStream fis = new FileInputStream(file);
                mediaPlayer.setDataSource(fis.getFD());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        mediaPlayer.prepare();
        mediaPlayer.seekTo(audioTime);
        mediaPlayer.setVolume(audioVolume, audioVolume);
        mediaPlayer.start();
    } catch (Exception e) {
        injectJS(
                "javascript:var ev = document.createEvent('Events');ev.initEvent('intel.xdk.player.audio.error',true,true);document.dispatchEvent(ev);");
        return;
    }

    //activity.trackPageView("/intel.xdk.podcast." + getAudioName(soundName) + ".stop");

    setPlayingAudio(true);
    injectJS(
            "javascript:var ev = document.createEvent('Events');ev.initEvent('intel.xdk.player.audio.start');document.dispatchEvent(ev);");
}

From source file:android.widget.TiVideoView4.java

private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;/*from   w ww  .ja  v  a2  s . co  m*/
    }
    // Tell the music playback service to pause
    // TODO: these constants need to be published somewhere in the framework.
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "pause");
    getContext().sendBroadcast(i);

    if (mMediaPlayer != null) {
        mMediaPlayer.reset();
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mIsPrepared = false;
        Log.v(TAG, "reset duration to -1 in openVideo");
        mDuration = -1;
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        if (URLUtil.isAssetUrl(mUri.toString())) { // DST: 20090606 detect asset url
            AssetFileDescriptor afd = null;
            try {
                String path = mUri.toString().substring("file:///android_asset/".length());
                afd = getContext().getAssets().openFd(path);
                mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            } finally {
                if (afd != null) {
                    afd.close();
                }
            }
        } else {
            setDataSource();
        }
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        return;
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        return;
    }
}

From source file:com.commontime.cordova.audio.AudioPlayer.java

/**
 * load audio file// w w w  . j av a  2  s . co  m
 * @throws IOException
 * @throws IllegalStateException
 * @throws SecurityException
 * @throws IllegalArgumentException
 */
private void loadAudioFile(String file)
        throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
    if (this.isStreaming(file)) {
        this.player.setDataSource(file);
        this.player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //if it's a streaming file, play mode is implied
        this.setMode(MODE.PLAY);
        this.setState(STATE.MEDIA_STARTING);
        this.player.setOnPreparedListener(this);
        this.player.prepareAsync();
    } else {
        if (file.startsWith("/android_asset/")) {
            String f = file.substring(15);
            android.content.res.AssetFileDescriptor fd = this.handler.cordova.getActivity().getAssets()
                    .openFd(f);
            this.player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        } else {
            File fp = new File(file);
            if (fp.exists()) {
                FileInputStream fileInputStream = new FileInputStream(file);
                this.player.setDataSource(fileInputStream.getFD());
                fileInputStream.close();
            } else {
                this.player.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/" + file);
            }
        }
        this.setState(STATE.MEDIA_STARTING);
        this.player.setOnPreparedListener(this);
        this.player.prepare();

        // Get duration
        this.duration = getDurationInSeconds();
    }
}

From source file:com.kjsaw.alcosys.ibacapp.IBAC.java

private void initBeepSound() {
    if (mediaPlayerBeep == null) {
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mediaPlayerBeep = new MediaPlayer();
        mediaPlayerBeep.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayerBeep.setOnCompletionListener(beepListener);

        AssetFileDescriptor fileBeep = getResources().openRawResourceFd(R.raw.beep);
        try {//from w w  w . j av a2s.  c  o  m
            mediaPlayerBeep.setDataSource(fileBeep.getFileDescriptor(), fileBeep.getStartOffset(),
                    fileBeep.getLength());
            fileBeep.close();
            mediaPlayerBeep.setVolume(BEEP_VOLUME, BEEP_VOLUME);
            mediaPlayerBeep.prepare();
        } catch (IOException e) {
            mediaPlayerBeep = null;
        }
    }
}

From source file:fr.bde_eseo.eseomega.GantierActivity.java

@Override
public void onResume() {
    super.onResume();

    /*/*from   ww w . j a  va  2  s .  c  o m*/
    senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_GAME);*/
    initListeners();

    // wait for one second until gyroscope and magnetometer/accelerometer
    // data is initialised then scedule the complementary filter task
    fuseTimer.scheduleAtFixedRate(new calculateFusedOrientationTask(), 1000, TIME_CONSTANT);

    // if not playing
    if (!mediaPlayer.isPlaying()) {
        AssetFileDescriptor descriptor = null;
        try {
            descriptor = getAssets().openFd("wabe.mp3");
            mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
                    descriptor.getLength());
            descriptor.close();
            mediaPlayer.prepare();
            mediaPlayer.setLooping(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Start music !
    mediaPlayer.start();
}

From source file:com.kjsaw.alcosys.ibacapp.IBAC.java

private void initCaptureSound() {
    if (mediaPlayerCapture == null) {
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mediaPlayerCapture = new MediaPlayer();
        mediaPlayerCapture.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayerCapture.setOnCompletionListener(captureListener);

        AssetFileDescriptor fileBeep = getResources().openRawResourceFd(R.raw.camera_flash);
        try {//from  www  .j ava  2  s  .  c  o m
            mediaPlayerCapture.setDataSource(fileBeep.getFileDescriptor(), fileBeep.getStartOffset(),
                    fileBeep.getLength());
            fileBeep.close();
            mediaPlayerCapture.setVolume(CAPTURE_VOLUME, CAPTURE_VOLUME);
            mediaPlayerCapture.prepare();
        } catch (IOException e) {
            mediaPlayerCapture = null;
        }
    }
}

From source file:org.hermes.android.NotificationsController.java

public void playOutChatSound() {
    if (!inChatSoundEnabled) {
        return;//from  ww w .  j  av a  2s  .c  o  m
    }
    try {
        if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
            return;
        }
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
    notificationsQueue.postRunnable(new Runnable() {
        @Override
        public void run() {
            try {
                if (mediaPlayerOut == null) {
                    AssetFileDescriptor assetFileDescriptor = ApplicationLoader.applicationContext
                            .getResources().openRawResourceFd(R.raw.sound_out);
                    if (assetFileDescriptor != null) {
                        mediaPlayerOut = new MediaPlayer();
                        mediaPlayerOut.setAudioStreamType(AudioManager.STREAM_SYSTEM);
                        mediaPlayerOut.setDataSource(assetFileDescriptor.getFileDescriptor(),
                                assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
                        mediaPlayerOut.setLooping(false);
                        assetFileDescriptor.close();
                        mediaPlayerOut.prepare();
                    }
                }
                mediaPlayerOut.start();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    });
}

From source file:org.hermes.android.NotificationsController.java

private void playInChatSound() {
    if (!inChatSoundEnabled) {
        return;//from ww  w . j  a v  a  2  s . c o  m
    }
    try {
        if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
            return;
        }
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }

    try {
        SharedPreferences preferences = ApplicationLoader.applicationContext
                .getSharedPreferences("Notifications", Context.MODE_PRIVATE);
        int notify_override = preferences.getInt("notify2_" + openned_dialog_id, 0);
        if (notify_override == 3) {
            int mute_until = preferences.getInt("notifyuntil_" + openned_dialog_id, 0);
            if (mute_until >= ConnectionsManager.getInstance().getCurrentTime()) {
                notify_override = 2;
            }
        }
        if (notify_override == 2) {
            return;
        }
        notificationsQueue.postRunnable(new Runnable() {
            @Override
            public void run() {
                if (lastSoundPlay > System.currentTimeMillis() - 500) {
                    return;
                }
                try {
                    if (mediaPlayerIn == null) {
                        AssetFileDescriptor assetFileDescriptor = ApplicationLoader.applicationContext
                                .getResources().openRawResourceFd(R.raw.sound_in);
                        if (assetFileDescriptor != null) {
                            mediaPlayerIn = new MediaPlayer();
                            mediaPlayerIn.setAudioStreamType(AudioManager.STREAM_SYSTEM);
                            mediaPlayerIn.setDataSource(assetFileDescriptor.getFileDescriptor(),
                                    assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
                            mediaPlayerIn.setLooping(false);
                            assetFileDescriptor.close();
                            mediaPlayerIn.prepare();
                        }
                    }
                    mediaPlayerIn.start();
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
        });
        /*String choosenSoundPath = null;
        String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
                
        choosenSoundPath = preferences.getString("sound_path_" + openned_dialog_id, null);
        boolean isChat = (int)(openned_dialog_id) < 0;
        if (isChat) {
        if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
            choosenSoundPath = null;
        } else if (choosenSoundPath == null) {
            choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
        }
        } else {
        if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
            choosenSoundPath = null;
        } else if (choosenSoundPath == null) {
            choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
        }
        }
                
        if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
        if (lastMediaPlayerUri == null || !choosenSoundPath.equals(lastMediaPlayerUri)) {
            lastMediaPlayerUri = choosenSoundPath;
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
            if (choosenSoundPath.equals(defaultPath)) {
                mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Settings.System.DEFAULT_NOTIFICATION_URI);
            } else {
                mediaPlayer.setDataSource(ApplicationLoader.applicationContext, Uri.parse(choosenSoundPath));
            }
            mediaPlayer.prepare();
        }
        mediaPlayer.start();
        }*/
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

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  .j av  a  2  s . c o 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  w  w  w. j a  va  2s. co  m
 * @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);
        }
    }
}