Skip to content

Commit

Permalink
add demo code for custom click listeners. Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydeepw committed May 10, 2014
1 parent b605450 commit 6ac188a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 37 deletions.
51 changes: 33 additions & 18 deletions AudioWifeDemo/src/nl/changer/audiowifedemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import nl.changer.audiowife.AudioWife;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
Expand Down Expand Up @@ -51,24 +52,10 @@ public void onClick(View v) {
@Override
public void onClick(View v) {

try {
AudioWife.getInstance().play();
} catch (IllegalStateException e) {
if (mUri == null)
Toast.makeText(MainActivity.this,
"Pick an audio file before playing",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}

}
});

mPauseMedia.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
AudioWife.getInstance().pause();
}
});
}
Expand All @@ -89,10 +76,37 @@ protected void onActivityResult(int requestCode, int resuleCode,
if (requestCode == INTENT_PICK_AUDIO) {
Uri uri = intent.getData();

mUri = uri;

AudioWife.getInstance().init(MainActivity.this, uri)
.setPlayView(mPlayMedia).setPauseView(mPauseMedia)
.setSeekBar(mMediaSeekBar).setPlaytime(mPlaybackTime);
// .play();
.setPlayView(mPlayMedia) // AudioWife takes care of click handler for play button
.setPauseView(mPauseMedia) // AudioWife takes care of click handler for pause button
.setSeekBar(mMediaSeekBar)
.setPlaytime(mPlaybackTime);

AudioWife.getInstance().addOnCompletionListener( new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(getBaseContext(), "Completed", Toast.LENGTH_SHORT).show();
}
});

AudioWife.getInstance().addOnPlayClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Play", Toast.LENGTH_SHORT).show();
}
});

AudioWife.getInstance().addOnPauseClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Pause", Toast.LENGTH_SHORT).show();
}
});
}
} else {
Log.w(TAG, "Audio file not picked up");
Expand All @@ -105,5 +119,6 @@ protected void onPause() {

// when done playing, release the resources
AudioWife.getInstance().release();
mUri = null;
}
}
62 changes: 56 additions & 6 deletions LibAudioWife/src/nl/changer/audiowife/AudioWife.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class AudioWife {
private ArrayList<OnCompletionListener> mCompletionListeners = new ArrayList<MediaPlayer.OnCompletionListener>();

private ArrayList<View.OnClickListener> mPlayListeners = new ArrayList<View.OnClickListener>();

private ArrayList<View.OnClickListener> mPauseListeners = new ArrayList<View.OnClickListener>();

/***
* Audio URI
Expand Down Expand Up @@ -132,6 +134,9 @@ public void play() {
*/
public void pause() {

if(mMediaPlayer == null)
return;

if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
setPlayable();
Expand Down Expand Up @@ -235,7 +240,9 @@ private void initOnPlayClick() {
if(mPlayButton == null)
throw new NullPointerException("Play view cannot be null");

mPlayListeners.add(new View.OnClickListener() {
// add default click listener to the top
// so that it is the one that gets fired first
mPlayListeners.add(0, new View.OnClickListener() {

@Override
public void onClick(View v) {
Expand Down Expand Up @@ -263,8 +270,37 @@ public void onClick(View v) {
****/
public AudioWife setPauseView(View pause) {
mPauseButton = pause;

initOnPauseClick();
return this;
}

private void initOnPauseClick() {
if(mPauseButton == null)
throw new NullPointerException("Pause view cannot be null");

// add default click listener to the top
// so that it is the one that gets fired first
mPauseListeners.add(0, new View.OnClickListener() {

@Override
public void onClick(View v) {
pause();
}
});

// Fire all the attached listeners
// when the pause button is actually clicked
mPauseButton.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View v) {
for (View.OnClickListener listener : mPauseListeners) {
listener.onClick(v);
}
}
});
}

/***
* Set current playback time. Use this if you have a playback time counter
Expand All @@ -285,19 +321,21 @@ public AudioWife setSeekBar(SeekBar seekbar) {
}

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

mCompletionListeners.add(listener);

// add default click listener to the top
// so that it is the one that gets fired first
mCompletionListeners.add(0 ,listener);

return this;
}

/****
* Add custom play button click listener. Adding multiple listeners will
* Add custom play view click listener. Adding multiple listeners will
* queue up all the listners and fire them all together when the event occurs.
*/
public AudioWife addOnPlayClickListener(
Expand All @@ -308,6 +346,18 @@ public AudioWife addOnPlayClickListener(
return this;
}

/****
* Add custom pause view click listener. Adding multiple listeners will
* queue up all the listners and fire them all together when the event occurs.
*/
public AudioWife addOnPauseClickListener(
View.OnClickListener listener) {

mPauseListeners.add(listener);

return this;
}

/****
* Initialize and prepare the audio player
****/
Expand Down
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,19 @@ Getting started
====================
```java

// inflate your audio player view or have one in the existing UI already.
ViewGroup yourAudioPlayerView = (ViewGroup) mLayoutInflator.inflate(R.layout.playback_audio, mMediaPlayerContainer);

// initialize the player contols
Button mPlayMedia = (Button) yourAudioPlayerView.findViewById(R.id.play);
Button mPauseMedia = (Button) yourAudioPlayerView.findViewById(R.id.pause);
SeekBar mMediaSeekBar = (SeekBar) yourAudioPlayerView.findViewById(R.id.mediaSeekBar);
TextView mPlaybackTime = (TextView) yourAudioPlayerView.findViewById(R.id.playback_time);
mPlayMedia = findViewById(R.id.play);
mPauseMedia = findViewById(R.id.pause);
mMediaSeekBar = (SeekBar) findViewById(R.id.mediaSeekBar);
mPlaybackTime = (TextView) findViewById(R.id.playback_time);

// initialize AudioWife
// and play
AudioWife.getInstance()
.init(mContext, mUri)
.setPlayView(mPlayMedia)
.setPauseView(mPauseMedia)
.init(MainActivity.this, uri)
.setPlayView(mPlayMedia) // AudioWife takes care of click handler for play button
.setPauseView(mPauseMedia) // AudioWife takes care of click handler for pause button
.setSeekBar(mMediaSeekBar)
.setPlaytime(mPlaybackTime)
.play();
.setPlaytime(mPlaybackTime);

// to pause
AudioWife.getInstance().pause();
Expand Down

0 comments on commit 6ac188a

Please sign in to comment.