2020package org .amahi .anywhere .activity ;
2121
2222import android .app .Activity ;
23- import android .media .AudioManager ;
23+ import android .content .ComponentName ;
24+ import android .content .Context ;
25+ import android .content .Intent ;
26+ import android .content .ServiceConnection ;
2427import android .media .MediaPlayer ;
2528import android .net .Uri ;
2629import android .os .Bundle ;
30+ import android .os .IBinder ;
2731import android .view .MenuItem ;
2832import android .view .View ;
2933import android .view .animation .Animation ;
4347import org .amahi .anywhere .server .client .ServerClient ;
4448import org .amahi .anywhere .server .model .ServerFile ;
4549import org .amahi .anywhere .server .model .ServerShare ;
50+ import org .amahi .anywhere .service .AudioService ;
4651import org .amahi .anywhere .task .AudioMetadataRetrievingTask ;
4752import org .amahi .anywhere .util .AudioAlbumArtDownloader ;
4853import org .amahi .anywhere .util .Intents ;
4954
50- import java .io .IOException ;
5155import java .util .Arrays ;
5256import java .util .HashSet ;
5357import java .util .Set ;
5458
5559import javax .inject .Inject ;
5660
57- public class ServerFileAudioActivity extends Activity implements MediaController .MediaPlayerControl , MediaPlayer .OnPreparedListener
61+ public class ServerFileAudioActivity extends Activity implements ServiceConnection ,
62+ MediaPlayer .OnPreparedListener ,
63+ MediaController .MediaPlayerControl
5864{
5965 public static final Set <String > SUPPORTED_FORMATS ;
6066
@@ -67,45 +73,24 @@ public class ServerFileAudioActivity extends Activity implements MediaController
6773 ));
6874 }
6975
70- private static final class SavedState
71- {
72- private SavedState () {
73- }
74-
75- public static final String AUDIO_TIME = "audio_time" ;
76- }
77-
7876 @ Inject
7977 ServerClient serverClient ;
8078
81- private MediaPlayer audioPlayer ;
82-
79+ private AudioService audioService ;
8380 private MediaController audioControls ;
8481
85- private int audioTime ;
86-
8782 @ Override
8883 protected void onCreate (Bundle savedInstanceState ) {
8984 super .onCreate (savedInstanceState );
9085 setContentView (R .layout .activity_server_file_audio );
9186
92- setUpSavedState (savedInstanceState );
93-
9487 setUpInjections ();
9588
9689 setUpHomeNavigation ();
9790
9891 setUpAudio ();
9992 }
10093
101- private void setUpSavedState (Bundle savedState ) {
102- if (savedState == null ) {
103- return ;
104- }
105-
106- audioTime = savedState .getInt (SavedState .AUDIO_TIME );
107- }
108-
10994 private void setUpInjections () {
11095 AmahiApplication .from (this ).inject (this );
11196 }
@@ -174,20 +159,35 @@ private void setUpAudioAlbumArt() {
174159 protected void onStart () {
175160 super .onStart ();
176161
177- setUpAudioPlayer ();
162+ setUpAudioService ();
163+ setUpAudioServiceBind ();
164+ }
165+
166+ private void setUpAudioService () {
167+ Intent intent = new Intent (this , AudioService .class );
168+ startService (intent );
169+ }
170+
171+ private void setUpAudioServiceBind () {
172+ Intent intent = new Intent (this , AudioService .class );
173+ bindService (intent , this , Context .BIND_AUTO_CREATE );
174+ }
175+
176+ @ Override
177+ public void onServiceDisconnected (ComponentName serviceName ) {
178+ }
179+
180+ @ Override
181+ public void onServiceConnected (ComponentName serviceName , IBinder serviceBinder ) {
182+ setUpAudioServiceBind (serviceBinder );
183+
178184 setUpAudioControls ();
185+ setUpAudioPlayback ();
179186 }
180187
181- private void setUpAudioPlayer () {
182- try {
183- audioPlayer = new MediaPlayer ();
184- audioPlayer .setAudioStreamType (AudioManager .STREAM_MUSIC );
185- audioPlayer .setDataSource (this , getAudioUri ());
186- audioPlayer .setOnPreparedListener (this );
187- audioPlayer .prepareAsync ();
188- } catch (IOException e ) {
189- throw new RuntimeException (e );
190- }
188+ private void setUpAudioServiceBind (IBinder serviceBinder ) {
189+ AudioService .AudioServiceBinder audioServiceBinder = (AudioService .AudioServiceBinder ) serviceBinder ;
190+ audioService = audioServiceBinder .getAudioService ();
191191 }
192192
193193 private void setUpAudioControls () {
@@ -197,22 +197,27 @@ private void setUpAudioControls() {
197197 audioControls .setAnchorView (findViewById (R .id .animator ));
198198 }
199199
200+ private void setUpAudioPlayback () {
201+ if (audioService .isStarted ()) {
202+ showAudio ();
203+ } else {
204+ audioService .startAudio (getAudioUri (), this );
205+ }
206+ }
207+
200208 @ Override
201- public void onPrepared (MediaPlayer mediaPlayer ) {
202- setUpAudioTime ();
209+ public void onPrepared (MediaPlayer audioPlayer ) {
203210 start ();
204211
205212 showAudio ();
206- showAudioControls ();
207213 }
208214
209- private void setUpAudioTime () {
210- if (audioPlayer .getCurrentPosition () == 0 ) {
211- audioPlayer .seekTo (audioTime );
212- }
215+ private void showAudio () {
216+ showAudioMetadata ();
217+ showAudioControls ();
213218 }
214219
215- private void showAudio () {
220+ private void showAudioMetadata () {
216221 ViewAnimator animator = (ViewAnimator ) findViewById (R .id .animator );
217222
218223 View content = findViewById (R .id .layout_content );
@@ -223,15 +228,27 @@ private void showAudio() {
223228 }
224229
225230 private void showAudioControls () {
226- Animation showAnimation = AnimationUtils .loadAnimation (this , R .anim .slide_up_view );
227- audioControls .startAnimation (showAnimation );
231+ if (areAudioControlsAvailable () && !audioControls .isShowing ()) {
232+ Animation showAnimation = AnimationUtils .loadAnimation (this , R .anim .slide_up_view );
233+ audioControls .startAnimation (showAnimation );
234+
235+ audioControls .show (0 );
236+ }
237+ }
238+
239+ private boolean areAudioControlsAvailable () {
240+ return audioControls != null ;
241+ }
228242
229- audioControls .show (0 );
243+ private void hideAudioControls () {
244+ if (areAudioControlsAvailable ()) {
245+ audioControls .hide ();
246+ }
230247 }
231248
232249 @ Override
233250 public void start () {
234- audioPlayer .start ();
251+ audioService . getAudioPlayer () .start ();
235252 }
236253
237254 @ Override
@@ -241,7 +258,7 @@ public boolean canPause() {
241258
242259 @ Override
243260 public void pause () {
244- audioPlayer .pause ();
261+ audioService . getAudioPlayer () .pause ();
245262 }
246263
247264 @ Override
@@ -256,22 +273,22 @@ public boolean canSeekForward() {
256273
257274 @ Override
258275 public void seekTo (int time ) {
259- audioPlayer .seekTo (time );
276+ audioService . getAudioPlayer () .seekTo (time );
260277 }
261278
262279 @ Override
263280 public int getDuration () {
264- return audioPlayer .getDuration ();
281+ return audioService . getAudioPlayer () .getDuration ();
265282 }
266283
267284 @ Override
268285 public int getCurrentPosition () {
269- return audioPlayer .getCurrentPosition ();
286+ return audioService . getAudioPlayer () .getCurrentPosition ();
270287 }
271288
272289 @ Override
273290 public boolean isPlaying () {
274- return audioPlayer .isPlaying ();
291+ return audioService . getAudioPlayer () .isPlaying ();
275292 }
276293
277294 @ Override
@@ -281,65 +298,61 @@ public int getBufferPercentage() {
281298
282299 @ Override
283300 public int getAudioSessionId () {
284- return audioPlayer .getAudioSessionId ();
301+ return audioService .getAudioPlayer ().getAudioSessionId ();
302+ }
303+
304+ @ Override
305+ public boolean onOptionsItemSelected (MenuItem menuItem ) {
306+ switch (menuItem .getItemId ()) {
307+ case android .R .id .home :
308+ finish ();
309+ return true ;
310+
311+ default :
312+ return super .onOptionsItemSelected (menuItem );
313+ }
285314 }
286315
287316 @ Override
288317 protected void onResume () {
289318 super .onResume ();
290319
291- BusProvider . getBus (). register ( this );
320+ showAudioControls ( );
292321
293- audioPlayer . start ( );
322+ BusProvider . getBus (). register ( this );
294323 }
295324
296325 @ Override
297326 protected void onPause () {
298327 super .onPause ();
299328
300- BusProvider .getBus ().unregister (this );
301-
302329 hideAudioControls ();
303- }
304330
305- private void hideAudioControls () {
306- audioControls .hide ();
331+ BusProvider .getBus ().unregister (this );
307332 }
308333
309334 @ Override
310335 protected void onStop () {
311336 super .onStop ();
312337
313- audioTime = getCurrentPosition ();
314-
315- tearDownAudioPlayer ();
338+ tearDownAudioServiceBind ();
316339 }
317340
318- private void tearDownAudioPlayer () {
319- audioPlayer .stop ();
320- audioPlayer .release ();
341+ private void tearDownAudioServiceBind () {
342+ unbindService (this );
321343 }
322344
323345 @ Override
324- public void onSaveInstanceState ( Bundle outState ) {
325- super .onSaveInstanceState ( outState );
346+ protected void onDestroy ( ) {
347+ super .onDestroy ( );
326348
327- tearDownSavedState (outState );
328- }
329-
330- private void tearDownSavedState (Bundle savedState ) {
331- savedState .putInt (SavedState .AUDIO_TIME , getCurrentPosition ());
349+ if (isFinishing ()) {
350+ tearDownAudioService ();
351+ }
332352 }
333353
334- @ Override
335- public boolean onOptionsItemSelected (MenuItem menuItem ) {
336- switch (menuItem .getItemId ()) {
337- case android .R .id .home :
338- finish ();
339- return true ;
340-
341- default :
342- return super .onOptionsItemSelected (menuItem );
343- }
354+ private void tearDownAudioService () {
355+ Intent intent = new Intent (this , AudioService .class );
356+ stopService (intent );
344357 }
345358}
0 commit comments