亲宝软件园·资讯

展开

Android音乐播放 Android仿音乐播放器功能

mdf_jy 人气:0
想了解Android仿音乐播放器功能的相关内容吗,mdf_jy在本文为您仔细讲解Android音乐播放的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android音乐播放,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;
  }

} 


真机效果图:

加载全部内容

相关教程
猜你喜欢
用户评论