如何解决ExoPlayer OkHttpDataSource传输侦听器和进度通知不起作用
我正在使用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 举报,一经查实,本站将立刻删除。