如何一键暂停/播放多个 MediaPlayer 对象?
Posted
技术标签:
【中文标题】如何一键暂停/播放多个 MediaPlayer 对象?【英文标题】:How to pause/play multiple MediaPlayer objects with one button? 【发布时间】:2019-10-27 13:11:03 【问题描述】:我有一个按钮,我想用那个按钮控制多个 MediaPlayer 对象。 在示例中,用户从列表中选择了 3 种声音,当他们正在播放时,用户单击了暂停图标按钮,所有声音都应该暂停(并且暂停按钮的资源应该变为播放图标)。如果用户再次点击播放图标,所有声音都应该继续播放。
如果我删除代码中的 else 部分,我可以实现当前正在播放的暂停声音,但如果我再次点击播放按钮,声音将不会播放,也不会发生任何事情。
我的 XML:
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<ImageButton
android:id="@+id/stop_all_sound"
android:layout_
android:layout_
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
的图标将为play_button,否则pause_button
当按下Button
时,我想要MediaPlayer
s 的以下行为:
【问题讨论】:
【参考方案1】:更新答案
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
【讨论】:
除了第二种情况外,所有情况都是正确的,因为当用户从列表中选择一首歌曲时,它已经在播放了。正如我在上面所说的,如果用户单击暂停按钮,然后暂停按钮将变为播放按钮,我想暂停当前正在播放的声音。如果用户再次点击该播放按钮,所有暂停的歌曲都应该继续播放。 我在上面尝试了你的代码,它暂停了正在播放的声音,但它正在播放没有播放的声音。以上是关于如何一键暂停/播放多个 MediaPlayer 对象?的主要内容,如果未能解决你的问题,请参考以下文章