概述
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.juanshichang.utils.ToastUtil;
import com.xhly.easystud.utils.Util;
import com.flyco.roundview.RoundTextView;
import com.xhkjedu.xhstudy.utils.L;
import com.xhly.easystud.R;
import com.xhly.easystud.base.BaseMvpActivity;
import com.xhly.easystud.bean.ReciteResultBean;
import com.xhly.easystud.config.BaseConfig;
import com.xhly.easystud.http.Content;
import com.xhly.easystud.utils.DownloadUtil;
import com.xhly.easystud.utils.ZJFileUtil;
import com.xhly.easystud.zhan.recite.presenter.ReciteResultPresenterImpl;
import com.xhly.easystud.zhan.recite.recitecontract.ReciteResultContract;
import java.io.File;
import java.util.ArrayList;
public class ReciteResultActivity extends BaseMvpActivity implements View.OnClickListener, ReciteResultContract.MyView {
private static final String TAG = "ReciteResultActivity";
private TextView tvBack;
private TextView tvNumberReally;
private CheckBox chkPlaybtn;
private SeekBar andioPlayBtn;
private Integer mRsid;
private ReciteResultPresenterImpl reciteResultPresenter;
private TextView tvTitle;
private TextView tvContent;
private TextView tvFinishTime;
private RoundTextView tvAnswer;
private RoundTextView tvOrigin;
private String answertxt;
private String recitecont;
private Integer costtime;
private TextView tvStopShowTime;
private String answerpath;
private boolean isStop;//线程标志位
private boolean isPlaying = false;
private ArrayList wavfileurllist = new ArrayList<>();
private ProgressDialog progressDialog;
private Integer costtime1;
private String getsubwavfilename;
private String wavfileurl;
private TextView tvStartTimeShow;
public MediaPlayer mediaPlayer = null;
private Thread thread;
@Override
protected int getContentView() {
return R.layout.activity_recite_result;
}
//运用Handler中的handleMessage方法接收子线程传递的信息
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 将SeekBar位置设置到当前播放位置
andioPlayBtn.setProgress(msg.what);
//获得音乐的当前播放时间
tvStartTimeShow.setText(Util.Companion.getTimeMS(msg.what / 1000));
}
};
@Override
protected void initView() {
changeStatusColor(true);
Intent intent = getIntent();
mRsid = intent.getIntExtra(Content.PSID, 0);
tvBack = findViewById(R.id.tv_back);
tvNumberReally = findViewById(R.id.tv_number_really);
chkPlaybtn = findViewById(R.id.chk_play_btn);
andioPlayBtn = findViewById(R.id.audio_play_bar);
tvTitle = findViewById(R.id.tv_title);
tvContent = findViewById(R.id.tv_content);
tvStartTimeShow = findViewById(R.id.tv_start_time_show);
tvNumberReally = findViewById(R.id.tv_number_really);
tvFinishTime = findViewById(R.id.tv_finish_time);
tvAnswer = findViewById(R.id.tv_answer);
tvOrigin = findViewById(R.id.tv_origin);
tvStopShowTime = findViewById(R.id.tv_stop_show_time);
tvAnswer.getDelegate().setBackgroundColor(getResources().getColor(R.color.logBut));
tvAnswer.setTextColor(this.getResources().getColor(R.color.white));
reciteResultPresenter = new ReciteResultPresenterImpl();
reciteResultPresenter.attachView(this);
initonclicklistener();
}
private void initonclicklistener() {
tvBack.setOnClickListener(this);
tvOrigin.setOnClickListener(this);
tvAnswer.setOnClickListener(this);
chkPlaybtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
if (mediaPlayer != null) {
if(!mediaPlayer.isPlaying()){
isStop = false;
mediaPlayer.start();
if (thread != null) {
thread.start();
} else {
thread = new Thread(new MuiscThread());
thread.start();
}
}
} else {
satrtPlayWav(); //刚开始为播放的时候 点击开始播放 并且更换状态
}
} else {
//如果在播放中,立刻暂停。
chkPlaybtn.setChecked(false);
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_back:
finish();
isStop = true;
break;
case R.id.tv_answer:
tvAnswer.getDelegate().setStrokeColor(getResources().getColor(R.color.logBut));
tvAnswer.getDelegate().setBackgroundColor(getResources().getColor(R.color.logBut));
tvAnswer.setTextColor(getResources().getColor(R.color.white));
tvOrigin.getDelegate().setBackgroundColor(getResources().getColor(R.color.white));
tvOrigin.getDelegate().setStrokeColor(getResources().getColor(R.color.gray_border));
tvOrigin.setTextColor(getResources().getColor(R.color.black));
tvContent.setText(answertxt);
break;
case R.id.tv_origin:
tvOrigin.getDelegate().setStrokeColor(getResources().getColor(R.color.logBut));
tvOrigin.getDelegate().setBackgroundColor(getResources().getColor(R.color.logBut));
tvOrigin.setTextColor(getResources().getColor(R.color.white));
tvAnswer.getDelegate().setBackgroundColor(getResources().getColor(R.color.white));
tvAnswer.getDelegate().setStrokeColor(getResources().getColor(R.color.gray_border));
tvAnswer.setTextColor(getResources().getColor(R.color.black));
tvContent.setText(recitecont);
break;
}
}
@Override
protected void initData() {
showLoading();
reciteResultPresenter.getReciteResultData(mRsid);
}
@Override
public void getReciteResultView(ReciteResultBean reciteResultBean) {
answerpath = reciteResultBean.getAnswerpath();
String recitetitle = reciteResultBean.getRecitetitle();
recitecont = reciteResultBean.getRecitecont();
answertxt = reciteResultBean.getAnswertxt();
Integer userscore = reciteResultBean.getUserscore();
Integer stoptime = reciteResultBean.getStoptime();
costtime1 = reciteResultBean.getCosttime(); //后台获取到的时间
String Stringstoptime = Util.Companion.getTimedate(stoptime);
tvFinishTime.setText("完成时间 : " + Stringstoptime);
tvTitle.setText(recitetitle);
tvContent.setText(answertxt);
tvNumberReally.setText(String.valueOf(userscore));
tvTitle.setText(recitetitle);
}
private void satrtPlayWav() {
wavfileurl = ZJFileUtil.AUDIO_WAV_DIR(this);
File file = new File(wavfileurl);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File file1 = files[i];
if (file1 != null) {
wavfileurllist.add(file1.getName());
} else {
}
}
getsubwavfilename = answerpath.substring(answerpath.lastIndexOf("/") + 1);
if (wavfileurllist != null && wavfileurllist.contains(getsubwavfilename)) {
startplsywav(getsubwavfilename);
} else {
chkPlaybtn.setChecked(false);
progressDialog = new ProgressDialog(ReciteResultActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("正在下载");
progressDialog.setMessage("请稍后...");
progressDialog.setProgress(0);
progressDialog.setMax(100);
progressDialog.show();
progressDialog.setCancelable(false);
DownloadUtil.get().download(BaseConfig.showImageUrl + answerpath, ZJFileUtil.AUDIO_WAV_DIR(this), getsubwavfilename, new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess(File file) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String getokwavurl = file.getName().substring(file.getName().lastIndexOf("/") + 1);
if (getokwavurl.equals(getsubwavfilename)) {
ToastUtil.Companion.showToast(ReciteResultActivity.this, "下载完成");
chkPlaybtn.setChecked(true);
}
}
});
}
@Override
public void onDownloading(int progress) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setProgress(progress);
if (progress == progressDialog.getMax()) {
progressDialog.dismiss();
}
}
});
}
@Override
public void onDownloadFailed(Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
ToastUtil.Companion.showToast(ReciteResultActivity.this, "下载失败");
progressDialog.dismiss();
}
});
}
});
}
}
private void startplsywav(String filename) {
isStop = false;
chkPlaybtn.setChecked(true);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(wavfileurl + File.separator + filename);
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
andioPlayBtn.setMax(mediaPlayer.getDuration());
int stopshowtime = mediaPlayer.getDuration() / 1000; //h直接获取本地的 文件时间
String Stringfinishtime = Util.Companion.getTimeMS(stopshowtime);
tvStopShowTime.setText(Stringfinishtime);
chkPlaybtn.setClickable(true);
}
});
thread = new Thread(new MuiscThread());
// 启动线程
thread.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
isStop = true;
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
if (thread != null) {
thread.interrupt();
}
// tvStartTimeShow.setText("00:00");
// andioPlayBtn.setProgress(0);
chkPlaybtn.setChecked(false);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null && handler != null && thread != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
thread.interrupt();
isStop = true;
handler.removeCallbacks(thread);
}
}
//建立一个子线程实现Runnable接口
class MuiscThread implements Runnable {
@Override
//实现run方法
public void run() {
//判断音乐的状态,在不停止与不暂停的情况下向总线程发出信息
while (mediaPlayer != null && !isStop) {
try {
//发出的信息
handler.sendEmptyMessage(mediaPlayer.getCurrentPosition());
// 每100毫秒更新一次位置
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
if (isStop) {
thread.interrupt();
break;
}
}
}
}
}
最后
以上就是震动抽屉为你收集整理的android自定义media,Android——Mediaplay音频播放最详细最全面使用方法-以及自定义seekbar 样式...的全部内容,希望文章能够帮你解决android自定义media,Android——Mediaplay音频播放最详细最全面使用方法-以及自定义seekbar 样式...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复