aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/org/antennapod/audio/ServiceBackedAudioPlayer.java
blob: 335f1fc8b55333056ea39205dbd93fe243ec0214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
// Copyright 2011, Aocate, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------
// Compared to the original version, this class been slightly modified so
// that any acquired WakeLocks are only held while the MediaPlayer is
// playing (see the stayAwake method for more details).


package org.antennapod.audio;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.net.Uri;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.RemoteException;
import android.util.Log;

import com.aocate.presto.service.IDeathCallback_0_8;
import com.aocate.presto.service.IOnBufferingUpdateListenerCallback_0_8;
import com.aocate.presto.service.IOnCompletionListenerCallback_0_8;
import com.aocate.presto.service.IOnErrorListenerCallback_0_8;
import com.aocate.presto.service.IOnInfoListenerCallback_0_8;
import com.aocate.presto.service.IOnPitchAdjustmentAvailableChangedListenerCallback_0_8;
import com.aocate.presto.service.IOnPreparedListenerCallback_0_8;
import com.aocate.presto.service.IOnSeekCompleteListenerCallback_0_8;
import com.aocate.presto.service.IOnSpeedAdjustmentAvailableChangedListenerCallback_0_8;
import com.aocate.presto.service.IPlayMedia_0_8;

import java.io.IOException;

/**
 * Class for connecting to remote speed-altering, media playing Service
 * Note that there is unusually high coupling between MediaPlayer and this 
 * class.  This is an unfortunate compromise, since the alternative was to
 * track state in two different places in this code (plus the internal state
 * of the remote media player).
 * @author aocate
 *
 */
public class ServiceBackedAudioPlayer extends AbstractAudioPlayer {

    static final String INTENT_NAME = "com.aocate.intent.PLAY_AUDIO_ADJUST_SPEED_0_8";

    private static final String SBMP_TAG = "ServiceBackedMediaPlaye";

    private ServiceConnection mPlayMediaServiceConnection = null;
    protected IPlayMedia_0_8 pmInterface = null;
    private Intent playMediaServiceIntent = null;
    // In some cases, we're going to have to replace the
    // android.media.MediaPlayer on the fly, and we don't want to touch the
    // wrong media player.

    private int sessionId = 0;
    private boolean isErroring = false;
    private int mAudioStreamType = AudioManager.STREAM_MUSIC;

    private WakeLock mWakeLock = null;

    // So here's the major problem
    // Sometimes the service won't exist or won't be connected,
    // so start with an android.media.MediaPlayer, and when
    // the service is connected, use that from then on
    public ServiceBackedAudioPlayer(MediaPlayer owningMediaPlayer, final Context context, final ServiceConnection serviceConnection) {
        super(owningMediaPlayer, context);
        Log.d(SBMP_TAG, "Instantiating ServiceBackedMediaPlayer 87");
        this.playMediaServiceIntent =
                MediaPlayer.getPrestoServiceIntent(context, INTENT_NAME);
        this.mPlayMediaServiceConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName name, IBinder service) {
                IPlayMedia_0_8 tmpPlayMediaInterface = IPlayMedia_0_8.Stub.asInterface((IBinder) service);

                Log.d(SBMP_TAG, "Setting up pmInterface 94");
                if (ServiceBackedAudioPlayer.this.sessionId == 0) {
                    try {
                        // The IDeathCallback isn't a conventional callback.
                        // It exists so that if the client ceases to exist,
                        // the Service becomes aware of that and can shut
                        // down whatever it needs to shut down
                        ServiceBackedAudioPlayer.this.sessionId = (int) tmpPlayMediaInterface.startSession(new IDeathCallback_0_8.Stub() {
                        });
                        // This is really bad if this fails
                    } catch (RemoteException e) {
                        e.printStackTrace();
                        ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
                    }
                }

                Log.d(SBMP_TAG, "Assigning pmInterface");

                ServiceBackedAudioPlayer.this.setOnBufferingUpdateCallback(tmpPlayMediaInterface);
                ServiceBackedAudioPlayer.this.setOnCompletionCallback(tmpPlayMediaInterface);
                ServiceBackedAudioPlayer.this.setOnErrorCallback(tmpPlayMediaInterface);
                ServiceBackedAudioPlayer.this.setOnInfoCallback(tmpPlayMediaInterface);
                ServiceBackedAudioPlayer.this.setOnPitchAdjustmentAvailableChangedListener(tmpPlayMediaInterface);
                ServiceBackedAudioPlayer.this.setOnPreparedCallback(tmpPlayMediaInterface);
                ServiceBackedAudioPlayer.this.setOnSeekCompleteCallback(tmpPlayMediaInterface);
                ServiceBackedAudioPlayer.this.setOnSpeedAdjustmentAvailableChangedCallback(tmpPlayMediaInterface);

                // In order to avoid race conditions from the sessionId or listener not being assigned
                pmInterface = tmpPlayMediaInterface;

                Log.d(SBMP_TAG, "Invoking onServiceConnected");
                serviceConnection.onServiceConnected(name, service);
            }

            public void onServiceDisconnected(ComponentName name) {
                Log.d(SBMP_TAG, "onServiceDisconnected 114");

                pmInterface = null;

                sessionId = 0;

                serviceConnection.onServiceDisconnected(name);
            }
        };

        Log.d(SBMP_TAG, "Connecting PlayMediaService 124");
        if (!ConnectPlayMediaService()) {
            Log.e(SBMP_TAG, "bindService failed");
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private boolean ConnectPlayMediaService() {
        Log.d(SBMP_TAG, "ConnectPlayMediaService()");

        if (MediaPlayer.isIntentAvailable(mContext, INTENT_NAME)) {
            Log.d(SBMP_TAG, INTENT_NAME + " is available");
            if (pmInterface == null) {
                try {
                    Log.d(SBMP_TAG, "Binding service");
                    return mContext.bindService(playMediaServiceIntent, mPlayMediaServiceConnection, Context.BIND_AUTO_CREATE);
                } catch (Exception e) {
                    Log.e(SBMP_TAG, "Could not bind with service", e);
                    return false;
                }
            } else {
                Log.d(SBMP_TAG, "Service already bound");
                return true;
            }
        }
        else {
            Log.d(SBMP_TAG, INTENT_NAME + " is not available");
            return false;
        }
    }

    @Override
    public int getAudioSessionId() {
        return sessionId;
    }

    /**
     * Returns true if pitch can be changed at this moment
     * @return True if pitch can be changed
     */
    @Override
    public boolean canSetPitch() {
        Log.d(SBMP_TAG, "canSetPitch() 155");

        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set pitch if the service isn't connected
            try {
                return pmInterface.canSetPitch(ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        return false;
    }

    /**
     * Returns true if speed can be changed at this moment
     * @return True if speed can be changed
     */
    @Override
    public boolean canSetSpeed() {
        Log.d(SBMP_TAG, "canSetSpeed() 180");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set speed if the service isn't connected
            try {
                return pmInterface.canSetSpeed(ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        return false;
    }

    @Override
    public boolean canDownmix() {
        return false;
    }

    @Override
    public void setDownmix(boolean enable) {
        return;
    }

    void error(int what, int extra) {
        owningMediaPlayer.lock.lock();
        Log.e(SBMP_TAG, "error(" + what + ", " + extra + ")");
        stayAwake(false);
        try {
            if (!this.isErroring) {
                this.isErroring = true;
                owningMediaPlayer.state = MediaPlayer.State.ERROR;
                if (owningMediaPlayer.onErrorListener != null) {
                    if (owningMediaPlayer.onErrorListener.onError(owningMediaPlayer, what, extra)) {
                        return;
                    }
                }
                if (owningMediaPlayer.onCompletionListener != null) {
                    owningMediaPlayer.onCompletionListener.onCompletion(owningMediaPlayer);
                }
            }
        }
        finally {
            this.isErroring = false;
            owningMediaPlayer.lock.unlock();
        }
    }

    protected void finalize() throws Throwable {
        owningMediaPlayer.lock.lock();
        try {
            Log.d(SBMP_TAG, "finalize() 224");
            this.release();
        }
        finally {
            owningMediaPlayer.lock.unlock();
        }
    }

    /**
     * Returns the number of steps (in a musical scale) by which playback is
     * currently shifted.  When greater than zero, pitch is shifted up.
     * When less than zero, pitch is shifted down.
     * @return The number of steps pitch is currently shifted by
     */
    @Override
    public float getCurrentPitchStepsAdjustment() {
        Log.d(SBMP_TAG, "getCurrentPitchStepsAdjustment() 240");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set pitch if the service isn't connected
            try {
                return pmInterface.getCurrentPitchStepsAdjustment(
                        ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        return 0f;
    }

    /**
     * Functions identically to android.media.MediaPlayer.getCurrentPosition()
     * @return Current position (in milliseconds)
     */
    @Override
    public int getCurrentPosition() {
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
            return 0;
        }
        try {
            return pmInterface.getCurrentPosition(
                    ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        return 0;
    }

    /**
     * Returns the current speed multiplier.  Defaults to 1.0 (normal speed)
     * @return The current speed multiplier
     */
    @Override
    public float getCurrentSpeedMultiplier() {
        Log.d(SBMP_TAG, "getCurrentSpeedMultiplier() 286");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set speed if the service isn't connected
            try {
                return pmInterface.getCurrentSpeedMultiplier(
                        ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        return 1;
    }

    /**
     * Functions identically to android.media.MediaPlayer.getDuration()
     * @return Length of the track (in milliseconds)
     */
    @Override
    public int getDuration() {
        Log.d(SBMP_TAG, "getDuration() 311");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            return pmInterface.getDuration(ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        return 0;
    }

    /**
     * Get the maximum value that can be passed to setPlaybackSpeed
     * @return The maximum speed multiplier
     */
    @Override
    public float getMaxSpeedMultiplier() {
        Log.d(SBMP_TAG, "getMaxSpeedMultiplier() 332");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set speed if the Service isn't connected
            try {
                return pmInterface.getMaxSpeedMultiplier(
                        ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        return 1f;
    }

    /**
     * Get the minimum value that can be passed to setPlaybackSpeed
     * @return The minimum speed multiplier
     */
    @Override
    public float getMinSpeedMultiplier() {
        Log.d(SBMP_TAG, "getMinSpeedMultiplier() 357");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set speed if the Service isn't connected
            try {
                return pmInterface.getMinSpeedMultiplier(
                        ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        return 1f;
    }

    public int getServiceVersionCode() {
        Log.d(SBMP_TAG, "getVersionCode");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            return pmInterface.getVersionCode();
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        return 0;
    }

    public String getServiceVersionName() {
        Log.d(SBMP_TAG, "getVersionName");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            return pmInterface.getVersionName();
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        return "";
    }

    public boolean isConnected() {
        return (pmInterface != null);
    }

    /**
     * Functions identically to android.media.MediaPlayer.isLooping()
     * @return True if the track is looping
     */
    @Override
    public boolean isLooping() {
        Log.d(SBMP_TAG, "isLooping() 382");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            return pmInterface.isLooping(ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        return false;
    }

    /**
     * Functions identically to android.media.MediaPlayer.isPlaying()
     * @return True if the track is playing
     */
    @Override
    public boolean isPlaying() {
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            try {
                return pmInterface.isPlaying(ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        return false;
    }

    /**
     * Functions identically to android.media.MediaPlayer.pause()
     * Pauses the track
     */
    @Override
    public void pause() {
        Log.d(SBMP_TAG, "pause() 424");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.pause(ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        stayAwake(false);
    }

    /**
     * Functions identically to android.media.MediaPlayer.prepare()
     * Prepares the track.  This or prepareAsync must be called before start()
     */
    @Override
    public void prepare() throws IllegalStateException, IOException {
        Log.d(SBMP_TAG, "prepare() 444");
        Log.d(SBMP_TAG, "onPreparedCallback is: " + ((this.mOnPreparedCallback == null) ? "null" : "non-null"));
        if (pmInterface == null) {
            Log.d(SBMP_TAG, "prepare: pmInterface is null");
            if (!ConnectPlayMediaService()) {
                Log.d(SBMP_TAG, "prepare: Failed to connect play media service");
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            Log.d(SBMP_TAG, "prepare: pmInterface isn't null");
            try {
                Log.d(SBMP_TAG, "prepare: Remote invoke pmInterface.prepare(" + ServiceBackedAudioPlayer.this.sessionId + ")");
                pmInterface.prepare(ServiceBackedAudioPlayer.this.sessionId);
                Log.d(SBMP_TAG, "prepare: prepared");
            } catch (RemoteException e) {
                Log.d(SBMP_TAG, "prepare: RemoteException");
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        Log.d(SBMP_TAG, "Done with prepare()");
    }

    /**
     * Functions identically to android.media.MediaPlayer.prepareAsync()
     * Prepares the track.  This or prepare must be called before start()
     */
    @Override
    public void prepareAsync() {
        Log.d(SBMP_TAG, "prepareAsync() 469");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.prepareAsync(ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    /**
     * Functions identically to android.media.MediaPlayer.release()
     * Releases the underlying resources used by the media player.
     */
    @Override
    public void release() {
        Log.d(SBMP_TAG, "release() 492");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            Log.d(SBMP_TAG, "release() 500");
            try {
                pmInterface.release(ServiceBackedAudioPlayer.this.sessionId);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
            mContext.unbindService(this.mPlayMediaServiceConnection);
            // Don't try to keep awake (if we were)
            this.setWakeMode(mContext, 0);
            pmInterface = null;
            this.sessionId = 0;
        }

        if ((this.mWakeLock != null) && this.mWakeLock.isHeld()) {
            Log.d(SBMP_TAG, "Releasing wakelock");
            this.mWakeLock.release();
        }
    }

    /**
     * Functions identically to android.media.MediaPlayer.reset()
     * Resets the track to idle state
     */
    @Override
    public void reset() {
        Log.d(SBMP_TAG, "reset() 523");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.reset(ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        stayAwake(false);
    }

    /**
     * Functions identically to android.media.MediaPlayer.seekTo(int msec)
     * Seeks to msec in the track
     */
    @Override
    public void seekTo(int msec) throws IllegalStateException {
        Log.d(SBMP_TAG, "seekTo(" + msec + ")");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.seekTo(ServiceBackedAudioPlayer.this.sessionId, msec);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    /**
     * Functions identically to android.media.MediaPlayer.setAudioStreamType(int streamtype)
     * Sets the audio stream type.
     */
    @Override
    public void setAudioStreamType(int streamtype) {
        Log.d(SBMP_TAG, "setAudioStreamType(" + streamtype + ")");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.setAudioStreamType(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mAudioStreamType);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }


    /**
     * Functions identically to android.media.MediaPlayer.setDataSource(Context context, Uri uri)
     * Sets uri as data source in the context given
     */
    @Override
    public void setDataSource(Context context, Uri uri) throws IllegalArgumentException, IllegalStateException, IOException {
        Log.d(SBMP_TAG, "setDataSource(context, uri)");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.setDataSourceUri(
                    ServiceBackedAudioPlayer.this.sessionId,
                    uri);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    /**
     * Functions identically to android.media.MediaPlayer.setDataSource(String path)
     * Sets the data source of the track to a file given.
     */
    @Override
    public void setDataSource(String path) throws IllegalArgumentException, IllegalStateException, IOException {
        Log.d(SBMP_TAG, "setDataSource(path)");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface == null) {
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        else {
            try {
                pmInterface.setDataSourceString(
                        ServiceBackedAudioPlayer.this.sessionId,
                        path);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
    }

    /**
     * Sets whether to use speed adjustment or not.  Speed adjustment on is
     * more computation-intensive than with it off.
     * @param enableSpeedAdjustment Whether speed adjustment should be supported.
     */
    @Override
    public void setEnableSpeedAdjustment(boolean enableSpeedAdjustment) {
        // TODO: This has no business being here, I think
        owningMediaPlayer.lock.lock();
        Log.d(SBMP_TAG, "setEnableSpeedAdjustment(enableSpeedAdjustment)");
        try {
            if (pmInterface == null) {
                if (!ConnectPlayMediaService()) {
                    ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
                }
            }
            if (pmInterface != null) {
                // Can't set speed if the Service isn't connected
                try {
                    pmInterface.setEnableSpeedAdjustment(
                            ServiceBackedAudioPlayer.this.sessionId,
                            enableSpeedAdjustment);
                } catch (RemoteException e) {
                    e.printStackTrace();
                    ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
                }
            }
        }
        finally {
            owningMediaPlayer.lock.unlock();
        }
    }


    /**
     * Functions identically to android.media.MediaPlayer.setLooping(boolean loop)
     * Sets the track to loop infinitely if loop is true, play once if loop is false
     */
    @Override
    public void setLooping(boolean loop) {
        Log.d(SBMP_TAG, "setLooping(" + loop + ")");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.setLooping(ServiceBackedAudioPlayer.this.sessionId, loop);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    /**
     * Sets the number of steps (in a musical scale) by which playback is
     * currently shifted.  When greater than zero, pitch is shifted up.
     * When less than zero, pitch is shifted down.
     *
     * @param pitchSteps The number of steps by which to shift playback
     */
    @Override
    public void setPitchStepsAdjustment(float pitchSteps) {
        Log.d(SBMP_TAG, "setPitchStepsAdjustment(" + pitchSteps + ")");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set speed if the Service isn't connected
            try {
                pmInterface.setPitchStepsAdjustment(
                        ServiceBackedAudioPlayer.this.sessionId,
                        pitchSteps);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
    }

    /**
     * Sets the percentage by which pitch is currently shifted.  When
     * greater than zero, pitch is shifted up.  When less than zero, pitch
     * is shifted down
     * @param f The percentage to shift pitch
     */
    @Override
    public void setPlaybackPitch(float f) {
        Log.d(SBMP_TAG, "setPlaybackPitch(" + f + ")");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set speed if the Service isn't connected
            try {
                pmInterface.setPlaybackPitch(
                        ServiceBackedAudioPlayer.this.sessionId,
                        f);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
    }

    /**
     * Set playback speed.  1.0 is normal speed, 2.0 is double speed, and so
     * on.  Speed should never be set to 0 or below.
     * @param f The speed multiplier to use for further playback
     */
    @Override
    public void setPlaybackSpeed(float f) {
        Log.d(SBMP_TAG, "setPlaybackSpeed(" + f + ")");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        if (pmInterface != null) {
            // Can't set speed if the Service isn't connected
            try {
                pmInterface.setPlaybackSpeed(
                        ServiceBackedAudioPlayer.this.sessionId,
                        f);
            } catch (RemoteException e) {
                e.printStackTrace();
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
    }

    /**
     * Functions identically to android.media.MediaPlayer.setVolume(float leftVolume, float rightVolume)
     * Sets the stereo volume
     */
    @Override
    public void setVolume(float leftVolume, float rightVolume) {
        Log.d(SBMP_TAG, "setVolume(" + leftVolume + ", " + rightVolume + ")");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.setVolume(
                    ServiceBackedAudioPlayer.this.sessionId,
                    leftVolume,
                    rightVolume);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    /**
     * Functions identically to android.media.MediaPlayer.setWakeMode(Context context, int mode)
     * Acquires a wake lock in the context given.  You must request the appropriate permissions
     * in your AndroidManifest.xml file.
     */
    @Override
    // This does not just call .setWakeMode() in the Service because doing so
    // would add a permission requirement to the Service.  Do it here, and it's
    // the client app's responsibility to request that permission
    public void setWakeMode(Context context, int mode) {
        Log.d(SBMP_TAG, "setWakeMode(context, " + mode + ")");
        boolean wasHeld = false;
        if (mWakeLock != null) {
            if(mWakeLock.isHeld()) {
                wasHeld = true;
                Log.d(SBMP_TAG, "Releasing wakelock");
                mWakeLock.release();
            }
            mWakeLock = null;
        }
        if (mode != 0) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(mode, this.getClass().getName());
            mWakeLock.setReferenceCounted(false);

            if(wasHeld) {
                Log.d(SBMP_TAG, "Acquiring wakelock");
                mWakeLock.acquire();
            }
        }
    }

    /**
     * Changes the state of the WakeLock if it has been acquired.
     * If no WakeLock has been acquired with setWakeMode, this method does nothing.
     * */
    private void stayAwake(boolean awake) {
        if (BuildConfig.DEBUG) Log.d(SBMP_TAG, "stayAwake(" + awake + ")");
        if (mWakeLock != null) {
            if (awake && !mWakeLock.isHeld()) {
                Log.d(SBMP_TAG, "Acquiring wakelock");
                mWakeLock.acquire();
            } else if (!awake && mWakeLock.isHeld()) {
                Log.d(SBMP_TAG, "Releasing wakelock");
                mWakeLock.release();
            }
        }
    }

    private IOnBufferingUpdateListenerCallback_0_8.Stub mOnBufferingUpdateCallback = null;
    private void setOnBufferingUpdateCallback(IPlayMedia_0_8 iface) {
        try {
            if (this.mOnBufferingUpdateCallback == null) {
                mOnBufferingUpdateCallback = new IOnBufferingUpdateListenerCallback_0_8.Stub() {
                    public void onBufferingUpdate(int percent)
                            throws RemoteException {
                        owningMediaPlayer.lock.lock();
                        try {
                            if ((owningMediaPlayer.onBufferingUpdateListener != null)
                                    && (owningMediaPlayer.mpi == ServiceBackedAudioPlayer.this)) {
                                owningMediaPlayer.onBufferingUpdateListener.onBufferingUpdate(owningMediaPlayer, percent);
                            }
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                    }
                };
            }
            iface.registerOnBufferingUpdateCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    mOnBufferingUpdateCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private IOnCompletionListenerCallback_0_8.Stub mOnCompletionCallback = null;
    private void setOnCompletionCallback(IPlayMedia_0_8 iface) {
        try {
            if (this.mOnCompletionCallback == null) {
                this.mOnCompletionCallback = new IOnCompletionListenerCallback_0_8.Stub() {
                    public void onCompletion() throws RemoteException {
                        owningMediaPlayer.lock.lock();
                        Log.d(SBMP_TAG, "onCompletionListener being called");
                        stayAwake(false);
                        try {
                            if (owningMediaPlayer.onCompletionListener != null) {
                                owningMediaPlayer.onCompletionListener.onCompletion(owningMediaPlayer);
                            }
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                    }
                };
            }
            iface.registerOnCompletionCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mOnCompletionCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private IOnErrorListenerCallback_0_8.Stub mOnErrorCallback = null;
    private void setOnErrorCallback(IPlayMedia_0_8 iface) {
        try {
            if (this.mOnErrorCallback == null) {
                this.mOnErrorCallback = new IOnErrorListenerCallback_0_8.Stub() {
                    public boolean onError(int what, int extra) throws RemoteException {
                        owningMediaPlayer.lock.lock();
                        stayAwake(false);
                        try {
                            if (owningMediaPlayer.onErrorListener != null) {
                                return owningMediaPlayer.onErrorListener.onError(owningMediaPlayer, what, extra);
                            }
                            return false;
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                    }
                };
            }
            iface.registerOnErrorCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mOnErrorCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private IOnInfoListenerCallback_0_8.Stub mOnInfoCallback = null;
    private void setOnInfoCallback(IPlayMedia_0_8 iface) {
        try {
            if (this.mOnInfoCallback == null) {
                this.mOnInfoCallback = new IOnInfoListenerCallback_0_8.Stub() {
                    public boolean onInfo(int what, int extra) throws RemoteException {
                        owningMediaPlayer.lock.lock();
                        try {
                            if ((owningMediaPlayer.onInfoListener != null)
                                    && (owningMediaPlayer.mpi == ServiceBackedAudioPlayer.this)) {
                                return owningMediaPlayer.onInfoListener.onInfo(owningMediaPlayer, what, extra);
                            }
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                        return false;
                    }
                };
            }
            iface.registerOnInfoCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mOnInfoCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private IOnPitchAdjustmentAvailableChangedListenerCallback_0_8.Stub mOnPitchAdjustmentAvailableChangedCallback = null;
    private void setOnPitchAdjustmentAvailableChangedListener(IPlayMedia_0_8 iface) {
        try {
            if (this.mOnPitchAdjustmentAvailableChangedCallback == null) {
                this.mOnPitchAdjustmentAvailableChangedCallback = new IOnPitchAdjustmentAvailableChangedListenerCallback_0_8.Stub() {
                    public void onPitchAdjustmentAvailableChanged(
                            boolean pitchAdjustmentAvailable)
                            throws RemoteException {
                        owningMediaPlayer.lock.lock();
                        try {
                            if (owningMediaPlayer.onPitchAdjustmentAvailableChangedListener != null) {
                                owningMediaPlayer.onPitchAdjustmentAvailableChangedListener.onPitchAdjustmentAvailableChanged(owningMediaPlayer, pitchAdjustmentAvailable);
                            }
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                    }
                };
            }
            iface.registerOnPitchAdjustmentAvailableChangedCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mOnPitchAdjustmentAvailableChangedCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private IOnPreparedListenerCallback_0_8.Stub mOnPreparedCallback = null;
    private void setOnPreparedCallback(IPlayMedia_0_8 iface) {
        try {
            if (this.mOnPreparedCallback == null) {
                this.mOnPreparedCallback = new IOnPreparedListenerCallback_0_8.Stub() {
                    public void onPrepared() throws RemoteException {
                        owningMediaPlayer.lock.lock();
                        Log.d(SBMP_TAG, "setOnPreparedCallback.mOnPreparedCallback.onPrepared 1050");
                        try {
                            Log.d(SBMP_TAG, "owningMediaPlayer.onPreparedListener is " + ((owningMediaPlayer.onPreparedListener == null) ? "null" : "non-null"));
                            Log.d(SBMP_TAG, "owningMediaPlayer.mpi is " + ((owningMediaPlayer.mpi == ServiceBackedAudioPlayer.this) ? "this" : "not this"));
                            ServiceBackedAudioPlayer.this.lockMuteOnPreparedCount.lock();
                            try {
                                if (ServiceBackedAudioPlayer.this.muteOnPreparedCount > 0) {
                                    ServiceBackedAudioPlayer.this.muteOnPreparedCount--;
                                }
                                else {
                                    ServiceBackedAudioPlayer.this.muteOnPreparedCount = 0;
                                    if (ServiceBackedAudioPlayer.this.owningMediaPlayer.onPreparedListener != null) {
                                        owningMediaPlayer.onPreparedListener.onPrepared(owningMediaPlayer);
                                    }
                                }
                            }
                            finally {
                                ServiceBackedAudioPlayer.this.lockMuteOnPreparedCount.unlock();
                            }
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                    }
                };
            }
            iface.registerOnPreparedCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mOnPreparedCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private IOnSeekCompleteListenerCallback_0_8.Stub mOnSeekCompleteCallback = null;
    private void setOnSeekCompleteCallback(IPlayMedia_0_8 iface) {
        try {
            if (this.mOnSeekCompleteCallback == null) {
                this.mOnSeekCompleteCallback = new IOnSeekCompleteListenerCallback_0_8.Stub() {
                    public void onSeekComplete() throws RemoteException {
                        Log.d(SBMP_TAG, "onSeekComplete() 941");
                        owningMediaPlayer.lock.lock();
                        try {
                            if (ServiceBackedAudioPlayer.this.muteOnSeekCount > 0) {
                                Log.d(SBMP_TAG, "The next " + ServiceBackedAudioPlayer.this.muteOnSeekCount + " seek events are muted (counting this one)");
                                ServiceBackedAudioPlayer.this.muteOnSeekCount--;
                            }
                            else {
                                ServiceBackedAudioPlayer.this.muteOnSeekCount = 0;
                                Log.d(SBMP_TAG, "Attempting to invoke next seek event");
                                if (ServiceBackedAudioPlayer.this.owningMediaPlayer.onSeekCompleteListener != null) {
                                    Log.d(SBMP_TAG, "Invoking onSeekComplete");
                                    owningMediaPlayer.onSeekCompleteListener.onSeekComplete(owningMediaPlayer);
                                }
                            }
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                    }
                };
            }
            iface.registerOnSeekCompleteCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mOnSeekCompleteCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private IOnSpeedAdjustmentAvailableChangedListenerCallback_0_8.Stub mOnSpeedAdjustmentAvailableChangedCallback = null;
    private void setOnSpeedAdjustmentAvailableChangedCallback(IPlayMedia_0_8 iface) {
        try {
            Log.d(SBMP_TAG, "Setting the service of on speed adjustment available changed");
            if (this.mOnSpeedAdjustmentAvailableChangedCallback == null) {
                this.mOnSpeedAdjustmentAvailableChangedCallback = new IOnSpeedAdjustmentAvailableChangedListenerCallback_0_8.Stub() {
                    public void onSpeedAdjustmentAvailableChanged(
                            boolean speedAdjustmentAvailable)
                            throws RemoteException {
                        owningMediaPlayer.lock.lock();
                        try {
                            if (owningMediaPlayer.onSpeedAdjustmentAvailableChangedListener != null) {
                                owningMediaPlayer.onSpeedAdjustmentAvailableChangedListener.onSpeedAdjustmentAvailableChanged(owningMediaPlayer, speedAdjustmentAvailable);
                            }
                        }
                        finally {
                            owningMediaPlayer.lock.unlock();
                        }
                    }
                };
            }
            iface.registerOnSpeedAdjustmentAvailableChangedCallback(
                    ServiceBackedAudioPlayer.this.sessionId,
                    this.mOnSpeedAdjustmentAvailableChangedCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    /**
     * Functions identically to android.media.MediaPlayer.start()
     * Starts a track playing
     */
    @Override
    public void start() {
        Log.d(SBMP_TAG, "start()");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.start(ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        stayAwake(true);
    }

    /**
     * Functions identically to android.media.MediaPlayer.stop()
     * Stops a track playing and resets its position to the start.
     */
    @Override
    public void stop() {
        Log.d(SBMP_TAG, "stop()");
        if (pmInterface == null) {
            if (!ConnectPlayMediaService()) {
                ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            }
        }
        try {
            pmInterface.stop(ServiceBackedAudioPlayer.this.sessionId);
        } catch (RemoteException e) {
            e.printStackTrace();
            ServiceBackedAudioPlayer.this.error(MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
        stayAwake(false);
    }
}