Skip to content

Commit f56131b

Browse files
HatakeKakashrihaggaie
authored andcommitted
Addressed review comments
1 parent ba69128 commit f56131b

3 files changed

Lines changed: 36 additions & 42 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import static org.schabi.newpipe.util.ListHelper.getPopupResolutionIndex;
4646
import static org.schabi.newpipe.util.ListHelper.getResolutionIndex;
4747
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
48-
import static java.util.Objects.requireNonNull;
4948
import static java.util.concurrent.TimeUnit.MILLISECONDS;
5049

5150
import android.content.BroadcastReceiver;
@@ -87,8 +86,8 @@
8786
import org.schabi.newpipe.error.ErrorInfo;
8887
import org.schabi.newpipe.error.ErrorUtil;
8988
import org.schabi.newpipe.error.UserAction;
90-
import org.schabi.newpipe.extractor.stream.AudioStream;
9189
import org.schabi.newpipe.extractor.Image;
90+
import org.schabi.newpipe.extractor.stream.AudioStream;
9291
import org.schabi.newpipe.extractor.stream.StreamInfo;
9392
import org.schabi.newpipe.extractor.stream.StreamType;
9493
import org.schabi.newpipe.extractor.stream.VideoStream;
@@ -119,9 +118,9 @@
119118
import org.schabi.newpipe.util.DependentPreferenceHelper;
120119
import org.schabi.newpipe.util.ListHelper;
121120
import org.schabi.newpipe.util.NavigationHelper;
122-
import org.schabi.newpipe.util.image.PicassoHelper;
123121
import org.schabi.newpipe.util.SerializedCache;
124122
import org.schabi.newpipe.util.StreamTypeUtil;
123+
import org.schabi.newpipe.util.image.PicassoHelper;
125124

126125
import java.util.List;
127126
import java.util.Optional;
@@ -416,9 +415,12 @@ public void handleIntent(@NonNull final Intent intent) {
416415
== com.google.android.exoplayer2.Player.STATE_IDLE) {
417416
simpleExoPlayer.prepare();
418417
}
418+
// Seeks to a specific index and position in the player if the queue index has changed.
419419
if (playQueue.getIndex() != newQueue.getIndex()) {
420-
simpleExoPlayer.seekTo(newQueue.getIndex(),
421-
requireNonNull(newQueue.getItem()).getRecoveryPosition());
420+
final PlayQueueItem queueItem = newQueue.getItem();
421+
if (queueItem != null) {
422+
simpleExoPlayer.seekTo(newQueue.getIndex(), queueItem.getRecoveryPosition());
423+
}
422424
}
423425
simpleExoPlayer.setPlayWhenReady(playWhenReady);
424426

app/src/main/java/org/schabi/newpipe/player/PlayerService.kt

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,18 @@ import java.lang.ref.WeakReference
3939
* One service for all players.
4040
*/
4141
class PlayerService : MediaBrowserServiceCompat() {
42-
private val player: Player by lazy {
43-
Player(this).apply {
44-
/*
45-
Create the player notification and start immediately the service in foreground,
46-
otherwise if nothing is played or initializing the player and its components (especially
47-
loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
48-
service would never be put in the foreground while we said to the system we would do so
49-
*/
50-
UIs()[NotificationPlayerUi::class.java].ifPresent {
51-
it.createNotificationAndStartForeground()
52-
}
53-
}
54-
}
42+
private var player: Player? = null
5543

5644
private val mBinder: IBinder = LocalBinder(this)
57-
private val compositeDisposableLoadChildren = CompositeDisposable()
58-
private var mediaBrowserConnector: MediaBrowserConnector? = null
45+
private val disposables = CompositeDisposable()
46+
private var _mediaBrowserConnector: MediaBrowserConnector? = null
47+
private val mediaBrowserConnector: MediaBrowserConnector
5948
get() {
60-
if (field == null) {
61-
field = MediaBrowserConnector(this)
49+
return _mediaBrowserConnector ?: run {
50+
val newMediaBrowserConnector = MediaBrowserConnector(this)
51+
_mediaBrowserConnector = newMediaBrowserConnector
52+
newMediaBrowserConnector
6253
}
63-
return field
6454
}
6555

6656
val sessionConnector: MediaSessionConnector?
@@ -78,13 +68,14 @@ class PlayerService : MediaBrowserServiceCompat() {
7868
Localization.assureCorrectAppLanguage(this)
7969
ThemeHelper.setTheme(this)
8070

71+
player = Player(this)
8172
/*
8273
Create the player notification and start immediately the service in foreground,
8374
otherwise if nothing is played or initializing the player and its components (especially
8475
loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
8576
service would never be put in the foreground while we said to the system we would do so
8677
*/
87-
player.UIs()[NotificationPlayerUi::class.java].ifPresent {
78+
player!!.UIs()[NotificationPlayerUi::class.java].ifPresent {
8879
it.createNotificationAndStartForeground()
8980
}
9081
}
@@ -112,11 +103,11 @@ class PlayerService : MediaBrowserServiceCompat() {
112103
If the service is already started in foreground, requesting it to be started shouldn't
113104
do anything
114105
*/
115-
player.UIs()[NotificationPlayerUi::class.java].ifPresent {
106+
player?.UIs()?.get(NotificationPlayerUi::class.java)?.ifPresent {
116107
it.createNotificationAndStartForeground()
117108
}
118109

119-
if (Intent.ACTION_MEDIA_BUTTON == intent.action && (player.playQueue == null)) {
110+
if (Intent.ACTION_MEDIA_BUTTON == intent.action && (player?.playQueue == null)) {
120111
/*
121112
No need to process media button's actions if the player is not working, otherwise
122113
the player service would strangely start with nothing to play
@@ -127,8 +118,8 @@ class PlayerService : MediaBrowserServiceCompat() {
127118
return START_NOT_STICKY
128119
}
129120

130-
player.handleIntent(intent)
131-
player.UIs()[MediaSessionPlayerUi::class.java].ifPresent {
121+
player?.handleIntent(intent)
122+
player?.UIs()?.get(MediaSessionPlayerUi::class.java)?.ifPresent {
132123
it.handleMediaButtonIntent(intent)
133124
}
134125

@@ -140,17 +131,17 @@ class PlayerService : MediaBrowserServiceCompat() {
140131
Log.d(TAG, "stopForImmediateReusing() called")
141132
}
142133

143-
if (!player.exoPlayerIsNull()) {
134+
if (player != null && !player!!.exoPlayerIsNull()) {
144135
// Releases wifi & cpu, disables keepScreenOn, etc.
145136
// We can't just pause the player here because it will make transition
146137
// from one stream to a new stream not smooth
147-
player.smoothStopForImmediateReusing()
138+
player?.smoothStopForImmediateReusing()
148139
}
149140
}
150141

151142
override fun onTaskRemoved(rootIntent: Intent) {
152143
super.onTaskRemoved(rootIntent)
153-
if (!player.videoPlayerSelected()) {
144+
if (player != null && !player!!.videoPlayerSelected()) {
154145
return
155146
}
156147
onDestroy()
@@ -166,14 +157,15 @@ class PlayerService : MediaBrowserServiceCompat() {
166157

167158
cleanup()
168159

169-
mediaBrowserConnector?.release()
170-
mediaBrowserConnector = null
160+
mediaBrowserConnector.release()
161+
_mediaBrowserConnector = null
171162

172-
compositeDisposableLoadChildren.clear()
163+
disposables.clear()
173164
}
174165

175166
private fun cleanup() {
176-
player.destroy()
167+
player?.destroy()
168+
player = null
177169
}
178170

179171
fun stopService() {
@@ -187,7 +179,7 @@ class PlayerService : MediaBrowserServiceCompat() {
187179

188180
override fun onBind(intent: Intent): IBinder = mBinder
189181

190-
// MediaBrowserServiceCompat methods
182+
// MediaBrowserServiceCompat methods (they defer function calls to mediaBrowserConnector)
191183
override fun onGetRoot(
192184
clientPackageName: String,
193185
clientUid: Int,
@@ -204,7 +196,7 @@ class PlayerService : MediaBrowserServiceCompat() {
204196
it.onLoadChildren(parentId).subscribe { mediaItems ->
205197
result.sendResult(mediaItems)
206198
}
207-
compositeDisposableLoadChildren.add(disposable)
199+
disposables.add(disposable)
208200
}
209201
}
210202

app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,20 @@ class MediaBrowserConnector(
125125

126126
private fun createPlaylistMediaItem(playlist: PlaylistLocalItem): MediaBrowserCompat.MediaItem {
127127
val builder = MediaDescriptionCompat.Builder()
128-
val isRemote = playlist is PlaylistRemoteEntity
129-
builder.setMediaId(createMediaIdForInfoItem(isRemote, playlist.uid))
128+
builder
129+
.setMediaId(createMediaIdForInfoItem(playlist is PlaylistRemoteEntity, playlist.uid))
130130
.setTitle(playlist.orderingName)
131-
.setIconUri(Uri.parse(playlist.thumbnailUrl))
131+
.setIconUri(playlist.thumbnailUrl?.let { Uri.parse(it) })
132132

133133
val extras = Bundle()
134134
extras.putString(
135135
MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
136-
playerService.resources.getString(R.string.tab_bookmarks)
136+
playerService.resources.getString(R.string.tab_bookmarks),
137137
)
138138
builder.setExtras(extras)
139139
return MediaBrowserCompat.MediaItem(
140140
builder.build(),
141-
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE
141+
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE,
142142
)
143143
}
144144

0 commit comments

Comments
 (0)