Skip to content

Commit

Permalink
Goonj error while playing track in background fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
kshivang committed Nov 12, 2019
1 parent d335c2c commit 84a22e8
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 63 deletions.
14 changes: 6 additions & 8 deletions app/src/main/java/ai/rever/goonjexample/MidActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ai.rever.goonjexample

import ai.rever.goonj.Goonj
import ai.rever.goonj.download.GoonjDownloadManager.addDownload
import ai.rever.goonj.download.GoonjDownloadManager.isTrackDownloaded
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
Expand Down Expand Up @@ -39,28 +37,28 @@ class MidActivity : AppCompatActivity() {

private fun setupDownloads(){
activity_mid_download1_btn.setOnClickListener {
addDownload(SAMPLES[0])
SAMPLES[0].requestDownload()
}

activity_mid_download2_btn.setOnClickListener {
addDownload(SAMPLES[1])
SAMPLES[1].requestDownload()
}
activity_mid_download3_btn.setOnClickListener {
addDownload(SAMPLES[2])
SAMPLES[2].requestDownload()
}
updateDownloadState()
}

private fun updateDownloadState(){
if(isTrackDownloaded(SAMPLES[0].url)){
if(Goonj.isDownloaded(SAMPLES[0].id)){
activity_mid_download1_btn.visibility = View.GONE
activity_mid_done1_btn.visibility = View.VISIBLE
}
if(isTrackDownloaded(SAMPLES[1].url)){
if(Goonj.isDownloaded(SAMPLES[1].id)){
activity_mid_download2_btn.visibility = View.GONE
activity_mid_done2_btn.visibility = View.VISIBLE
}
if(isTrackDownloaded(SAMPLES[2].url)){
if(Goonj.isDownloaded(SAMPLES[2].id)){
activity_mid_download3_btn.visibility = View.GONE
activity_mid_done3_btn.visibility = View.VISIBLE
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.android.tools.build:gradle:3.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
47 changes: 17 additions & 30 deletions goonj/src/main/java/ai/rever/goonj/Goonj.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@ object Goonj {
runOnSet()
}

private var mServiceConnection: ServiceConnection? = null
private var mServiceConnection: ServiceConnection = object : ServiceConnection{
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
(binder as? GoonjService.Binder)?.goonjPlayerServiceInterface?.let {
goonjPlayerServiceInterface = it
}
}

override fun onServiceDisconnected(name: ComponentName?) {
goonjPlayerServiceInterface = null
}
}

private var runs = ArrayList<GoonjPlayerServiceInterface.() -> Unit>()

Expand Down Expand Up @@ -88,6 +98,7 @@ object Goonj {
*
* Note: GoonjService must added to app manifest
*/

fun <S: GoonjService> register(context: Context, activityIntent: Intent, audioServiceClass: Class<S>) {
if (appContext == null) {
val ctx = if (context.applicationContext == null) context else context.applicationContext
Expand All @@ -98,38 +109,14 @@ object Goonj {
changeActivityIntentForNotification(activityIntent)
}

private fun <T: GoonjService> register(audioServiceClass: Class<T>) {
if(mServiceConnection == null){
mServiceConnection = object : ServiceConnection{
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
(binder as? GoonjService.Binder)?.goonjPlayerServiceInterface?.let {
goonjPlayerServiceInterface = it
}
}

override fun onServiceDisconnected(name: ComponentName?) {
goonjPlayerServiceInterface = null
}

override fun onBindingDied(name: ComponentName?) {
unregister()
register(audioServiceClass)
}
}
}

mServiceConnection?.let {
appContext?.bindService(
Intent(appContext, audioServiceClass),
it, Context.BIND_AUTO_CREATE
)
}
private fun <S: GoonjService> register(audioServiceClass: Class<S>) {
appContext?.bindService(Intent(appContext, audioServiceClass),
mServiceConnection, Context.BIND_AUTO_CREATE
)
}

fun unregister() {
mServiceConnection?.let {
appContext?.unbindService(it)
}
appContext?.unbindService(mServiceConnection)
imageLoader = null
weakContext = null
goonjPlayerServiceInterface = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package ai.rever.goonj.analytics

import ai.rever.goonj.analytics.GoonjAnalytics.logEvent
import android.util.Log.e
import android.view.Surface
import com.google.android.exoplayer2.ExoPlaybackException
import com.google.android.exoplayer2.Format
import com.google.android.exoplayer2.PlaybackParameters
import com.google.android.exoplayer2.analytics.AnalyticsListener
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.decoder.DecoderCounters
import com.google.android.exoplayer2.metadata.Metadata
import com.google.android.exoplayer2.source.MediaSourceEventListener
import com.google.android.exoplayer2.source.TrackGroupArray
import com.google.android.exoplayer2.trackselection.TrackSelectionArray
import java.io.IOException
import java.lang.Exception

object ExoPlayerAnalyticsListenerImp: AnalyticsListener {
override fun onSeekProcessed(eventTime: AnalyticsListener.EventTime?) {
Expand Down Expand Up @@ -52,6 +62,7 @@ object ExoPlayerAnalyticsListenerImp: AnalyticsListener {
)
}


override fun onLoadCompleted(
eventTime: AnalyticsListener.EventTime?,
loadEventInfo: MediaSourceEventListener.LoadEventInfo?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ai.rever.goonj.analytics

import ai.rever.goonj.analytics.GoonjAnalytics.logEvent
import android.util.Log.e
import com.google.android.exoplayer2.ExoPlaybackException
import com.google.android.exoplayer2.PlaybackParameters
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.Timeline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.mediarouter.media.RemotePlaybackClient
open class ItemActionCallbackImp(var operation: String, var onSuccess: ((String?, MediaItemStatus?) -> Unit)? = null) : RemotePlaybackClient.ItemActionCallback() {



override fun onResult(
data: Bundle?,
sessionId: String?,
Expand All @@ -24,7 +23,6 @@ open class ItemActionCallbackImp(var operation: String, var onSuccess: ((String?

override fun onError(error: String?, code: Int, data: Bundle?) {
logError("$operation: failed", error , code)

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.io.File
import java.util.*

enum class DownloadState{
IDLE, DOWNLOAD_CHANGE, DOWNLOADING, INITIALIZED, REQUIREMENT_STATE_CHANGED, DOWNLOAD_REMOVED
IDLE, DOWNLOAD_CHANGE, DOWNLOADING, INITIALIZED, REQUIREMENT_STATE_CHANGED, DOWNLOAD_REMOVED, DOWNLOADED
}

object GoonjDownloadManager {
Expand Down Expand Up @@ -101,4 +101,18 @@ object GoonjDownloadManager {

fun isDownloaded(trackId: String) = downloadManager.downloadIndex
.getDownload(trackId)?.state == STATE_COMPLETED

fun getAllDownloads(): List<String> {
val downloadCursor = downloadManager.downloadIndex.getDownloads(STATE_COMPLETED)
val downloadedTrackList = mutableListOf<String>()
if(downloadCursor.count == 0) {
return downloadedTrackList
}
downloadCursor.moveToFirst()
do {
downloadedTrackList.add(downloadCursor.download.request.id)
} while (downloadCursor.moveToNext())

return downloadedTrackList
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ internal object LocalPlayerNotificationManager {

private val notificationListener = object : PlayerNotificationManager.NotificationListener {

override fun onNotificationStarted(notificationId: Int, notification: Notification?) {
Goonj.startForeground(notificationId, notification)
}
// override fun onNotificationStarted(notificationId: Int, notification: Notification?) {
// Goonj.startForeground(notificationId, notification)
// }

override fun onNotificationCancelled(notificationId: Int) {
Goonj.stopForeground(true)
Expand All @@ -101,6 +101,10 @@ internal object LocalPlayerNotificationManager {
activityIntent,
PendingIntent.FLAG_CANCEL_CURRENT
)

if (ongoing) {
Goonj.startForeground(notificationId, notification)
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion goonj/src/main/java/ai/rever/goonj/models/Track.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ data class TrackState(var index: Int = 0,
var playedAt: Date? = Date(),
var completedAt: Date? = Date(),
var remoteItemId: String? = null): Parcelable {
val progress: Double get() = position.toDouble() / duration.toDouble()
val progress: Double get() = run {
val progress = position.toDouble() / duration.toDouble()
if (progress in 0.0..1.0) {
progress
} else {
2.0
}
}
}


Expand Down
Loading

0 comments on commit 84a22e8

Please sign in to comment.