gpt4 book ai didi

android - zip 列表完成

转载 作者:行者123 更新时间:2023-11-29 23:25:38 27 4
gpt4 key购买 nike

我有一个包含媒体播放器的 Sound 类,我想编写一个函数来接收声音列表并全部播放,该函数应该返回一个可完成

interface MediaPlayer {
fun play(): Completable
}

class Sound(val title, val mediaPlayer: MediaPlayer)

//In other class, we have a list of sound to play
val soundList = List<Sound>(mockSound1, mockSound2,..,mockSound10)

fun playSound(): Completable {
return mockSound1.play()
}

fun playAllSounds(): Completable {
soundList.forEach(sound -> sound.mediaPlayer.play()) //Each of this will return Completable.

//HOW to return Completable
return ??? do we have somthing like zip(listOf<Completable>)
}


//USE
playSound().subscrible(...) //Works well

playAllSounds().subscribe()???

最佳答案

您可以使用 concat,来自文档

Returns a Completable which completes only when all sources complete, one after another.

你可以这样做:

fun playAllSounds(): Completable {
val soundsCompletables = soundList.map(sound -> sound.mediaPlayer.play())
return Completable.concat(soundCompletables)
}

引用:http://reactivex.io/RxJava/javadoc/io/reactivex/Completable.html#concat-java.lang.Iterable-

关于android - zip 列表完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53616800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com