Skip to content

Commit

Permalink
add oncomplition listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaydeep committed May 8, 2014
1 parent 2fc052b commit d25c60d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
56 changes: 45 additions & 11 deletions LibAudioWife/src/nl/changer/audiowife/AudioWife.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@
package nl.changer.audiowife;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.util.MonthDisplayHelper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
Expand Down Expand Up @@ -66,6 +69,11 @@ public class AudioWife {
private View mPlayButton;
private View mPauseButton;

/****
* Array to hold custom completion listeners
****/
private ArrayList<OnCompletionListener> mCompletionListeners = new ArrayList<MediaPlayer.OnCompletionListener>();

/***
* Audio URI
****/
Expand Down Expand Up @@ -242,6 +250,19 @@ public AudioWife setSeekBar(SeekBar seekbar) {
initMediaSeekBar();
return this;
}

/****
* Add custom Recorder completion listener.
Adding multiple listeners will queue up all the listners and fire them
on media playback completes.*/
public AudioWife addOnCompletionListener(MediaPlayer.OnCompletionListener listener) {

mCompletionListeners.add(listener);

return this;
}



/****
* Initialize and prepare the audio player
Expand Down Expand Up @@ -276,18 +297,25 @@ private void initPlayer(Context ctx) {
}

mMediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
// set UI when audio finished playing
int currentPlayTime = 0;
mSeekBar.setProgress((int) currentPlayTime);
updatePlaytime(currentPlayTime);
setPlayable();
}
});
.setOnCompletionListener(mOnCompletion);
}

private MediaPlayer.OnCompletionListener mOnCompletion = new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
// set UI when audio finished playing
int currentPlayTime = 0;
mSeekBar.setProgress((int) currentPlayTime);
updatePlaytime(currentPlayTime);
setPlayable();
// ensure that our completion listener fires first.
// This will provide the developer to over-ride our
// completion listener functionality

fireCustomCompletionListeners(mp);
}
};

private void initMediaSeekBar() {

Expand Down Expand Up @@ -315,6 +343,12 @@ public void onProgressChanged(SeekBar seekBar, int progress,
});
}

private void fireCustomCompletionListeners(MediaPlayer mp) {
for (OnCompletionListener listener : mCompletionListeners) {
listener.onCompletion(mp);
}
}

/***
* Releases the allocated resources.
*
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ Permission required to play audio

Why the name 'AudioWife'?
=========================
This relates with yet another Android AudioRecorder library project that is coming soon.
So thought of it as analogous to a married couple where the wife is an active Player, hence AudioWife
for Audio Player and husband being a Listener, hence AudioHusband for Audio Recorder.

This relates with another Android AudioRecorder library project that is coming soon.
The name AudioWife comes from an analogy of a married couple where the wife is an active Player, hence AudioWife
for Audio Player and husband being a Listener hence AudioHusband for Audio Recorder.

Contributing
=========================
Expand Down

0 comments on commit d25c60d

Please sign in to comment.