Example usage for android.media MediaPlayer release

List of usage examples for android.media MediaPlayer release

Introduction

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

Prototype

public void release() 

Source Link

Document

Releases resources associated with this MediaPlayer object.

Usage

From source file:Main.java

public static void playAudio(Context context, int resId) {

    if (context == null)
        throw new NullPointerException("context cannot be null");

    mediaPlayer = MediaPlayer.create(context, resId);
    mediaPlayer.start();/*w  ww.j a  v a2s.  c  om*/
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
}

From source file:Main.java

public static void stopPlaying(MediaPlayer mediaPlayer) {
    mediaPlayer.stop();
    mediaPlayer.release();
}

From source file:Main.java

public static void stopPlayback(MediaPlayer mPlayer) {
    if (mPlayer != null) {
        mPlayer.release();
    }
}

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 w w.ja  va2  s  . c om*/
    int duration = create.getDuration() / 1000;
    create.reset();
    create.release();
    return duration;
}

From source file:Main.java

public static void playSoundfile(Context context, int R_raw_fileid) {
    MediaPlayer mp = MediaPlayer.create(context, R_raw_fileid);
    mp.setOnCompletionListener(new OnCompletionListener() {
        @Override//  w  w w. j a  va2 s.c o  m
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
    mp.start();
}

From source file:Main.java

public static boolean is3gpFileAudio(String url) {
    int height = 0;
    File mediaFile = new File(url);
    try {//  w  w w.j  a v a 2 s .c o  m
        MediaPlayer mp = new MediaPlayer();
        FileInputStream fs;
        FileDescriptor fd;
        fs = new FileInputStream(mediaFile);
        fd = fs.getFD();
        mp.setDataSource(fd);
        mp.prepare();
        height = mp.getVideoHeight();
        mp.release();
    } catch (Exception e) {
        Log.e("KKIM", "Exception trying to determine if 3gp file is video.", e);
    }
    Log.i("KKIM", "The height of the file is " + height);
    return height == 0;
}

From source file:com.cw.litenote.note.AudioUi_note.java

public static void initAudioProgress(AppCompatActivity act, String audioUriInDB, ViewPager _pager) {
    SeekBar seekBar = (SeekBar) act.findViewById(R.id.pager_img_audio_seek_bar);
    ImageView mPager_audio_play_button = (ImageView) act.findViewById(R.id.pager_btn_audio_play);

    // set audio block listeners
    setAudioBlockListener(act, audioUriInDB, _pager);

    mProgress = 0;// ww  w . jav a 2s.c om

    mAudioUriInDB = audioUriInDB;
    showAudioName(act);

    TextView audioTitle = (TextView) act.findViewById(R.id.pager_audio_title);
    audioTitle.setSelected(false);
    mPager_audio_play_button.setImageResource(R.drawable.ic_media_play);
    audioTitle.setTextColor(ColorSet.getPauseColor(act));
    audioTitle.setSelected(false);

    // current position
    int curHour = Math.round((float) (mProgress / 1000 / 60 / 60));
    int curMin = Math.round((float) ((mProgress - curHour * 60 * 60 * 1000) / 1000 / 60));
    int curSec = Math.round((float) ((mProgress - curHour * 60 * 60 * 1000 - curMin * 60 * 1000) / 1000));
    String curr_pos_str = String.format(Locale.ENGLISH, "%2d", curHour) + ":"
            + String.format(Locale.ENGLISH, "%02d", curMin) + ":"
            + String.format(Locale.ENGLISH, "%02d", curSec);

    TextView audio_curr_pos = (TextView) act.findViewById(R.id.pager_audio_current_pos);
    audio_curr_pos.setText(curr_pos_str);
    audio_curr_pos.setTextColor(ColorSet.color_white);

    // audio seek bar
    seekBar.setProgress(mProgress); // This math construction give a percentage of "was playing"/"song length"
    seekBar.setMax(99); // It means 100% .0-99
    seekBar.setVisibility(View.VISIBLE);

    // get audio file length
    try {
        if (Util.isUriExisted(mAudioUriInDB, act)) {
            MediaPlayer mp = MediaPlayer.create(act, Uri.parse(mAudioUriInDB));
            mediaFileLength = mp.getDuration();
            mp.release();
        }
    } catch (Exception e) {
        System.out.println("AudioUi_note / _initAudioProgress / exception");
    }
    // set audio file length
    int fileHour = Math.round((float) (mediaFileLength / 1000 / 60 / 60));
    int fileMin = Math.round((float) ((mediaFileLength - fileHour * 60 * 60 * 1000) / 1000 / 60));
    int fileSec = Math
            .round((float) ((mediaFileLength - fileHour * 60 * 60 * 1000 - fileMin * 1000 * 60) / 1000));

    String strHour = String.format(Locale.ENGLISH, "%2d", fileHour);
    String strMinute = String.format(Locale.ENGLISH, "%02d", fileMin);
    String strSecond = String.format(Locale.ENGLISH, "%02d", fileSec);
    String strLength = strHour + ":" + strMinute + ":" + strSecond;

    TextView audio_length = (TextView) act.findViewById(R.id.pager_audio_file_length);
    audio_length.setText(strLength);
    audio_length.setTextColor(ColorSet.color_white);
}

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 v a  2  s .  co 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:Main.java

public static void playSound(Context context, Uri uri) {
    final MediaPlayer player = new MediaPlayer();
    try {/* w  w w . j  a  v  a2 s  .c  om*/
        player.setDataSource(context.getApplicationContext(), uri);
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
            }
        });
        player.prepareAsync();
    } catch (Exception e) {
    }
}

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;
    }//from  w w w . j  ava 2s .  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;
}