gpt4 book ai didi

java - Android:MediaPlayer 音频结束后更改按钮文本

转载 作者:行者123 更新时间:2023-11-30 02:36:07 24 4
gpt4 key购买 nike

所以我正在关注 Big Nerd Ranch 的 Android 编程书籍,并且我有一个通过 MediaPLayer 播放的音频文件。我还有一个按钮可以播放和暂停音频。如果您按下播放按钮播放音频,按钮上的文字会变为“暂停”。问题是音频结束后按钮仍然显示“暂停”,因此用户必须按两次按钮才能再次播放音频。我如何获得将其文本更改为“音频结束后立即播放”的按钮

这是我的代码减去了按钮的布局 xml

fragment 类:

public class HelloMoonFragment extends Fragment {

private Button mPlayButton;
private Button mStopButton;

private AudioPlayer mPlayer = new AudioPlayer();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

mPlayButton = (Button)v.findViewById(R.id.hellomoon_playButton);
mPlayButton.setText("Play");
mPlayButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if(mPlayButton.getText().equals("Play")) {
mPlayButton.setText("Pause");
mPlayer.play(getActivity());
}
else if(mPlayButton.getText().equals("Pause")){
mPlayButton.setText("Play");
mPlayer.pause();
}
}
});

mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
mStopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPlayer.stop();
mPlayButton.setText("Play");
}
});

return v;
}

@Override
public void onDestroy() {
super.onDestroy();
mPlayer.stop();
}
}

媒体播放器类:

package com.bignerdranch.android.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;

public class AudioPlayer {

private MediaPlayer mPlayer;
private boolean mCompleted = false;

public void stop() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}

public void play(Context c) {

if(mPlayer == null) {
mPlayer = MediaPlayer.create(c, R.raw.one_small_step);

mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
stop();
mCompleted = true;
}
});
}
mPlayer.start();
}

public void pause() {
if(mPlayer != null) mPlayer.pause();
}
}

最佳答案

如果您直接在 Activity 上使用 MediaPlayer 类,那么您可以使用它的 OnCompletionListener 来检测媒体是否停止播放。在此 OnCompletionListener 中,您可以将按钮的文本从“暂停”切换为“播放”,如下所示:

mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mPlayButton.setText("Play");
}
});

我将您的 AudioPlayer 集成到 fragment 中,使其看起来像:

public class HelloMoonFragment extends Fragment {

private Button mPlayButton;
private Button mStopButton;

private MediaPlayer mPlayer;
private boolean mCompleted = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

mPlayButton = (Button) v.findViewById(R.id.hellomoon_playButton);
mPlayButton.setText("Play");

mPlayButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {

if(mPlayer == null) {
mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
stopMediaPlayer();
mPlayButton.setText("Play");
}
});
}

if(mPlayButton.getText().equals("Play")) {
mPlayButton.setText("Pause");
mPlayer.start();
}
else if(mPlayButton.getText().equals("Pause")){
mPlayButton.setText("Play");
mPlayer.pause();
}
}
});

mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
mStopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopMediaPlayer();
mPlayButton.setText("Play");
}
});

return v;
}

@Override
public void onDestroy() {
stopMediaPlayer();
super.onDestroy();
}


public void stopMediaPlayer() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}

关于java - Android:MediaPlayer 音频结束后更改按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26520608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com