Skip to content

Commit

Permalink
Merge pull request #307 from boostcampwm2023/android/feature/273
Browse files Browse the repository at this point in the history
이전, 다음 곡 이동 로직 개선
  • Loading branch information
2taezeat authored Dec 7, 2023
2 parents 59bf426 + 2ff31c3 commit e548b62
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import com.ohdodok.catchytape.feature.player.PlayerViewModel
import com.ohdodok.catchytape.feature.player.getMediasWithMetaData
import com.ohdodok.catchytape.feature.player.millisecondsPerSecond
import com.ohdodok.catchytape.feature.player.moveNextMedia
import com.ohdodok.catchytape.feature.player.movePreviousMedia
import com.ohdodok.catchytape.feature.player.navigateToPlayer
import com.ohdodok.catchytape.feature.player.onPreviousBtnClick
import com.ohdodok.catchytape.mediasession.PlaybackService
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -188,7 +188,7 @@ class MainActivity : AppCompatActivity() {

private fun setupPreviousButton() {
binding.pcvController.setOnPreviousButtonClick {
player.movePreviousMedia()
player.onPreviousBtnClick()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PlayerFragment : BaseFragment<FragmentPlayerBinding>(R.layout.fragment_pla
}

binding.ibPrevious.setOnClickListener {
player.movePreviousMedia()
player.onPreviousBtnClick()
}

binding.btnAddToPlaylist.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ class PlayerListener(
listener.onPlayingChanged(player.isPlaying)
}

events.containsAny(Player.EVENT_TIMELINE_CHANGED, Player.EVENT_MEDIA_ITEM_TRANSITION) -> {
events.contains(Player.EVENT_MEDIA_ITEM_TRANSITION) -> {
val durationMs = player.duration.toInt()
listener.onMediaItemChanged(player.currentMediaItemIndex, durationMs / millisecondsPerSecond)
}

events.contains(Player.EVENT_TRACKS_CHANGED) -> {
val durationMs = player.duration.toInt()
listener.onMediaItemChanged(player.currentMediaItemIndex, durationMs / millisecondsPerSecond)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import androidx.media3.common.MediaMetadata
import androidx.media3.exoplayer.ExoPlayer
import com.ohdodok.catchytape.core.domain.model.Music


private const val POSITION_MOVE_HEAD_STANDARD = 0.05

fun ExoPlayer.moveNextMedia() {
seekToNextMediaItem()
if (isPlaying) {
Expand All @@ -20,6 +23,19 @@ fun ExoPlayer.movePreviousMedia() {
}
}

fun ExoPlayer.moveHeadMedia() {
seekTo(0)
play()
}

fun ExoPlayer.onPreviousBtnClick() {
if (currentPosition.toFloat() / duration.toFloat() < POSITION_MOVE_HEAD_STANDARD) {
movePreviousMedia()
} else {
moveHeadMedia()
}
}

fun getMediasWithMetaData(musics: List<Music>): List<MediaItem> {
val mediaItemBuilder = MediaItem.Builder()
val mediaMetadataBuilder = MediaMetadata.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class PlayerViewModel @Inject constructor(
it.copy(
duration = duration,
currentMusic = playlist.musics[index],
currentPositionSecond = 0,
isNextEnable = playlist.musics.lastIndex != index,
isPreviousEnable = index != 0
)
Expand Down

0 comments on commit e548b62

Please sign in to comment.