List of usage examples for android.media MediaPlayer setDataSource
public void setDataSource(FileDescriptor fd, long offset, long length) throws IOException, IllegalArgumentException, IllegalStateException
From source file:Main.java
/** * Play video file from res folder./*www . j a v a 2s . co m*/ * Then call mediaPlayer.start(); * @param fileName * @param listener * @return * @throws Exception */ public static MediaPlayer playSound(AssetManager assetManager, String fileName, MediaPlayer.OnCompletionListener listener) throws Exception { MediaPlayer mediaPlayer = new MediaPlayer(); if (listener != null) { mediaPlayer.setOnCompletionListener(listener); } AssetFileDescriptor descriptor = assetManager.openFd(fileName); mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); descriptor.close(); mediaPlayer.prepare(); return mediaPlayer; }
From source file:com.xlythe.engine.theme.Theme.java
public static long getDurationOfSound(Context context, Theme.Res res) { int millis = 0; MediaPlayer mp = new MediaPlayer(); try {//from w w w . j a v a2s . c o m AssetFileDescriptor afd; int id = getId(context, res.getType(), res.getName()); if (id == 0) { id = context.getResources().getIdentifier(res.getName(), res.getType(), context.getPackageName()); afd = context.getResources().openRawResourceFd(id); } afd = getThemeContext(context).getResources().openRawResourceFd(id); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.prepare(); millis = mp.getDuration(); } catch (Exception e) { e.printStackTrace(); } finally { mp.release(); mp = null; } return millis; }
From source file:it.interfree.leonardoce.bootreceiver.AlarmKlaxon.java
private void setDataSourceFromResource(Resources resources, MediaPlayer player, int res) throws java.io.IOException { AssetFileDescriptor afd = resources.openRawResourceFd(res); if (afd != null) { player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close();//from w ww . j a v a2 s.c o m } }
From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java
private MediaPlayer initAudibleMediaPlayer(AssetFileDescriptor afd, int type) throws IOException { MediaPlayer mp = mMediaPlayers.get(type); if (mp == null) { mp = new MediaPlayer(); mMediaPlayers.put(type, mp);/* w w w . j a va2s. c o m*/ } else { mp.reset(); } mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mp.prepare(); mp.setOnCompletionListener(mPlayCompletionListener); return mp; }
From source file:com.android.onemedia.playback.LocalRenderer.java
@Override public void setNextContent(Bundle request) { String source = request.getString(RequestUtils.EXTRA_KEY_SOURCE); Map<String, String> headers = null; // request.mHeaders; // TODO support video if (DEBUG) {/* w w w. ja v a 2 s .com*/ Log.d(TAG, mDebugId + ": Setting next content. Have player? " + (mPlayer != null) + " have next player? " + (mNextPlayer != null)); } if (mPlayer == null) { // The manager isn't being used to play anything, don't try to // set a next. return; } if (mNextPlayer != null) { // Before setting up the new one clear out the old one and release // it to ensure it doesn't play. mPlayer.setNextMediaPlayer(null); mNextPlayer.release(); mNextPlayer = null; mNextContent = null; } if (source == null) { // If there's no new content we're done return; } final MediaPlayer newPlayer = new MediaPlayer(); try { if (headers != null) { Uri sourceUri = Uri.parse(source); newPlayer.setDataSource(mContext, sourceUri, headers); } else { newPlayer.setDataSource(source); } } catch (Exception e) { newPlayer.release(); // Don't return an error until we get to this item in playback return; } if (preparePlayer(newPlayer, false)) { mPlayer.setNextMediaPlayer(newPlayer); mNextPlayer = newPlayer; mNextContent = new PlayerContent(source, headers); } }
From source file:com.android.onemedia.playback.LocalRenderer.java
/** * Prepares the player for the given playback request. If the holder is null * it is assumed this is an audio only source. If playOnReady is set to true * the media will begin playing as soon as it can. * * @see RequestUtils for the set of valid keys. */// w w w.java 2 s . c o m public void setContent(Bundle request, SurfaceHolder holder) { String source = request.getString(RequestUtils.EXTRA_KEY_SOURCE); Map<String, String> headers = null; // request.mHeaders; boolean playOnReady = true; // request.mPlayOnReady; if (DEBUG) { Log.d(TAG, mDebugId + ": Settings new content. Have a player? " + (mPlayer != null) + " have a next player? " + (mNextPlayer != null)); } cleanUpPlayer(); setState(STATE_PREPARING); mPlayOnReady = playOnReady; mSeekOnReady = -1; final MediaPlayer newPlayer = new MediaPlayer(); requestAudioFocus(); mPlayer = newPlayer; mContent = new PlayerContent(source, headers); try { if (headers != null) { Uri sourceUri = Uri.parse(source); newPlayer.setDataSource(mContext, sourceUri, headers); } else { newPlayer.setDataSource(source); } } catch (Exception e) { setError(Listener.ERROR_LOAD_FAILED, 0, null, e); return; } if (isHolderReady(holder, newPlayer)) { preparePlayer(newPlayer, true); } }
From source file:es.javocsoft.android.lib.toolbox.ToolBox.java
/** * Plays the specified sound from the application asset folder. * //from w ww . j a v a 2s . c o 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); } } }
From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java
private void playBeep(boolean error) { if (!isAdded()) { return;/*from ww w .j a va 2s . c o m*/ } 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;/*w w w. j ava 2s . co m*/ } 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 :) } }