Example usage for android.media MediaPlayer reset

List of usage examples for android.media MediaPlayer reset

Introduction

In this page you can find the example usage for android.media MediaPlayer reset.

Prototype

public void reset() 

Source Link

Document

Resets the MediaPlayer to its uninitialized state.

Usage

From source file:Main.java

public static int m6967a(Context context, String str) {
    MediaPlayer create = MediaPlayer.create(context, Uri.parse(str));
    if (create == null) {
        return 0;
    }/*  w ww.java 2 s  .  c  o m*/
    int duration = create.getDuration() / 1000;
    create.reset();
    create.release();
    return duration;
}

From source file:Main.java

/**
 * Get video's duration without {@link ContentProvider}. Because not know
 * {@link Uri} of video./*from   w  w w  .  j  a va  2s.  c o m*/
 *
 * @param context
 * @param path    Path of video file.
 * @return Duration of video, in milliseconds. Return 0 if path is null.
 */
public static long getDuration(Context context, String path) {
    MediaPlayer mMediaPlayer = null;
    long duration = 0;
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(context, Uri.parse(path));
        mMediaPlayer.prepare();
        duration = mMediaPlayer.getDuration();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    return duration;
}

From source file:com.visva.voicerecorder.utils.Utils.java

public static long getDurationTimeFromFile(String filePath) {
    if (StringUtility.isEmpty(filePath)) {
        AIOLog.e(MyCallRecorderConstant.TAG, "filePath is null");
        return 0;
    }// w  w  w . ja  v a 2 s .c  o m
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        Log.d("KieuThang", "file it not exitsted!");
    }
    MediaPlayer mediaPlayer = new MediaPlayer();
    long duration = 0L;
    try {
        mediaPlayer.setDataSource(filePath);
        mediaPlayer.prepare();
        duration = mediaPlayer.getDuration();
        mediaPlayer.reset();
        mediaPlayer.release();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //the valid time we offer to save at least more than 1s
    if (duration > 1000) {
        AIOLog.d(MyCallRecorderConstant.TAG, "InValid time:" + duration);
        return duration;
    }
    AIOLog.d(MyCallRecorderConstant.TAG, "Valid time:" + duration);
    return 0;
}

From source file:de.azapps.mirakel.helper.TaskDialogHelpers.java

public static void playbackFile(final Activity context, final FileMirakel file, final boolean loud) {
    final MediaPlayer mPlayer = new MediaPlayer();
    final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (!loud) {/*  ww w.  j  a  v a2 s.  co  m*/
        am.setSpeakerphoneOn(false);
        am.setMode(AudioManager.MODE_IN_CALL);
        context.setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    }
    try {
        mPlayer.reset();
        if (!loud) {
            mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        }
        mPlayer.setDataSource(file.getFileStream(context).getFD());
        mPlayer.prepare();
        mPlayer.start();
        mPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(final MediaPlayer mp) {
                audio_playback_dialog.dismiss();
            }
        });
        am.setMode(AudioManager.MODE_NORMAL);
        audio_playback_playing = true;
    } catch (final IOException e) {
        Log.e(TAG, "prepare() failed");
    }
    audio_playback_dialog = new AlertDialog.Builder(context).setTitle(R.string.audio_playback_title)
            .setPositiveButton(R.string.audio_playback_pause, null)
            .setNegativeButton(R.string.audio_playback_stop, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    mPlayer.release();
                }
            }).setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(final DialogInterface dialog) {
                    mPlayer.release();
                    dialog.cancel();
                }
            }).create();
    audio_playback_dialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(final DialogInterface dialog) {
            final Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    if (audio_playback_playing) {
                        button.setText(R.string.audio_playback_play);
                        mPlayer.pause();
                        audio_playback_playing = false;
                    } else {
                        button.setText(R.string.audio_playback_pause);
                        mPlayer.start();
                        audio_playback_playing = true;
                    }
                }
            });
        }
    });
    audio_playback_dialog.show();
}

From source file:com.dudka.rich.streamingmusicplayer.ServiceMusicPlayer.java

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    mp.reset();
    mp.stop();//  ww  w  .  j ava2 s.  c  om
    mp.release();
    sendLocalBroadcast(MainActivity.PLAYER_ERROR);
    stopSelf();
    return false;
}

From source file:com.meiste.tempalarm.ui.Alarm.java

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

    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    // do not play alarms if stream volume is 0 (typically because ringer mode is silent).
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        final Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(final MediaPlayer mp, final int what, final int extra) {
                Timber.e("Error occurred while playing audio.");
                mp.stop();/*from ww w  .j a  va  2s. com*/
                mp.reset();
                mp.release();
                mMediaPlayer = null;
                return true;
            }
        });
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        try {
            mMediaPlayer.setDataSource(this, alert);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (final IOException e) {
            Timber.e("Failed to play alarm tone: %s", e);
        }
    }

    mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    mVibrator.vibrate(sVibratePattern, 0);

    mPlaying = true;
    mHandler.sendEmptyMessageDelayed(KILLER, ALARM_TIMEOUT);
}

From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java

private MediaPlayer initAudibleMediaPlayer(String audiblePath, int type) throws IOException {
    MediaPlayer mp = mMediaPlayers.get(type);
    if (mp == null) {
        mp = new MediaPlayer();
        mMediaPlayers.put(type, mp);//from   www  .j a v a 2s  .  c o m
    } else {
        mp.reset();
    }
    mp.setDataSource(audiblePath);
    mp.prepare();
    mp.setOnCompletionListener(mPlayCompletionListener);
    return mp;
}

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 av  a2 s .com*/
    } else {
        mp.reset();
    }
    mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    mp.prepare();
    mp.setOnCompletionListener(mPlayCompletionListener);
    return mp;
}

From source file:org.odk.collect.android.widgets.QuestionWidget.java

public QuestionWidget(Context context, FormEntryPrompt prompt) {
    super(context);

    player = new MediaPlayer();
    player.setOnCompletionListener(new OnCompletionListener() {
        @Override/* w w w.  j  a v  a2 s .  c o m*/
        public void onCompletion(MediaPlayer mediaPlayer) {
            questionMediaLayout.resetTextFormatting();
            mediaPlayer.reset();
        }

    });

    player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Timber.e("Error occured in MediaPlayer. what = %d, extra = %d", what, extra);
            return false;
        }
    });

    questionFontsize = Collect.getQuestionFontsize();
    answerFontsize = questionFontsize + 2;

    formEntryPrompt = prompt;

    setGravity(Gravity.TOP);
    setPadding(0, 7, 0, 0);

    questionMediaLayout = createQuestionMediaLayout(prompt);
    helpTextView = createHelpText(prompt);

    addQuestionMediaLayout(questionMediaLayout);
    addHelpTextView(helpTextView);
}

From source file:org.odk.collect.android.views.MediaLayout.java

/**
 * This is what gets called when the AudioButton gets clicked
 *//*from w w w.j  a  va  2s.c  om*/
@Override
public void onClick(View v) {
    if (audioPlayListener != null) {
        audioPlayListener.resetQuestionTextColor();
    }
    if (player.isPlaying()) {
        player.stop();
        Bitmap b = BitmapFactory.decodeResource(getContext().getResources(),
                android.R.drawable.ic_lock_silent_mode_off);
        audioButton.setImageBitmap(b);

    } else {
        playAudio();
        Bitmap b = BitmapFactory.decodeResource(getContext().getResources(), android.R.drawable.ic_media_pause);
        audioButton.setImageBitmap(b);
    }
    player.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            resetTextFormatting();
            mediaPlayer.reset();
            Bitmap b = BitmapFactory.decodeResource(getContext().getResources(),
                    android.R.drawable.ic_lock_silent_mode_off);
            audioButton.setImageBitmap(b);
        }
    });
}