问题描述
我正在使用Exoplayer DownloadService
进行媒体下载。当我为OkHttpDataSourceFactory
设置DownloadManager
时,TransferListener
未显示下载进度,Notification
(进度通知)未更新,但TransferListener
和Notification
(进度通知)可以与DefaultHttpDataSourceFactory
配合使用。
下载服务:
class MyDownloadService : DownloadService {
//...
override fun getDownloadManager(): DownloadManager {
val myApplication = application as MyApplication
return DownloadManager(
applicationContext,myApplication.getDatabaseProvider(),//singleton
myApplication.getDownloadCache(),//singleton
myApplication.getDataSourceFactory() //singleton
).apply {
addListener(
DownloadStateListener(
applicationContext,myApplication.getNotificationHelper(),FOREGROUND_NOTIFICATION_ID
)
)
}
}
override fun getForegroundNotification(downloads: MutableList<Download>): Notification {
//...
val myApplication = application as MyApplication
return myApplication.getNotificationHelper()
.buildProgressNotification(R.drawable.ic_android,null,downloads)
}
//...
class DownloadStateListener(
private val context: Context,private val notificationHelper: DownloadNotificationHelper,firstNotificationId: Int
) : DownloadManager.Listener {
private var nextNotificationId = firstNotificationId
override fun onDownloadChanged(downloadManager: DownloadManager,download: Download) {
val notification: Notification
if (download.state == Download.STATE_COMPLETED) {
notification = notificationHelper
.buildDownloadCompletedNotification(R.drawable.ic_android,null)
} else if (download.state == Download.STATE_FAILED) {
notification = notificationHelper
.buildDownloadFailedNotification(R.drawable.ic_android,null)
} else
return
NotificationUtil.setNotification(context,nextNotificationId++,notification)
}
}
}
OkHttpDataSourceFactory
的实例:
fun getDataSourceFactory(): OkHttpDataSourceFactory =
if (::dataSourceFactory.isInitialized) dataSourceFactory
else OkHttpDataSourceFactory(
getOkHttpClient(),applicationContext.packageName,object : TransferListener {
override fun onTransferInitializing(source: DataSource?,dataSpec: DataSpec?,isNetwork: Boolean) {
Log.v("exoplayerDownload","initializing")
}
override fun onTransferStart(source: DataSource?,"started")
}
override fun onTransferEnd(source: DataSource?,"finished")
}
override fun onBytesTransferred(source: DataSource?,isNetwork: Boolean,bytesTransferred: Int) {
Log.v("exoplayerDownload","downloading")
}
},/*cache control=*/ null
).apply { dataSourceFactory = this }
日志:
//on download start
V/exoplayerDownload: initializing
//on download finish
V/exoplayerDownload: started
V/exoplayerDownload: downloading
V/exoplayerDownload: downloading
...
V/exoplayerDownload: downloading
V/exoplayerDownload: downloading
V/exoplayerDownload: finished
如何在OkHttpDataSourceFactory
之类的DefaultHttpDataSourceFactory
上获得下载进度?
感谢您的帮助
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)