[英]Changing audio button from play to pause when clicked and vice versa
I want a play button in my app that when clicked plays the audio and changes to pause, and when the pause button is clicked the audio stops and the button changes to play again and so on. 我想要我的应用程序中的一个播放按钮,当单击该按钮时,它会播放音频并更改为暂停,而当单击暂停按钮时,音频会停止并且该按钮更改为再次播放,依此类推。 But the button is not working as expected. 但是该按钮无法正常工作。 Please help. 请帮忙。 I'm adding my java code below. 我在下面添加我的Java代码。
public class surah extends AppCompatActivity {
MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surah);
mp=MediaPlayer.create(surah.this, R.raw.surahkahf);
final ImageView audio = (ImageView)findViewById(R.id.btn);
audio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp.isPlaying()){
mp.stop();
audio.setBackgroundResource(R.drawable.play);
try {
mp.prepare();
}catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
mp.start();
audio.setImageResource(R.drawable.pause);
}
}
});
}
}
First, as aborocz mentions in comments, you probably intend to pause playback instead of stop it, so the method you want to use is pause()
. 首先,正如aborocz在评论中提到的那样,您可能打算暂停播放而不是停止播放,因此要使用的方法是pause()
。 In that case you would not need to prepare the MediaPlayer
again, and it will start from the same place it was paused when playback is resumed. 在这种情况下,您无需再次准备MediaPlayer
,它将从恢复播放时暂停的位置开始。
Second, the isPlaying()
method is not particularly appropriate for this purpose. 其次, isPlaying()
方法不适用于此目的。 There are race conditions that prevent the desired behavior. 有些比赛条件阻止了期望的行为。 From the Android MediaPlayer
documentation : 从Android MediaPlayer
文档中 :
Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. 请注意,在播放器引擎中,异步发生从“开始”状态到“暂停”状态的转换,反之亦然。 It may take some time before the state is updated in calls to isPlaying(), and it can be a number of seconds in the case of streamed content. 在对isPlaying()的调用中更新状态之前,可能需要花费一些时间,对于流式传输的内容,状态更新可能需要花费几秒钟的时间。
Instead, store your own boolean value. 而是存储您自己的布尔值。
public class surah extends AppCompatActivity {
private MediaPlayer mp;
private boolean isMediaPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surah);
mp = MediaPlayer.create(surah.this, R.raw.surahkahf);
final ImageView audio = (ImageView)findViewById(R.id.btn);
audio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isMediaPlaying) {
mp.pause();
audio.setBackgroundResource(R.drawable.play);
isMediaPlaying = false;
} else {
mp.start();
audio.setImageResource(R.drawable.pause);
isMediaPlaying = true;
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.