android - 邮编清单可填写
问题描述
我有一个包含媒体播放器的 Sound 类,我想编写一个函数来接收声音列表并全部播放,该函数应该返回一个 Completable
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
, 从文档
返回一个 Completable,它仅在所有源一个接一个地完成时才完成。
您可以执行以下操作:
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-
推荐阅读
- c# - 如何从c#中的给定字符串中获取日期
- django - 使用 URL Dispatcher 遇到 404 错误
- microsoft-graph-api - 使用 Microsoft Graph API 使用电子邮件别名获取用户主电子邮件地址
- kubernetes - 在 Kubernetes 的路径中使用变量
- python - Python Tkinter - 使用 ttk.Notebook 和 tkinter.Text 打开多个文件
- bash - 在 SLURM 上运行带有命令行参数的命令
- node.js - CheerioJS 变得具体
- 标题文本“我想要什么”
- sql - 如何使用字段 Postgres 更新表
- swift - 如何在特定时间安排通知,然后每 x 时间重复一次?
- c++ - 尝试使用 Box2D 的 b2Body::SetUserData(void*) 时未定义的引用