diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/MyWidgetProvider.kt index fb223133..4bdfd766 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/MyWidgetProvider.kt @@ -17,6 +17,7 @@ import com.simplemobiletools.musicplayer.extensions.getSharedPrefs import com.simplemobiletools.musicplayer.extensions.sendIntent import com.simplemobiletools.musicplayer.models.Events import com.simplemobiletools.musicplayer.models.Song +import com.simplemobiletools.musicplayer.services.MusicService import com.squareup.otto.Bus import com.squareup.otto.Subscribe @@ -63,6 +64,8 @@ class MyWidgetProvider : AppWidgetProvider() { mBus = BusProvider.instance } registerBus() + updateSong(MusicService.mCurrSong) + updateSongState(MusicService.getIsPlaying()) } private fun setupIntent(action: String, id: Int) { @@ -84,7 +87,11 @@ class MyWidgetProvider : AppWidgetProvider() { @Subscribe fun songChangedEvent(event: Events.SongChanged) { - mCurrSong = event.song + updateSong(event.song) + } + + private fun updateSong(song: Song?) { + mCurrSong = song updateSongInfo() updateWidgets() } @@ -103,10 +110,14 @@ class MyWidgetProvider : AppWidgetProvider() { @Subscribe fun songStateChanged(event: Events.SongStateChanged) { - if (mIsPlaying == event.isPlaying) + updateSongState(event.isPlaying) + } + + private fun updateSongState(isPlaying: Boolean) { + if (mIsPlaying == isPlaying) return - mIsPlaying = event.isPlaying + mIsPlaying = isPlaying updatePlayPauseButton() updateWidgets() } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt index 95599a78..256ac961 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt @@ -57,6 +57,8 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr private var mPlayPauseIntent: PendingIntent? = null private var mWasPlayingAtCall = false + + fun getIsPlaying() = mPlayer?.isPlaying == true } override fun onCreate() { @@ -319,8 +321,6 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr } } - fun getIsPlaying() = mPlayer?.isPlaying == true - fun playPreviousSong() { if (mSongs!!.isEmpty()) return