@@ -38,14 +38,11 @@ import java.util.function.Consumer
3838 * this allows us to keep playing even when switching between the different UIs.
3939 */
4040class PlayerService : Service () {
41- private var player: Player ? = null
41+ lateinit var player: Player
42+ private set
4243
4344 private val mBinder: IBinder = LocalBinder (this )
4445
45- fun getPlayer (): Player ? {
46- return player
47- }
48-
4946 /* //////////////////////////////////////////////////////////////////////////
5047 // Service's LifeCycle
5148 ////////////////////////////////////////////////////////////////////////// */
@@ -63,7 +60,7 @@ class PlayerService : Service() {
6360 loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
6461 service would never be put in the foreground while we said to the system we would do so
6562 */
66- player!! .UIs ().getOpt<NotificationPlayerUi >(NotificationPlayerUi ::class .java)
63+ player.UIs ().getOpt<NotificationPlayerUi >(NotificationPlayerUi ::class .java)
6764 .ifPresent(Consumer { obj: NotificationPlayerUi ? -> obj!! .createNotificationAndStartForeground() })
6865 }
6966
@@ -88,13 +85,11 @@ class PlayerService : Service() {
8885 If the service is already started in foreground, requesting it to be started shouldn't
8986 do anything
9087 */
91- if (player != null ) {
92- player!! .UIs ().getOpt<NotificationPlayerUi >(NotificationPlayerUi ::class .java)
93- .ifPresent(Consumer { obj: NotificationPlayerUi ? -> obj!! .createNotificationAndStartForeground() })
94- }
88+ player.UIs ().getOpt<NotificationPlayerUi >(NotificationPlayerUi ::class .java)
89+ .ifPresent(Consumer { obj: NotificationPlayerUi ? -> obj!! .createNotificationAndStartForeground() })
9590
9691 if (Intent .ACTION_MEDIA_BUTTON == intent.getAction() &&
97- (player == null || player !! .getPlayQueue() == null )
92+ (player.getPlayQueue() == null )
9893 ) {
9994 /*
10095 No need to process media button's actions if the player is not working, otherwise
@@ -106,17 +101,15 @@ class PlayerService : Service() {
106101 return START_NOT_STICKY
107102 }
108103
109- if (player != null ) {
110- player!! .handleIntent(intent)
111- player!! .UIs ().getOpt<MediaSessionPlayerUi >(MediaSessionPlayerUi ::class .java)
112- .ifPresent(
113- Consumer { ui: MediaSessionPlayerUi ? ->
114- ui!! .handleMediaButtonIntent(
115- intent
116- )
117- }
118- )
119- }
104+ player.handleIntent(intent)
105+ player.UIs ().getOpt<MediaSessionPlayerUi >(MediaSessionPlayerUi ::class .java)
106+ .ifPresent(
107+ Consumer { ui: MediaSessionPlayerUi ? ->
108+ ui!! .handleMediaButtonIntent(
109+ intent
110+ )
111+ }
112+ )
120113
121114 return START_NOT_STICKY
122115 }
@@ -126,17 +119,17 @@ class PlayerService : Service() {
126119 Log .d(TAG , " stopForImmediateReusing() called" )
127120 }
128121
129- if (player != null && ! player!! .exoPlayerIsNull()) {
122+ if (! player.exoPlayerIsNull()) {
130123 // Releases wifi & cpu, disables keepScreenOn, etc.
131124 // We can't just pause the player here because it will make transition
132125 // from one stream to a new stream not smooth
133- player!! .smoothStopForImmediateReusing()
126+ player.smoothStopForImmediateReusing()
134127 }
135128 }
136129
137130 override fun onTaskRemoved (rootIntent : Intent ? ) {
138131 super .onTaskRemoved(rootIntent)
139- if (player != null && ! player!! .videoPlayerSelected()) {
132+ if (! player.videoPlayerSelected()) {
140133 return
141134 }
142135 onDestroy()
@@ -148,18 +141,11 @@ class PlayerService : Service() {
148141 if (DEBUG ) {
149142 Log .d(TAG , " destroy() called" )
150143 }
151- cleanup()
152- }
153-
154- private fun cleanup () {
155- if (player != null ) {
156- player!! .destroy()
157- player = null
158- }
144+ player.saveAndShutdown()
159145 }
160146
161147 fun stopService () {
162- cleanup ()
148+ player.saveAndShutdown ()
163149 stopSelf()
164150 }
165151
0 commit comments