本文实例为大家分享了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; } }
真机效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。