查看: 1334|回复: 0

[手机开发] Android仿音乐播放器功能

发表于 2017-8-6 08:53:25
尚学堂AD

本文实例为大家分享了Android仿音乐播放器功能的具体代码,供大家参考,具体内容如下

读取本地音乐文件

源代码:

  1. import android.media.MediaPlayer;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.ImageButton;
  6. import android.widget.SeekBar;
  7. import android.widget.TextView;
  8. public class MainActivity extends AppCompatActivity {
  9. private MediaPlayer mediaPlayer;
  10. private SeekBar seekBar;
  11. private TextView tv_mian_stop;
  12. private TextView tv_mian_start;
  13. private int musicaction;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. tv_mian_start = (TextView) findViewById(R.id.tv_mian_start);
  19. tv_mian_stop = (TextView) findViewById(R.id.tv_mian_stop);
  20. seekBar = (SeekBar) findViewById(R.id.seekBar);
  21. //进度条的监听事件
  22. seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  23. @Override
  24. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  25. //改变
  26. //每当进度条发生改变时tv_mian_start的值发生变化
  27. //new TimeUtil().secToTime()一个外部类(时间格式转化)
  28. tv_mian_start.setText(new TimeUtil().secToTime(mediaPlayer.getCurrentPosition()/1000)+"");
  29. }
  30. @Override
  31. public void onStartTrackingTouch(SeekBar seekBar) {
  32. //开始
  33. }
  34. @Override
  35. public void onStopTrackingTouch(SeekBar seekBar) {
  36. //结束
  37. //获取拖动结束之后的位置
  38. int progrss=seekBar.getProgress();
  39. mediaPlayer.seekTo(progrss);
  40. }
  41. });
  42. }
  43. public void isPlay(View view){
  44. ImageButton imageButton= (ImageButton) view;
  45. if (mediaPlayer==null){
  46. mediaPlayer = MediaPlayer.create(this, R.raw.seve);
  47. mediaPlayer.start();
  48. //把图标修改为暂停的图标
  49. imageButton.setImageResource(android.R.drawable.ic_media_pause);
  50. //获取音乐的总时长
  51. int muiscsum=mediaPlayer.getDuration();
  52. //设置进度条的最大值为总时长
  53. seekBar.setMax(muiscsum);
  54. //执行一个多线程的耗时操作
  55. new MyThread().start();
  56. tv_mian_stop.setText(new TimeUtil().secToTime(muiscsum/1000)+"");
  57. }else if (mediaPlayer.isPlaying()){
  58. mediaPlayer.pause();
  59. imageButton.setImageResource(android.R.drawable.ic_media_play);
  60. }else{
  61. mediaPlayer.start();
  62. imageButton.setImageResource(android.R.drawable.ic_media_pause);
  63. }
  64. }
  65. class MyThread extends Thread{
  66. @Override
  67. public void run() {
  68. super.run();
  69. //当进度条的当前值小于最大值时进度条发生改变
  70. while(seekBar.getProgress()<=seekBar.getMax()){
  71. //获取当前音乐播放的位置
  72. musicaction = mediaPlayer.getCurrentPosition();
  73. //让进度条动起来
  74. seekBar.setProgress(musicaction);
  75. }
  76. }
  77. }
  78. }
复制代码

时间格式转化类:

  1. public class TimeUtil {
  2. public static String secToTime(int time) {
  3. String timeStr = null;
  4. int hour = 0;
  5. int minute = 0;
  6. int second = 0;
  7. if (time <= 0)
  8. return "00:00";
  9. else {
  10. minute = time / 60;
  11. if (minute < 60) {
  12. second = time % 60;
  13. timeStr = unitFormat(minute) + ":" + unitFormat(second);
  14. } else {
  15. hour = minute / 60;
  16. if (hour > 99)
  17. return "99:59:59";
  18. minute = minute % 60;
  19. second = time - hour * 3600 - minute * 60;
  20. timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
  21. }
  22. }
  23. return timeStr;
  24. }
  25. private static String unitFormat(int i) {
  26. String retStr = null;
  27. if (i >= 0 && i < 10)
  28. retStr = "0" + Integer.toString(i);
  29. else
  30. retStr = "" + i;
  31. return retStr;
  32. }
  33. }
复制代码

真机效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报