问题描述
希望您能提供一些指导.我创建了一个媒体播放器(mp1)的最终"实例,然后单击按钮以播放mp3文件.然后,当我单击第二个按钮时,我需要停止文件.在我尝试再次播放文件之前,此方法可以正常工作-没有任何反应.我认为因为mp1实例是最终的",所以当我停止它时,它会永久停止,直到我重新启动该应用程序为止.我不想暂停文件,我想停止它然后重新启动它.任何想法都欢迎.我尝试将mp1创建放在按钮内.在应用程序崩溃之前一直有效-可能是因为创建多个mediaplayer占用了所有设备内存?
Im hoping you can provide some guidance. I created a 'final' instance of a mediaplayer (mp1) and on a button click it plays a mp3 file. I then need to stop the file when I click a second button. This works fine until I try to play the file again - nothing happens. I think that because the mp1 instance is 'final', when I stop it, it stops for good until I relaunch the app. I dont want to pause the file, I want to stop it and then restart it afresh. Any ideas welcome. I tried putting the mp1 creation within the button. This worked until the app crashed - probably because multiple mediaplayer creations used all the device memory?
谢谢!
// const mediaplayer
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.mysound);
...
// in button 1
if (radSound1.isChecked()) {
radSound2.setChecked(false); // ...set radiobutton 2 to false
mp1.start(); // ...play the mp3 file
}
...
// in button 2
if (mp1 != null){
mp1.reset();
//mp1.setDataSource();
// mp1.prepare();
}
推荐答案
按照状态图此处一个>. stop()
停止播放并使您进入停止状态.在下次开始播放之前,您应该先调用 prepare()
,然后再次调用 start()
.
Follow the state diagram here. stop()
stops playing and puts you in stopped state. Before starting to play for the next time, you should call prepare()
and then start()
again.
因此,在按钮1中,您应该调用 start()
,在按钮2中,您应调用 stop()
和 prepare()
.初始化很好,只需执行一次,然后将其保留在按钮之外即可.
So in button 1, you should call start()
and in button 2, you should call stop()
and prepare()
. The initialization is fine, do it once and keep it outside the buttons.
还接受您对此类问题的回答,以便将来像我这样的人更有动力回答这些问题.
Also accept answers to your questions including this so that people like me will be more motivated to reply to them in the future.
这篇关于使用Mediaplayer重放相同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!