所以我正在关注Big Nerd Ranch的Android编程书,我有一个通过MediaPLayer播放的音频文件.我还有一个播放和暂停音频的按钮.如果按播放按钮播放音频,按钮上的文字将变为“暂停”.问题是在音频结束后按钮仍然显示“暂停”,因此用户必须按两次按钮再次播放音频.如何让按钮将其文本更改为“在音频结束后立即播放”
这是我的代码减去按钮的布局xml
片段类:
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(); }}
MediaPlayer类:
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(); }}
解决方法:
如果直接在活动上使用MediaPlayer类,则可以使用其OnCompletionListener来检测媒体是否停止播放.在这个OnCompletionListener中,您可以将按钮的文本从“暂停”切换为“播放”,如下所示:
mPlayer.setonCompletionListener(new MediaPlayer.OnCompletionListener() { public voID onCompletion(MediaPlayer mp) { mPlaybutton.setText("Play"); }});
我将您的AudioPlayer集成到片段中,使其看起来像:
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音频结束后更改按钮文本全部内容,希望文章能够帮你解决java – Android:在MediaPlayer音频结束后更改按钮文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)