Skip to content

Commit

Permalink
привязываем сервис
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayRyabcev committed Apr 17, 2024
1 parent d58f6dd commit aac9510
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +22,7 @@ class MusicService : Service(), AudioPlayerControl {
private val binder = MusicServiceBinder()

private val _playerState = MutableStateFlow<PlayerState>(PlayerState.Default)
val playerServiceState = _playerState.asStateFlow()
private val playerServiceState = _playerState.asStateFlow()

private var songUrl = ""

Expand All @@ -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
}

Expand All @@ -66,6 +68,7 @@ class MusicService : Service(), AudioPlayerControl {
}
mediaPlayer?.setOnCompletionListener {
_playerState.value = PlayerState.Prepared
Log.d("плеер", "prepared")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class PlaybackButtonView @JvmOverloads constructor(
}

override fun performClick(): Boolean {

return super.performClick()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 -> {}
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
timeJob = viewModelScope.launch {
while (true) {
delay(PLAYER_BUTTON_PRESSING_DELAY)
playerInteractor.getTime().collect() {
timer.postValue(it)
}
}
}
return timer
}*/

/* fun putTime(): LiveData<String> {
getTimeFromInteractor()
return timer
}*/

fun onFavoriteClicked(track: Track) {
if (track.isFavorite) {
track.trackId?.let { favouritesInteractor.favouritesDelete(track) }
Expand Down

0 comments on commit aac9510

Please sign in to comment.