Android仿音乐播放器功能
2019/7/7 20:54:14
本文主要是介绍Android仿音乐播放器功能,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例为大家分享了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; } }
真机效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于Android仿音乐播放器功能的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路