废话不多说上代码
private AudioManager audioManager;
private SimpleDateFormat format;
private SeekBar seekBar;
private TextView musicLength;
private TextView musicCur;
private MediaPlayer mediaPlayer = new MediaPlayer();
private ImageButton play;
private ImageButton pause;
private ImageButton stop;
private Timer timer;
private boolean isSeekBarChanging;//互斥变量,防止进度条与定时器冲突。
private int currentPosition = 0;//当前音乐播放的进度
private ImageView mDynamicCircle;
private ObjectAnimator mCircleAnimator;
private ImageView mDynamicCircle1;
private ImageView mMp3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_start);
audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
findViewById(R.id.iv_goback).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
format = new SimpleDateFormat("mm:ss");
musicLength = (TextView) findViewById(R.id.music_length);
musicCur = (TextView) findViewById(R.id.music_cur);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new MySeekBar());
play = (ImageButton) findViewById(R.id.play);
pause = (ImageButton) findViewById(R.id.pause);
stop = (ImageButton) findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
donghua();
donghua2();
initMediaPlayer();//初始化mediaplayer
}
private void donghua(){
mDynamicCircle = (ImageView) findViewById(R.id.cdbg);
mCircleAnimator = ObjectAnimator.ofFloat(mDynamicCircle, "rotation", 0.0f, 360.0f);
mCircleAnimator.setDuration(6000);
mCircleAnimator.setInterpolator(new LinearInterpolator());
mCircleAnimator.setRepeatCount(-1);
mCircleAnimator.setRepeatMode(ObjectAnimator.RESTART);
}
private void donghua2(){
mDynamicCircle1 = (ImageView) findViewById(R.id.cdbg1);
mMp3 = (ImageView) findViewById(R.id.cdbg2);
mMp3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!mediaPlayer.isPlaying()) {
paly();
}else {
mCircleAnimator.pause();
mDynamicCircle1.startAnimation(rotateAnimation1);
mediaPlayer.pause();//暂停播放
pause.setBackgroundResource(R.drawable.cdbgbut);
}
}
});
}
private void initMediaPlayer() {
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/eBook/朴树+-+平凡之路.mp3");//指定音频文件的路径
mediaPlayer.prepare();//让mediaplayer进入准备状态
mediaPlayer.setLooping(true);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
seekBar.setMax(mediaPlayer.getDuration());
musicLength.setText(format.format(mediaPlayer.getDuration())+"");
musicCur.setText("00:00");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initMediaPlayer();
} else {
Toast.makeText(MusicStartActivity.this,"denied access",Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
private void paly(){
pause.setBackgroundResource(R.drawable.cdztbut);
mDynamicCircle1.startAnimation(rotateAnimation);
mCircleAnimator.start();
mediaPlayer.start();//开始播放
mediaPlayer.seekTo(currentPosition);
//监听播放时回调函数
timer = new Timer();
timer.schedule(new TimerTask() {
Runnable updateUI = new Runnable() {
@Override
public void run() {
musicCur.setText(format.format(mediaPlayer.getCurrentPosition())+"");
}
};
@Override
public void run() {
if(!isSeekBarChanging){
seekBar.setProgress(mediaPlayer.getCurrentPosition());
runOnUiThread(updateUI);
}
}
},0,50);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play:
// if (!mediaPlayer.isPlaying()) {
// paly();
// }
break;
case R.id.pause:
if (mediaPlayer.isPlaying()) {
mCircleAnimator.pause();
mDynamicCircle1.startAnimation(rotateAnimation1);
mediaPlayer.pause();//暂停播放
pause.setBackgroundResource(R.drawable.cdbgbut);
}else {
paly();
}
break;
case R.id.stop:
// if (mediaPlayer.isPlaying()) {
// mediaPlayer.reset();//停止播放
// mCircleAnimator.end();
// mDynamicCircle1.startAnimation(rotateAnimation1);
// currentPosition = 0;
// initMediaPlayer();
// }
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
isSeekBarChanging = true;
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
if (timer != null){
timer.cancel();
timer = null;
}
}
/*进度条处理*/
public class MySeekBar implements SeekBar.OnSeekBarChangeListener {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
/*滚动时,应当暂停后台定时器*/
public void onStartTrackingTouch(SeekBar seekBar) {
isSeekBarChanging = true;
}
/*滑动结束后,重新设置值*/
public void onStopTrackingTouch(SeekBar seekBar) {
isSeekBarChanging = false;
currentPosition = seekBar.getProgress();
mediaPlayer.seekTo(seekBar.getProgress());
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="24.5dp"
android:layout_marginBottom="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="16dp"
android:layout_height="25dp"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:background="@drawable/goback"/>
<ImageView
android:id="@+id/iv_grid_list_page_goback"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="18dp" />
<TextView
android:id="@+id/tv_succeed_experience_page_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_grid_list_page_goback"
android:text="返回"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textColor="@color/textColorWrite"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"
android:layout_centerVertical="true"
android:textSize="22sp"
android:textColor="@color/textColorBaiWrite"
android:layout_centerInParent="true"/>
<ImageView
android:id="@+id/iv_goback"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignLeft="@+id/iv_grid_list_page_goback"
android:layout_alignRight="@+id/tv_succeed_experience_page_title"/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_mp3bg"
android:layout_width="450dp"
android:layout_height="430dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
>
<ImageView
android:id="@+id/cdbg"
android:layout_width="380dp"
android:layout_height="380dp"
android:layout_centerInParent="true"
android:background="@drawable/cdbg"/>
<ImageView
android:id="@+id/cdbg2"
android:layout_width="200dp"
android:layout_height="69dp"
android:layout_alignBottom="@+id/cdbg"
android:layout_alignParentEnd="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="13dp" />
<ImageView
android:id="@+id/cdbg1"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignBottom="@+id/cdbg"
android:layout_alignParentEnd="true"
android:layout_marginBottom="59dp"
android:layout_marginEnd="13dp"
android:background="@drawable/cdkg" />
</RelativeLayout>
<LinearLayout
android:id="@+id/bg_bg"
android:layout_below="@+id/rl_mp3bg"
android:gravity="center"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/play"
android:background="@drawable/cdbfbut"
android:padding="8dp"
android:layout_width="30dp"
android:layout_height="30dp"/>
<ImageButton
android:id="@+id/pause"
android:background="@drawable/cdbgbut"
android:layout_marginLeft="60dp"
android:padding="12dp"
android:layout_width="40dp"
android:layout_height="40dp"/>
<ImageButton
android:id="@+id/stop"
android:background="@drawable/cdtzbut"
android:layout_marginLeft="60dp"
android:padding="8dp"
android:layout_width="30dp"
android:layout_height="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_below="@+id/bg_bg"
android:layout_width="match_parent"
android:layout_marginTop="35dp"
android:gravity="center"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/seekBar"
android:layout_gravity="center_vertical"
android:background="@null"
android:max="100"
android:maxHeight="6dp"
android:progressDrawable="@drawable/seek_progress"
android:thumb="@drawable/seek_thumbicon2"
android:thumbOffset="8dp"
android:progressBackgroundTint="#30ffffff"
android:thumbTint="@color/video_progress1"
android:layout_width="729dp"
android:layout_centerHorizontal="true"
android:layout_height="30dp"/>
<LinearLayout
android:layout_alignEnd="@id/seekBar"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/music_cur"
android:textColor="@color/textColorBaiWrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/textColorBaiWrite"
android:text="/"/>
<TextView
android:id="@+id/music_length"
android:textColor="@color/textColorBaiWrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>