Android Open Source - notes Audio Player






From Project

Back to project page notes.

License

The source code is released under:

Apache License

If you think the Android project notes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.donnemartin.android.notes.notes;
//from   w  ww  . j a va 2 s.c  o m
import android.media.MediaPlayer;
import android.util.Log;

import java.io.IOException;

public class AudioPlayer {

    private MediaPlayer mPlayer;
    private static String mFileName;

    private static final String LOG_TAG = "AudioPlayer";
    public static final int PLAY_FROM_START = 0;

    public AudioPlayer(String fileName) {
        mFileName = fileName;
    }

    public boolean validMediaPlayer() {
        boolean isValidMediaPlayer = false;

        if (mPlayer != null) {
            isValidMediaPlayer = true;
        }

        return isValidMediaPlayer;
    }

    public boolean isPlaying() {
        boolean isPlaying = false;

        if (mPlayer != null && mPlayer.isPlaying()) {
            isPlaying = true;
        }

        return isPlaying;
    }

    public void stop() {
        if (mPlayer != null) {
            // We don't want to hold onto the audio decoder hardware and
            // other system resources, so call release instead of stop
            mPlayer.release();
            mPlayer = null;
        }
    }

    public void play(int position) {
        // Keep exactly one MediaPlayer around and keep it
        // around only as long as it is playing something
        // Call stop here and set a listener to call stop() when the
        // audio file has finished playing
        stop();

        mPlayer = new MediaPlayer();

        mPlayer.setOnCompletionListener(
            new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO: Notify that audio has completed
                // so we can change the button text
                stop();
            }
        });

        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.seekTo(position);
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    public int getCurrentPosition()
    {
        int position = 0;

        if (mPlayer != null) {
            position = mPlayer.getCurrentPosition();
        }

        return position;
    }
}




Java Source Code List

com.donnemartin.android.notes.notes.AudioPlayer.java
com.donnemartin.android.notes.notes.AudioRecorder.java
com.donnemartin.android.notes.notes.DatePickerFragment.java
com.donnemartin.android.notes.notes.ImageFragment.java
com.donnemartin.android.notes.notes.NoteCameraActivity.java
com.donnemartin.android.notes.notes.NoteCameraFragment.java
com.donnemartin.android.notes.notes.NoteFragment.java
com.donnemartin.android.notes.notes.NoteIntentJSONSerializer.java
com.donnemartin.android.notes.notes.NoteListActivity.java
com.donnemartin.android.notes.notes.NoteListFragment.java
com.donnemartin.android.notes.notes.NotePagerActivity.java
com.donnemartin.android.notes.notes.Note.java
com.donnemartin.android.notes.notes.Notebook.java
com.donnemartin.android.notes.notes.Photo.java
com.donnemartin.android.notes.notes.PictureUtils.java
com.donnemartin.android.notes.notes.SingleFragmentActivity.java