-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pull different steps in the codelab into modules #63
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great. I tried pretty hard to find issues with the code but could only find one thing! I've asked borrelli@ to take a look as well.
Please don't merge until the codelab itself has been updated as well.
app/build.gradle
Outdated
@@ -24,5 +25,6 @@ android { | |||
} | |||
|
|||
dependencies { | |||
implementation project(":player-lib") | |||
// Switch out the code lab step to jump to different points in the code lab | |||
implementation project(':exoplayer-codelab-04') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implementation project(':exoplayer-codelab-04') | |
implementation project(':exoplayer-codelab-00') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, and force pushed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good =D I made some comments about using some of the more helpful parts of AndroidX. Other stuff is much smaller.
private fun initializePlayer() { | ||
player = SimpleExoPlayer.Builder(this) | ||
.build() | ||
.also { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider an explicit name for it
or even just getting rid of the also
block, since this doesn't all need to be in one statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opted for it
based on conversations here to be more idiomatic: #62 as the more explicit names can be a bit noisy. It could also just be that I didn't choose very good explicit names lmao.
What would you suggest naming it
more explicitly as?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some alternatives:
- Use
apply
instead ofalso
. This would avoid usingit
. - Rename
it
toexoPlayer
sinceit
is the ExoPlayer implementation of thePlayer
interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opted for apply { -> exoPlayer }
as the semantics of this
got hard to discern because of the playWhenReady
var
.
It would have required:
apply {
playWhenReady = this@PlayerActivity.playWhenReady
}
} | ||
|
||
private fun releasePlayer() { | ||
player?.let { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the above, providing a specific name for it
import com.google.android.exoplayer2.util.MimeTypes | ||
import com.google.android.exoplayer2.util.Util | ||
|
||
private val TAG = PlayerActivity::class.java.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest just putting in a static string here, which would allow making this a private const val
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_player); | ||
} | ||
private val viewBinding by lazy { ActivityPlayerBinding.inflate(layoutInflater) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider moving this to be a local variable in onCreate -- I know it's used beyond onCreate in subsequent steps, so I'll explain more there.
public override fun onStart() { | ||
super.onStart() | ||
if (Util.SDK_INT > 23) { | ||
initializePlayer() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of calling these inline like this, consider creating a LifecycleObserver
. It could be initialized in onCreate
and added as an observer to the activity with getLifecycle().addObserver(...)
.
If I were building it, I might consider having two parameters to the observer -- the 'init' and 'release' lifecycle states. Then, in the observer, check the state and call the appropriate method. (This way there wouldn't have to be duplicate code to init/release the player in onStart/onResume and onPause/onStop)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered this, however my major prompt was to keep it super simple so users can copy paste, and also to keep it as close to the original java codelab as possible as this is a first pass kotlin conversion.
After the convergence implementation, I can then explore using androidx dependencies like in lifecycle observers, and then even plausibly move the exoplayer instance to a ViewModel
.
I can't do that at the moment as it would require extra dependencies, and even more edits to the codelab doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good suggestion but agree that we might want to do this in a follow up PR. I would file a separate issue on this repo so that this suggestion doesn't get forgotten.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #65
delete rootProject.buildDir | ||
} | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'com.google.android.exoplayer:exoplayer-core:2.12.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe consider moving 2.12.0
to a gradle variable at the top level, since these are used in each of the modules, and they're all the same version (unless this might change in the future) -- even then, though, it might be useful to have a core, dash, and ui version in that case, so they only have to be updated in one place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this affect the user's ability to copy paste? I considered doing this as well as I do it with the kotlin version, but as the user may be jumping in between modules, I decided to err on the side of copy pasting convenience.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't get too caught up on copypastability. Maintainability and readability is also important. In this instance I agree with @nic0lette that moving to gradle variables is the better solution since it avoids duplicating the same version string throughout the code, although this could also be done in a follow up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will create an issue as an action item.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #66
} | ||
|
||
private fun releasePlayer() { | ||
player?.let { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
player?.run {
playbackPosition = this.currentPosition
currentWindow = this.currentWindowIndex
playWhenReady = this.playWhenReady
removeListener(playbackStateListener)
release()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using run
is marginally better here as it avoids it
@tunjid Thanks for all your hard work on this. Please rebase and merge as soon as the codelab has been updated. |
A single PR against a copy of master with all the commits squashed; closes out #60, #61 & #62
Commit summary:
Initial kotlin conversion
Update kotlin to be more idiomatic
More idiomatic kotlin