Permalink
Browse files

adding ability to observer and stop transient player

  • Loading branch information...
1 parent 87f04e1 commit 5642a824ab106eb361e053c825a16a7e1a0e9b80 @rebeccanesson rebeccanesson committed May 18, 2012
View
2 .gitignore
@@ -1,3 +1,5 @@
bin
gen
gen
+.settings/org.eclipse.jdt.core.prefs
+.settings/*
View
8 src/org/prx/android/playerhater/PlaybackService.java
@@ -191,12 +191,12 @@ public boolean play() throws IllegalStateException, IOException {
}
- public void transientPlay(FileDescriptor file, boolean isDuckable) {
- TransientPlayer.play(this, file, isDuckable);
+ public TransientPlayer transientPlay(FileDescriptor file, boolean isDuckable) {
+ return TransientPlayer.play(this, file, isDuckable);
}
- public void transientPlay(String url, boolean isDuckable) {
- TransientPlayer.play(this, url, isDuckable);
+ public TransientPlayer transientPlay(String url, boolean isDuckable) {
+ return TransientPlayer.play(this, url, isDuckable);
}
private void reset() {
View
8 src/org/prx/android/playerhater/PlayerHater.java
@@ -34,10 +34,10 @@
boolean play(Uri url, Activity activityTriggeredOnNotificationTouch) throws IllegalStateException, IllegalArgumentException, SecurityException, IOException;
boolean play(Uri url, Activity activityTriggeredOnNotificationTouch, int notificationView) throws IllegalStateException, IllegalArgumentException, SecurityException, IOException;
- void transientPlay(String fileOrUrl);
- void transientPlay(String fileOrUrl, boolean isDuckable);
- void transientPlay(FileDescriptor file);
- void transientPlay(FileDescriptor file, boolean isDuckable);
+ TransientPlayer transientPlay(String fileOrUrl);
+ TransientPlayer transientPlay(String fileOrUrl, boolean isDuckable);
+ TransientPlayer transientPlay(FileDescriptor file);
+ TransientPlayer transientPlay(FileDescriptor file, boolean isDuckable);
void setNotificationIntentActivity(Activity activityTriggeredOnNotificationTouch);
void setNotificationIcon(int notificationIcon);
View
21 src/org/prx/android/playerhater/PlayerHaterBinder.java
@@ -217,33 +217,32 @@ public void commitBundle(Bundle icicle) {
}
@Override
- public void transientPlay(String fileOrUrl) {
- transientPlay(fileOrUrl, false);
+ public TransientPlayer transientPlay(String fileOrUrl) {
+ return transientPlay(fileOrUrl, false);
}
@Override
- public void transientPlay(String fileOrUrl, boolean isDuckable) {
+ public TransientPlayer transientPlay(String fileOrUrl, boolean isDuckable) {
if (fileOrUrl.charAt(0) == '/') {
try {
- transientPlay(
+ return transientPlay(
(new FileInputStream(new File(fileOrUrl))).getFD(),
isDuckable);
} catch (Exception e) {
e.printStackTrace();
}
- } else {
- mService.transientPlay(fileOrUrl, isDuckable);
- }
+ }
+ return mService.transientPlay(fileOrUrl, isDuckable);
}
@Override
- public void transientPlay(FileDescriptor file) {
- transientPlay(file, false);
+ public TransientPlayer transientPlay(FileDescriptor file) {
+ return transientPlay(file, false);
}
@Override
- public void transientPlay(FileDescriptor file, boolean isDuckable) {
- mService.transientPlay(file, isDuckable);
+ public TransientPlayer transientPlay(FileDescriptor file, boolean isDuckable) {
+ return mService.transientPlay(file, isDuckable);
}
@Override
View
56 src/org/prx/android/playerhater/TransientPlayer.java
@@ -5,15 +5,16 @@
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
+import android.util.Log;
public class TransientPlayer {
- public static void play(Context c, String url, boolean isDuckable) {
- new TransientPlayer(c, url, isDuckable).play();
+ public static TransientPlayer play(Context c, String url, boolean isDuckable) {
+ return new TransientPlayer(c, url, isDuckable).play();
}
- public static void play(Context c, FileDescriptor file, boolean isDuckable) {
- new TransientPlayer(c, file, isDuckable).play();
+ public static TransientPlayer play(Context c, FileDescriptor file, boolean isDuckable) {
+ return new TransientPlayer(c, file, isDuckable).play();
}
private final Context c;
@@ -24,6 +25,19 @@ public static void play(Context c, FileDescriptor file, boolean isDuckable) {
private final int playType;
private static final int FILE = 1;
private static final int URL = 2;
+ private static final String TAG = "TransientPlayer";
+
+ final AudioManager audioManager;
+
+ final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
+ @Override
+ public void onAudioFocusChange(int focusChange) {
+ // I don't think we need to do anything here.
+ // generally, we might want to stop the audio in
+ // the event that we have lost focus in a non-
+ // transient way, I think.
+ }
+ };
protected TransientPlayer(Context c, String url, boolean isDuckable) {
this.c = c;
@@ -32,6 +46,7 @@ protected TransientPlayer(Context c, String url, boolean isDuckable) {
this.url = url;
this.isDuckable = isDuckable;
this.playType = URL;
+ this.audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
}
protected TransientPlayer(Context c, FileDescriptor file, boolean isDuckable) {
@@ -41,12 +56,31 @@ protected TransientPlayer(Context c, FileDescriptor file, boolean isDuckable) {
this.file = file;
this.isDuckable = isDuckable;
this.playType = FILE;
+ this.audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
}
- private void play() {
+ private TransientPlayer play() {
TransientPlayerTask task = new TransientPlayerTask();
Thread thread = new Thread(task);
thread.run();
+ return this;
+ }
+
+ public void stop() {
+ try {
+ wrapper.stop();
+ audioManager.abandonAudioFocus(audioFocusListener);
+ wrapper.release();
+ } catch (Exception e) {
+
+ }
+ }
+
+ public boolean isPlaying() {
+ return (wrapper.getState() == MediaPlayerWrapper.STARTED ||
+ wrapper.getState() == MediaPlayerWrapper.INITIALIZED ||
+ wrapper.getState() == MediaPlayerWrapper.PREPARED ||
+ wrapper.getState() == MediaPlayerWrapper.PREPARING);
}
private int getDurationHint() {
@@ -79,18 +113,6 @@ public void run() {
return;
}
- final AudioManager audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
-
- final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
- @Override
- public void onAudioFocusChange(int focusChange) {
- // I don't think we need to do anything here.
- // generally, we might want to stop the audio in
- // the event that we have lost focus in a non-
- // transient way, I think.
- }
- };
-
wrapper.setOnCompletionListener(new OnCompletionListener() {
@Override

0 comments on commit 5642a82

Please sign in to comment.