Skip to content
This repository
Browse code

adding ability to observer and stop transient player

  • Loading branch information...
commit 5642a824ab106eb361e053c825a16a7e1a0e9b80 1 parent 87f04e1
Rebecca Nesson rebeccanesson authored
2  .gitignore
... ... @@ -1,3 +1,5 @@
1 1 bin
2 2 gen
3 3 gen
  4 +.settings/org.eclipse.jdt.core.prefs
  5 +.settings/*
8 src/org/prx/android/playerhater/PlaybackService.java
@@ -191,12 +191,12 @@ public boolean play() throws IllegalStateException, IOException {
191 191
192 192 }
193 193
194   - public void transientPlay(FileDescriptor file, boolean isDuckable) {
195   - TransientPlayer.play(this, file, isDuckable);
  194 + public TransientPlayer transientPlay(FileDescriptor file, boolean isDuckable) {
  195 + return TransientPlayer.play(this, file, isDuckable);
196 196 }
197 197
198   - public void transientPlay(String url, boolean isDuckable) {
199   - TransientPlayer.play(this, url, isDuckable);
  198 + public TransientPlayer transientPlay(String url, boolean isDuckable) {
  199 + return TransientPlayer.play(this, url, isDuckable);
200 200 }
201 201
202 202 private void reset() {
8 src/org/prx/android/playerhater/PlayerHater.java
@@ -34,10 +34,10 @@
34 34 boolean play(Uri url, Activity activityTriggeredOnNotificationTouch) throws IllegalStateException, IllegalArgumentException, SecurityException, IOException;
35 35 boolean play(Uri url, Activity activityTriggeredOnNotificationTouch, int notificationView) throws IllegalStateException, IllegalArgumentException, SecurityException, IOException;
36 36
37   - void transientPlay(String fileOrUrl);
38   - void transientPlay(String fileOrUrl, boolean isDuckable);
39   - void transientPlay(FileDescriptor file);
40   - void transientPlay(FileDescriptor file, boolean isDuckable);
  37 + TransientPlayer transientPlay(String fileOrUrl);
  38 + TransientPlayer transientPlay(String fileOrUrl, boolean isDuckable);
  39 + TransientPlayer transientPlay(FileDescriptor file);
  40 + TransientPlayer transientPlay(FileDescriptor file, boolean isDuckable);
41 41
42 42 void setNotificationIntentActivity(Activity activityTriggeredOnNotificationTouch);
43 43 void setNotificationIcon(int notificationIcon);
21 src/org/prx/android/playerhater/PlayerHaterBinder.java
@@ -217,33 +217,32 @@ public void commitBundle(Bundle icicle) {
217 217 }
218 218
219 219 @Override
220   - public void transientPlay(String fileOrUrl) {
221   - transientPlay(fileOrUrl, false);
  220 + public TransientPlayer transientPlay(String fileOrUrl) {
  221 + return transientPlay(fileOrUrl, false);
222 222 }
223 223
224 224 @Override
225   - public void transientPlay(String fileOrUrl, boolean isDuckable) {
  225 + public TransientPlayer transientPlay(String fileOrUrl, boolean isDuckable) {
226 226 if (fileOrUrl.charAt(0) == '/') {
227 227 try {
228   - transientPlay(
  228 + return transientPlay(
229 229 (new FileInputStream(new File(fileOrUrl))).getFD(),
230 230 isDuckable);
231 231 } catch (Exception e) {
232 232 e.printStackTrace();
233 233 }
234   - } else {
235   - mService.transientPlay(fileOrUrl, isDuckable);
236   - }
  234 + }
  235 + return mService.transientPlay(fileOrUrl, isDuckable);
237 236 }
238 237
239 238 @Override
240   - public void transientPlay(FileDescriptor file) {
241   - transientPlay(file, false);
  239 + public TransientPlayer transientPlay(FileDescriptor file) {
  240 + return transientPlay(file, false);
242 241 }
243 242
244 243 @Override
245   - public void transientPlay(FileDescriptor file, boolean isDuckable) {
246   - mService.transientPlay(file, isDuckable);
  244 + public TransientPlayer transientPlay(FileDescriptor file, boolean isDuckable) {
  245 + return mService.transientPlay(file, isDuckable);
247 246 }
248 247
249 248 @Override
56 src/org/prx/android/playerhater/TransientPlayer.java
@@ -5,15 +5,16 @@
5 5 import android.media.AudioManager;
6 6 import android.media.MediaPlayer;
7 7 import android.media.MediaPlayer.OnCompletionListener;
  8 +import android.util.Log;
8 9
9 10 public class TransientPlayer {
10 11
11   - public static void play(Context c, String url, boolean isDuckable) {
12   - new TransientPlayer(c, url, isDuckable).play();
  12 + public static TransientPlayer play(Context c, String url, boolean isDuckable) {
  13 + return new TransientPlayer(c, url, isDuckable).play();
13 14 }
14 15
15   - public static void play(Context c, FileDescriptor file, boolean isDuckable) {
16   - new TransientPlayer(c, file, isDuckable).play();
  16 + public static TransientPlayer play(Context c, FileDescriptor file, boolean isDuckable) {
  17 + return new TransientPlayer(c, file, isDuckable).play();
17 18 }
18 19
19 20 private final Context c;
@@ -24,6 +25,19 @@ public static void play(Context c, FileDescriptor file, boolean isDuckable) {
24 25 private final int playType;
25 26 private static final int FILE = 1;
26 27 private static final int URL = 2;
  28 + private static final String TAG = "TransientPlayer";
  29 +
  30 + final AudioManager audioManager;
  31 +
  32 + final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
  33 + @Override
  34 + public void onAudioFocusChange(int focusChange) {
  35 + // I don't think we need to do anything here.
  36 + // generally, we might want to stop the audio in
  37 + // the event that we have lost focus in a non-
  38 + // transient way, I think.
  39 + }
  40 + };
27 41
28 42 protected TransientPlayer(Context c, String url, boolean isDuckable) {
29 43 this.c = c;
@@ -32,6 +46,7 @@ protected TransientPlayer(Context c, String url, boolean isDuckable) {
32 46 this.url = url;
33 47 this.isDuckable = isDuckable;
34 48 this.playType = URL;
  49 + this.audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
35 50 }
36 51
37 52 protected TransientPlayer(Context c, FileDescriptor file, boolean isDuckable) {
@@ -41,12 +56,31 @@ protected TransientPlayer(Context c, FileDescriptor file, boolean isDuckable) {
41 56 this.file = file;
42 57 this.isDuckable = isDuckable;
43 58 this.playType = FILE;
  59 + this.audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
44 60 }
45 61
46   - private void play() {
  62 + private TransientPlayer play() {
47 63 TransientPlayerTask task = new TransientPlayerTask();
48 64 Thread thread = new Thread(task);
49 65 thread.run();
  66 + return this;
  67 + }
  68 +
  69 + public void stop() {
  70 + try {
  71 + wrapper.stop();
  72 + audioManager.abandonAudioFocus(audioFocusListener);
  73 + wrapper.release();
  74 + } catch (Exception e) {
  75 +
  76 + }
  77 + }
  78 +
  79 + public boolean isPlaying() {
  80 + return (wrapper.getState() == MediaPlayerWrapper.STARTED ||
  81 + wrapper.getState() == MediaPlayerWrapper.INITIALIZED ||
  82 + wrapper.getState() == MediaPlayerWrapper.PREPARED ||
  83 + wrapper.getState() == MediaPlayerWrapper.PREPARING);
50 84 }
51 85
52 86 private int getDurationHint() {
@@ -79,18 +113,6 @@ public void run() {
79 113 return;
80 114 }
81 115
82   - final AudioManager audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
83   -
84   - final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
85   - @Override
86   - public void onAudioFocusChange(int focusChange) {
87   - // I don't think we need to do anything here.
88   - // generally, we might want to stop the audio in
89   - // the event that we have lost focus in a non-
90   - // transient way, I think.
91   - }
92   - };
93   -
94 116 wrapper.setOnCompletionListener(new OnCompletionListener() {
95 117
96 118 @Override

0 comments on commit 5642a82

Please sign in to comment.
Something went wrong with that request. Please try again.