重置媒体播放器后,播放按钮不起作用

After resetting the mediaplayer , the play button is not working

我正在制作一个播放音频的应用程序,我在其中设置了播放、暂停和重置 button.All 按钮在应用程序启动时工作,如果我播放音频并在按下重置按钮后重置它,如果我再次按下播放按钮,音频不会播放。

package com.example.android.mediaplayer;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mmediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         mmediaPlayer = MediaPlayer.create(this, R.raw.song);

        Button b1=(Button) findViewById(R.id.play_button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Playing sound",Toast.LENGTH_SHORT).show();

                        mmediaPlayer.start();
                        mmediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mediaPlayer) {
                                Toast.makeText(getApplicationContext(),"Song is done",Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });
        Button b2=(Button) findViewById(R.id.pause_button);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mmediaPlayer.pause();
            }
        });
        Button b3=(Button) findViewById(R.id.reset_button);
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mmediaPlayer.reset();
            }
        });

    }



}

调用 mediaPlayer.reset() 后,还必须调用 prepare 方法。

您也可以这样做: mediaPlayer.seekTo(0); mediaPlayer.pause();

然后你可以再次调用播放从0开始播放音乐