I have a list of urls in a mutable list and I want to perform an IO operation, cacheVideo on each of the urls sequentially one after the other
suspend fun cacheVideo(mediaItem: MediaItem) = {
val videoUrl = mediaItem.mediaUrl
val uri = Uri.parse(videoUrl)
val dataSpec = DataSpec(uri)
val progressListener =
CacheUtil.ProgressListener { requestLength, bytesCached, newBytesCached ->
val downloadPercentage: Double = (bytesCached * 100.0
/ requestLength)
if (downloadPercentage == 100.0) {
// I WANT TO RETURN HERE
}
}
try {
CacheUtil.cache(
dataSpec,
cache,
DataSourceFactory?.createDataSource(),
progressListener,
null
);
} catch (err: Exception) {
// IF ERROR, THEN RETURN NULL
}
}
How would I shape the cacheVideo to do that using Coroutines?
uiScope.launch {
for(item in mediaItems){
cacheVideo(item) // I WANT TO WAIT HERE BEFORE GOING TO NEXT ITEM
}
}