当声音播放完毕时,我试图将暂停图标更改为播放图标。这里有两个ImageButton,第二个ImageButton可以很好地将暂停图标更改为在声音播放完毕时播放,但是第一个按钮不会更改为在声音播放完毕时播放的图标。
public class MainActivity extends AppCompatActivity {
ImageButton playBtn, playbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (ImageButton) findViewById(R.id.button1);
playbutton= (ImageButton) findViewById(R.id.button2);
final MediaPlayer sound = MediaPlayer.create(this,R.raw.evumm);
playBtn.setBackgroundResource(R.drawable.playicon);
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sound.isPlaying()) {
sound.pause();
playBtn.setBackgroundResource(R.drawable.playicon);
} else {
sound.start();
playBtn.setBackgroundResource(R.drawable.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playBtn.setBackgroundResource(R.drawable.playicon);
}
});
playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sound.isPlaying()) {
sound.pause();
playbutton.setBackgroundResource(R.drawable.playicon);
} else {
sound.start();
playbutton.setBackgroundResource(R.drawable.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playbutton.setBackgroundResource(R.drawable.playicon);
}
});
}
}
这是XML方面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="develop.kokoson.playorstopapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play or Pause !"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:id="@+id/textView" />
<ImageButton
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/playicon"
android:layout_marginTop="81dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="@+id/button2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/playicon"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1" />
</RelativeLayout>
答案 0 :(得分:0)
因为MediaPlayer
类的一个实例只有一个OnCompletionListener
侦听器,所以调用
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playBtn.setBackgroundResource(R.drawable.playicon);
}
});
它将注册一个新的侦听器,该侦听器设置了playBtn
按钮的图标。之后,您致电
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playbutton.setBackgroundResource(R.drawable.playicon);
}
});
它将设置一个新的侦听器,该侦听器设置playbutton
按钮的图标并覆盖第一个侦听器。因此,第一个侦听器中的代码未执行且playBtn
的图标也未更改的原因。
您应该使用一个侦听器,然后将要执行的所有代码放入其中。
playBtn = (ImageButton) findViewById(R.id.button1);
playbutton= (ImageButton) findViewById(R.id.button2);
final MediaPlayer sound = MediaPlayer.create(this,R.raw.evumm);
playBtn.setBackgroundResource(R.drawable.playicon);
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sound.isPlaying()) {
sound.pause();
playBtn.setBackgroundResource(R.drawable.playicon);
} else {
sound.start();
playBtn.setBackgroundResource(R.drawable.pauseicon);
}
}
});
playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sound.isPlaying()) {
sound.pause();
playbutton.setBackgroundResource(R.drawable.playicon);
} else {
sound.start();
playbutton.setBackgroundResource(R.drawable.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playBtn.setBackgroundResource(R.drawable.playicon);
playbutton.setBackgroundResource(R.drawable.playicon);
}
});