<commit_before>package com.beepscore.android.spotifystreamer;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;

import java.io.IOException;

/**
 * Created by stevebaker on 8/13/15.
 * References
 * https://developer.android.com/reference/android/media/MediaPlayer.html
 *
 * book "Android Programming", The Big Nerd Ranch Guide
 * authors <NAME>, <NAME>
 * http://www.bignerdranch.com/we-write/android-programming
 * Chapters 13-15
 * HelloMoon
 * https://github.com/beepscore/HelloMoon.git
 */
public class BSMediaPlayer
        implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnSeekCompleteListener {
        //, MediaPlayer.OnInfoListener {

    private final String LOG_TAG = BSMediaPlayer.class.getSimpleName();

    private MediaPlayer mMediaPlayer;
    public int durationMilliseconds = 0;

    private boolean isPrepared = false;
    /**
     * accessor/getter method, provides Java equivalent of "read only" access
     * http://stackoverflow.com/questions/8151342/do-we-have-a-readonly-field-in-java-which-is-set-able-within-the-scope-of-the-c
     * @return true if player is prepared to play
     */
    public boolean isPrepared() {
        return isPrepared;
    }

    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    public void play(Context c, String url) throws IllegalArgumentException, IOException {

        // prevent creation of multiple media players.
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            mMediaPlayer.setDataSource(url);
            configureMediaPlayerListeners(mMediaPlayer);

            mMediaPlayer.prepareAsync();

        } else {
            if (isPrepared) {
                start();
            }
        }
    }

    /**
     * @return duration in msec
     */
    int getDuration() {
        return mMediaPlayer.getDuration();
    }

    /**
     * @return current position in msec
     */
    public int getCurrentPosition() {
        if (mMediaPlayer == null) {
            return 0;
        } else {
            return mMediaPlayer.getCurrentPosition();
        }
    }

    private void configureMediaPlayerListeners(MediaPlayer player) {
        player.setOnErrorListener(this);
        //player.setOnInfoListener(this);
        player.setOnPreparedListener(this);
        player.setOnSeekCompleteListener(this);
        player.setOnCompletionListener(this);
    }
<commit_msg>fix: BSMediaPlayer getDuration() null pointer exception if mMediaPlayer is null<commit_after>package com.beepscore.android.spotifystreamer;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;

import java.io.IOException;

/**
 * Created by stevebaker on 8/13/15.
 * References
 * https://developer.android.com/reference/android/media/MediaPlayer.html
 *
 * book "Android Programming", The Big Nerd Ranch Guide
 * authors <NAME>, <NAME>
 * http://www.bignerdranch.com/we-write/android-programming
 * Chapters 13-15
 * HelloMoon
 * https://github.com/beepscore/HelloMoon.git
 */
public class BSMediaPlayer
        implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnSeekCompleteListener {
        //, MediaPlayer.OnInfoListener {

    private final String LOG_TAG = BSMediaPlayer.class.getSimpleName();

    private MediaPlayer mMediaPlayer;
    public int durationMilliseconds = 0;

    private boolean isPrepared = false;
    /**
     * accessor/getter method, provides Java equivalent of "read only" access
     * http://stackoverflow.com/questions/8151342/do-we-have-a-readonly-field-in-java-which-is-set-able-within-the-scope-of-the-c
     * @return true if player is prepared to play
     */
    public boolean isPrepared() {
        return isPrepared;
    }

    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    public void play(Context c, String url) throws IllegalArgumentException, IOException {

        // prevent creation of multiple media players.
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            mMediaPlayer.setDataSource(url);
            configureMediaPlayerListeners(mMediaPlayer);

            mMediaPlayer.prepareAsync();

        } else {
            if (isPrepared) {
                start();
            }
        }
    }

    /**
     * @return duration in msec
     * return 0 if internal media player is null
     */
    int getDuration() {
        if (mMediaPlayer == null) {
            return 0;
        } else {
            return mMediaPlayer.getDuration();
        }
    }

    /**
     * @return current position in msec
     * return 0 if internal media player is null
     */
    public int getCurrentPosition() {
        if (mMediaPlayer == null) {
            return 0;
        } else {
            return mMediaPlayer.getCurrentPosition();
        }
    }

    private void configureMediaPlayerListeners(MediaPlayer player) {
        player.setOnErrorListener(this);
        //player.setOnInfoListener(this);
        player.setOnPreparedListener(this);
        player.setOnSeekCompleteListener(this);
        player.setOnCompletionListener(this);
    }

{'fraction_non_alphanumeric': 0.0958582022065473, 'fraction_numerical': 0.00705371676614216, 'mean_word_length': 2.65016501650165, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3837328', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1501, 'n_words': 331}