【问题标题】:I am trying display the pause icon while playing the sound and then display the play icon when the sound finish playing我正在尝试在播放声音时显示暂停图标,然后在声音播放完毕时显示播放图标
【发布时间】:2018-09-15 17:03:50
【问题描述】:

在这里,我试图在媒体播放时显示暂停图标,并且我想在媒体播放完毕后自动更改为播放图标。当我第一次点击播放图标时,它会变为暂停图标,但在媒体播放完毕后它不会变回播放图标。

public class MainActivity extends AppCompatActivity {
    ImageButton playBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        playBtn = (ImageButton) findViewById(R.id.button1);

        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);
                }
            }

        });

    }
}

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" />
   </RelativeLayout>

【问题讨论】:

    标签: java android


    【解决方案1】:

    您可以使用 MediaPlayer 中的OnCompletionListener 来执行此操作。只需添加一个监听器,在声音完成时将按钮图标设置回播放图标。

    sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            playBtn.setBackgroundResource(R.drawable.playicon);
        }
    });
    

    【讨论】:

    • 我在两个 ImageButtons 上实现了播放不同的声音。事实证明,第一个 ImageButton 在声音播放完毕后不会变回播放图标,但第二个工作正常,并且 ImageButton 在声音播放完毕后会更改为播放图标。你能帮我解决这个问题吗?
    • 当然。最好的方法是将此答案标记为已接受,并使用您的新代码添加一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多