Android仿音乐播放器功能
程序员文章站
2023-12-20 22:15:40
本文实例为大家分享了android仿音乐播放器功能的具体代码,供大家参考,具体内容如下
读取本地音乐文件
源代码:
import android.media....
本文实例为大家分享了android仿音乐播放器功能的具体代码,供大家参考,具体内容如下
读取本地音乐文件
源代码:
import android.media.mediaplayer; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.imagebutton; import android.widget.seekbar; import android.widget.textview; public class mainactivity extends appcompatactivity { private mediaplayer mediaplayer; private seekbar seekbar; private textview tv_mian_stop; private textview tv_mian_start; private int musicaction; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv_mian_start = (textview) findviewbyid(r.id.tv_mian_start); tv_mian_stop = (textview) findviewbyid(r.id.tv_mian_stop); seekbar = (seekbar) findviewbyid(r.id.seekbar); //进度条的监听事件 seekbar.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { //改变 //每当进度条发生改变时tv_mian_start的值发生变化 //new timeutil().sectotime()一个外部类(时间格式转化) tv_mian_start.settext(new timeutil().sectotime(mediaplayer.getcurrentposition()/1000)+""); } @override public void onstarttrackingtouch(seekbar seekbar) { //开始 } @override public void onstoptrackingtouch(seekbar seekbar) { //结束 //获取拖动结束之后的位置 int progrss=seekbar.getprogress(); mediaplayer.seekto(progrss); } }); } public void isplay(view view){ imagebutton imagebutton= (imagebutton) view; if (mediaplayer==null){ mediaplayer = mediaplayer.create(this, r.raw.seve); mediaplayer.start(); //把图标修改为暂停的图标 imagebutton.setimageresource(android.r.drawable.ic_media_pause); //获取音乐的总时长 int muiscsum=mediaplayer.getduration(); //设置进度条的最大值为总时长 seekbar.setmax(muiscsum); //执行一个多线程的耗时操作 new mythread().start(); tv_mian_stop.settext(new timeutil().sectotime(muiscsum/1000)+""); }else if (mediaplayer.isplaying()){ mediaplayer.pause(); imagebutton.setimageresource(android.r.drawable.ic_media_play); }else{ mediaplayer.start(); imagebutton.setimageresource(android.r.drawable.ic_media_pause); } } class mythread extends thread{ @override public void run() { super.run(); //当进度条的当前值小于最大值时进度条发生改变 while(seekbar.getprogress()<=seekbar.getmax()){ //获取当前音乐播放的位置 musicaction = mediaplayer.getcurrentposition(); //让进度条动起来 seekbar.setprogress(musicaction); } } } }
时间格式转化类:
public class timeutil { public static string sectotime(int time) { string timestr = null; int hour = 0; int minute = 0; int second = 0; if (time <= 0) return "00:00"; else { minute = time / 60; if (minute < 60) { second = time % 60; timestr = unitformat(minute) + ":" + unitformat(second); } else { hour = minute / 60; if (hour > 99) return "99:59:59"; minute = minute % 60; second = time - hour * 3600 - minute * 60; timestr = unitformat(hour) + ":" + unitformat(minute) + ":" + unitformat(second); } } return timestr; } private static string unitformat(int i) { string retstr = null; if (i >= 0 && i < 10) retstr = "0" + integer.tostring(i); else retstr = "" + i; return retstr; } }
真机效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。