我有一个按钮,我想用那个按钮控制多个MediaPlayer对象。 例如,用户从列表中选择了3种声音,并且在播放时,用户单击了暂停图标按钮,所有声音应暂停(并且暂停按钮的资源应更改为播放图标)。如果用户再次单击播放图标,则所有声音应继续进行。
如果我删除代码中的else部分,我可以实现当前正在播放的暂停声音,但是如果我再次单击播放按钮,声音将不会播放,并且什么也没有发生。
我的XML:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/stop_all_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/pause_button" />
</LinearLayout>
我的代码:
stop_all_sound.setOnClickListener {
// List of sounds
val list = arrayListOf(sound1, sound2, sound3, sound4)
// List for playing sounds
val playingSounds = arrayListOf<MediaPlayer>()
list.forEach { sound ->
if (sound.isPlaying) {
// If sound is playing add it to the playingSounds list
playingSounds.add(sound)
for (playingSound in playingSounds) {
// Pause that playing sounds currently
playingSound.pause()
stop_all_sound.setImageResource(R.drawable.play_button)
}
}
// I couldn't figured out this part. I guess playingSounds list is empty
else {
for (playingSound in playingSounds) {
playingSound.start()
stop_all_sound.setImageResource(R.drawable.pause_button)
}
}
}
}
为了更加清楚,假设有MediaPlayer
和Button
的列表
MediaPlayer
处于播放状态,则Button
的图标将为播放按钮,否则为暂停按钮当按下MediaPlayer
时,我想要Button
的以下行为:
var buttonIcon: Int = R.drawable.play_button
val playList = PlayList { isPlaying ->
buttonIcon =
if (isPlaying) R.drawable.pause_button
else R.drawable.play_button
stop_all_sound.setImageResource(buttonIcon)
}
stop_all_sound.setOnClickListener {
// List of sounds
val list = arrayListOf(sound1, sound2, sound3, sound4)
if (playList.isEmpty()) {
buttonIcon = R.drawable.play_button
list.forEach { sound ->
if (sound.isPlaying) {
playList.add(sound)
buttonIcon = R.drawable.pause_button
}
}
if (playList.isEmpty()) list.forEach { playList.add(it) }
}
when (buttonIcon) {
R.drawable.play_button -> playList.play()
R.drawable.pause_button -> playList.pause()
}
}
// PlayList class
class PlayList(private val playingState: (Boolean) -> Unit) {
private val soundList = mutableListOf<MediaPlayer>()
fun isEmpty() = soundList.isEmpty()
fun add(sound: MediaPlayer) {
sound.setOnCompletionListener {
soundList.remove(it)
if (soundList.isEmpty()) playingState(false)
}
soundList.add(sound)
}
fun play() {
soundList.forEach {
it.start()
}
playingState(true)
}
fun pause() {
soundList.forEach {
if (it.isPlaying) it.pause()
}
playingState(false)
}
}
我可以从您的问题中找出以下测试案例,
在上述情况下,我可能会执行以下操作:
var buttonIcon = R.drawable.play_button
val playList = PlayList(arrayListOf(sound1, sound2, sound3, sound4)) { isPlaying ->
if (isPlaying) {
buttonIcon = R.drawable.pause_button
stop_all_sound.setImageResource(buttonIcon)
} else {
buttonIcon = R.drawable.play_button
stop_all_sound.setImageResource(buttonIcon)
}
}
stop_all_sound.setOnClickListener {
when(buttonIcon) {
R.drawable.play_button -> playList.play()
R.drawable.pause_button -> playList.pause()
}
}
PlayList
类
class PlayList(
private val soundList: List<MediaPlayer>,
private val playingState: (Boolean) -> Unit
) {
private var nowPlayingCount = 0
init {
soundList.forEach {
it.setOnCompletionListener {
nowPlayingCount = nowPlayingCount.dec()
if (nowPlayingCount == 0) playingState(false)
}
}
}
fun play() {
soundList.forEach {
it.start()
}
playingState(true)
nowPlayingCount = soundList.size
}
fun pause() {
soundList.forEach {
if (it.isPlaying) it.pause()
}
playingState(false)
nowPlayingCount = 0
}
}