Android Studio:使用 mediaPlayer 启动和停止音频

Android Studio: Start and Stop audio using mediaPlayer

如何startstop使用同一个按钮播放音乐。如果我重复点击,下面的代码将多次播放歌曲(重叠)。

另一个问题是在哪里插入 starTimerStopTimer 函数,这样 starTimer 将在没有播放声音时激活,而 stopTimer 将在播放声音时激活。

更新代码:

   public void playFile(View v) {
    if (mediaPlayer == null)
        mediaPlayer = new MediaPlayer();

    try {

        mediaPlayer.setDataSource(question.getAudio());

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(@NotNull MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.prepareAsync();

    }catch (IOException e) {
        e.printStackTrace();
        stopSelf();
    }

    if (mediaPlayer.isPlaying()) {
        //pause music
        mediaPlayer.pause();
    } else {
        //play music
        mediaPlayer.start();
    }
}

Xml 文件:

<Button
    android:id="@+id/audQuestion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:onClick="playFile"
    android:layout_centerInParent="true"/>

以及 startTimer stopTimer 函数

public void starTimer() {
    timer = new Timer(Constant.TIME_PER_QUESTION, Constant.COUNT_DOWN_TIMER);
    timer.start();
}

public void stopTimer() {
    if (timer != null)
        timer.cancel();
}

最好在单击按钮时生成一个布尔值,然后轻松处理

isClick 为 false 表示是否会执行其他 else.else spot you make mediaplayer stop and timer stop function

boolean isClick=false;
public void playFile(View v){
    MediaPlayer mediaPlayer = new MediaPlayer(); if(!isClick){

    isClick=true;
        try {

            mediaPlayer.setDataSource(question.getAudio());

            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                     if( mediaPlayer.isPlaying() ) {
                     mediaPlayer.stop();}  mp.start();
                     starTimer();
                 }
             });
            mediaPlayer.prepareAsync();

        }catch (IOException e){
            e.printStackTrace();
            stopSelf();
        }

    }else{ 
        if( mediaPlayer.isPlaying() ) {
            mediaPlayer.stop();
            stopTimer();
        }
     isClick=false;
    }
}

要停止 music/sound 使用媒体播放器创建一个布尔值 isPlaying。播放音乐时设为真

声明正在播放 global variable 喜欢

private boolean isPlaying = false;
@Override
public void onPrepared(MediaPlayer mp) {
    mp.start();
    isPlaying = true;
}

现在您可以创建一个方法来检查 isPlaying 的值并播放或暂停音乐,调用此方法 onClick() 按钮

private playPauseMusic(){
    if (!isPlaying) {
        //play music
        isPlaying = true;
        mediaPlayer.start();
    } else {
        //pause music
        isPlaying = false;
        mediaPlayer.pause();
    }
}

希望这会有所帮助!

The code below will play the song multipletimes (overlaying) if i click it repeatedly.

因为您每次按下按钮时都会实例化媒体播放器对象,要解决这个问题,请将您的医疗播放器对象设置为 class 字段,而不是方法本地字段,如果对象不是在实例化之前为 null

MediaPlayer mediaPlayer;

public void playFile(View v) { 
    if (mediaPlayer == null)
        mediaPlayer = new MediaPlayer();

    // The reset of the code
    ...

并且无需使用变量来检查媒体是否正在播放,因为您可以直接从 mediaplayer 对象进行检查

if (mediaPlayer.isPlaying()) {
    mediaPlayer.pause();
} else if () {
    mediaPlayer.start();
}

所以将您的代码更改为

MediaPlayer mediaPlayer;

public void playFile(View v) {
    if (mediaPlayer == null)
        mediaPlayer = new MediaPlayer();

    try {

        mediaPlayer.setDataSource(question.getAudio());

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(@NotNull MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.prepareAsync();

    }catch (IOException e) {
        e.printStackTrace();
        stopSelf();
    }

    if (mediaPlayer.isPlaying()) {
        //pause music
        mediaPlayer.pause();
    } else if () {
        //play music
        mediaPlayer.start();
    }
}

定时器的问题,不是很清楚,你的定时器是怎么实现的?

请在全局定义以下代码:

private Button play_pause;
MediaPlayer mediaPlayer; 

现在 onCreate 方法:

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

        mediaPlayer = new MediaPlayer();
        play_pause = findViewById(R.id.play_pause);
    }

播放暂停按钮:

play_pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mediaPlayer != null) {
                        if (mediaPlayer.isPlaying()) {
                            mediaPlayer.pause();
                        } else {
                            mediaPlayer.getDuration();
                            mediaPlayer.start();
                        }
                    } 
                }
            });

当音频准备好播放时:

private void readyToPlay(Uri uri) {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.setOnCompletionListener(this);
                mediaPlayer.setDataSource(activity, uri);//Write your location here
                mediaPlayer.prepare();
                mediaPlayer.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

音频播放完毕自动停止:

@Override
    public void onCompletion(MediaPlayer mp) {
        if (mediaPlayer != null) {
            mp.stop();
            mp.release();
        }
    }

别忘了添加这一行:

implements MediaPlayer.OnCompletionListener