From aac951055bc8a7b3115f7fe1ebb4eab7c7d5f117 Mon Sep 17 00:00:00 2001 From: NikolayRyabcev Date: Thu, 18 Apr 2024 01:07:58 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=B2=D1=8F=D0=B7=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D0=B5=D0=BC=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../playlistmaker/services/MusicService.kt | 5 ++- .../player/buttonView/PlaybackButtonView.kt | 1 + .../ui/player/fragment/PlayerFragment.kt | 21 +++++----- .../ui/player/view_model/PlayerViewModel.kt | 41 ------------------- 4 files changed, 15 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/example/playlistmaker/services/MusicService.kt b/app/src/main/java/com/example/playlistmaker/services/MusicService.kt index a7023538..09ea2b6b 100644 --- a/app/src/main/java/com/example/playlistmaker/services/MusicService.kt +++ b/app/src/main/java/com/example/playlistmaker/services/MusicService.kt @@ -3,6 +3,7 @@ import android.content.Intent import android.media.MediaPlayer import android.os.Binder import android.os.IBinder +import android.util.Log import com.example.playlistmaker.domain.player.PlayerState import com.example.playlistmaker.services.AudioPlayerControl import kotlinx.coroutines.CoroutineScope @@ -21,7 +22,7 @@ class MusicService : Service(), AudioPlayerControl { private val binder = MusicServiceBinder() private val _playerState = MutableStateFlow(PlayerState.Default) - val playerServiceState = _playerState.asStateFlow() + private val playerServiceState = _playerState.asStateFlow() private var songUrl = "" @@ -46,6 +47,7 @@ class MusicService : Service(), AudioPlayerControl { override fun onBind(intent: Intent?): IBinder? { songUrl = intent?.getStringExtra("song_url") ?: "" initMediaPlayer() + Log.d("плеер", "bind") return binder } @@ -66,6 +68,7 @@ class MusicService : Service(), AudioPlayerControl { } mediaPlayer?.setOnCompletionListener { _playerState.value = PlayerState.Prepared + Log.d("плеер", "prepared") } } diff --git a/app/src/main/java/com/example/playlistmaker/ui/player/buttonView/PlaybackButtonView.kt b/app/src/main/java/com/example/playlistmaker/ui/player/buttonView/PlaybackButtonView.kt index 7728cc35..7be46e72 100644 --- a/app/src/main/java/com/example/playlistmaker/ui/player/buttonView/PlaybackButtonView.kt +++ b/app/src/main/java/com/example/playlistmaker/ui/player/buttonView/PlaybackButtonView.kt @@ -65,6 +65,7 @@ class PlaybackButtonView @JvmOverloads constructor( } override fun performClick(): Boolean { + return super.performClick() } diff --git a/app/src/main/java/com/example/playlistmaker/ui/player/fragment/PlayerFragment.kt b/app/src/main/java/com/example/playlistmaker/ui/player/fragment/PlayerFragment.kt index b52a5f70..932236e9 100644 --- a/app/src/main/java/com/example/playlistmaker/ui/player/fragment/PlayerFragment.kt +++ b/app/src/main/java/com/example/playlistmaker/ui/player/fragment/PlayerFragment.kt @@ -9,6 +9,7 @@ import android.content.IntentFilter import android.content.ServiceConnection import android.os.Bundle import android.os.IBinder +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.View.VISIBLE @@ -77,12 +78,13 @@ class PlayerFragment : Fragment() { ) //переключение кнопок плэй/пауза binding.playButton.isEnabled = false - + binding.playButton.setOnClickListener { playerViewModel.onPlayerButtonClicked() } //привязка сервиса муз плеера playerViewModel.observePlayerState().observe(viewLifecycleOwner) { updateButton() + binding.trackTimer.text = musicService?.getCurrentPlayerPosition() ?: "00:00" } - bindMusicService() + bindMusicService(requireContext()) return binding.root } @@ -205,8 +207,8 @@ class PlayerFragment : Fragment() { is PlayerState.Prepared -> { if (isFirstPlay) { - binding.playButton.onTouchListener = - { playerViewModel.onPlayerButtonClicked() } + /* binding.playButton.onTouchListener = + { playerViewModel.onPlayerButtonClicked() }*/ isFirstPlay = false binding.playButton.alpha = 1f } else { @@ -216,11 +218,9 @@ class PlayerFragment : Fragment() { is PlayerState.Paused -> { binding.playButton.alpha = 1f - binding.trackTimer.text = musicService?.getCurrentPlayerPosition() ?: "00:00" } is PlayerState.Playing -> { - binding.trackTimer.text = musicService?.getCurrentPlayerPosition() ?: "00:00" } null -> {} @@ -267,8 +267,6 @@ class PlayerFragment : Fragment() { } } - private var state: PlayerState = PlayerState.Default - private val serviceConnection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName?, service: IBinder?) { val binder = service as MusicService.MusicServiceBinder @@ -280,11 +278,12 @@ class PlayerFragment : Fragment() { } } - private fun bindMusicService() { - val intent = Intent(requireContext(), MusicService::class.java).apply { + private fun bindMusicService(context: Context) { + Log.d("плеер", "bindService") + val intent = Intent(context, MusicService::class.java).apply { putExtra("song_url", url) } - requireActivity().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE) + context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE) } private fun unBindMusicService() { diff --git a/app/src/main/java/com/example/playlistmaker/ui/player/view_model/PlayerViewModel.kt b/app/src/main/java/com/example/playlistmaker/ui/player/view_model/PlayerViewModel.kt index ae842599..7992a300 100644 --- a/app/src/main/java/com/example/playlistmaker/ui/player/view_model/PlayerViewModel.kt +++ b/app/src/main/java/com/example/playlistmaker/ui/player/view_model/PlayerViewModel.kt @@ -56,47 +56,6 @@ class PlayerViewModel( audioPlayerControl = null } - - /*fun createPlayer(url: String) { - playerInteractor.createPlayer(url, listener = object : PlayerStateListener { - override fun onStateChanged(state: PlayerState) { - stateLiveData.postValue(state) - } - }) - } - - - fun play() { - playerInteractor.play() - timeJob!!.start() - } - - fun pause() { - playerInteractor.pause() - } - - fun destroy() { - timeJob?.cancel() - playerInteractor.destroy() - } - - private fun getTimeFromInteractor(): LiveData { - timeJob = viewModelScope.launch { - while (true) { - delay(PLAYER_BUTTON_PRESSING_DELAY) - playerInteractor.getTime().collect() { - timer.postValue(it) - } - } - } - return timer - }*/ - - /* fun putTime(): LiveData { - getTimeFromInteractor() - return timer - }*/ - fun onFavoriteClicked(track: Track) { if (track.isFavorite) { track.trackId?.let { favouritesInteractor.favouritesDelete(track) }