import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnPreparedListener,
OnClickListener {
private ImageView pre;
private ImageView play;
private ImageView next;
private ImageView needle;
private ImageView disc;
private SeekBar seek_bar;
private int index = 0;// 记录点击条目的索引
private MediaPlayer player;
private boolean flag = false;// 记录是否处于暂停状态
private int duration;//总时长
private Timer timer;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
//取值--->当前播放的时间
int currrent = (Integer) msg.obj;
//格式化时间 ----format.format(duration);
text_star_time.setText(format.format(currrent) );
text_end_time.setText( format.format(duration));
//进度条
seek_bar.setProgress(currrent*1000/duration);
}
};
};
private SimpleDateFormat format;
private Uri parse;
private TextView text_star_time;
private TextView text_end_time;
private ObjectAnimator discAnimation;
private ObjectAnimator needleAnimation;
private ObjectAnimator needleAnimation02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件
findViewId();
ss();
play();
setAnimations();
}
private void ss() {
// 展示音乐列表
// 创建MediaPlayer对象
player = new MediaPlayer();
//创建一个计时器
timer = new Timer();
//创建时间格式化的类
format = new SimpleDateFormat("mm:ss");
/* // 点击列表的条目,,,,播放对应的音乐
list_view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 给index重新赋值
index = position;
// 开始播放音乐
play();
}
});*/
// 点击上一曲,,播放/暂停,,下一曲的监听
pre.setOnClickListener(this);
play.setOnClickListener(this);
next.setOnClickListener(this);
//设置进度条的监听
seek_bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
//进度发生改变的时候,,,歌曲进度也跟着改变
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (player!=null && fromUser) {
player.seekTo(progress*duration/1000);
}
}
});
/* //自动下一曲
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//音乐播放完成以后,,直接调用下一曲的方法
next();
}
});*/
}
/**
* 找控件的方法
*/
private void findViewId() {
pre = (ImageView) findViewById(R.id.pre);
play = (ImageView) findViewById(R.id.play);
next = (ImageView) findViewById(R.id.next);
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
text_star_time = findViewById(R.id.text_star_time);
text_end_time = findViewById(R.id.text_end_time);
needle = (ImageView) findViewById(R.id.needle);
disc = (ImageView) findViewById(R.id.disc);
}
//动画设置
private void setAnimations() {
discAnimation = ObjectAnimator.ofFloat(disc, "rotation", 0, 360);
discAnimation.setDuration(20000);
discAnimation.setInterpolator(new LinearInterpolator());
discAnimation.setRepeatCount(ValueAnimator.INFINITE);
needleAnimation = ObjectAnimator.ofFloat(needle, "rotation", 0,20);
needle.setPivotX(0);
needle.setPivotY(0);
needleAnimation.setDuration(800);
needleAnimation.setInterpolator(new LinearInterpolator());
needleAnimation02 = ObjectAnimator.ofFloat(needle, "rotation", 20, 0);
needle.setPivotX(0);
needle.setPivotY(0);
needleAnimation02.setDuration(800);
needleAnimation02.setInterpolator(new LinearInterpolator());
}
/**
* 播放音乐的方法
*/
private void play() {
if (player != null) {
try {
// 重置资源
player.reset();
// 加载音乐
player.setDataSource(Environment
.getExternalStorageDirectory() + "/Music/sugar.mp3");
// 准备
player.prepareAsync();// 异步
// 准备完成的监听
player.setOnPreparedListener(this);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 准备完成的监听
*/
@Override
public void onPrepared(MediaPlayer mp) {
// 开始播放
player.start();
// 更改播放按钮的图片
play.setImageResource(R.drawable.ic_pause);
discAnimation.start();
needleAnimation.start();
//获取歌曲总时长
duration = player.getDuration();
//音乐开始播放,,,时间/进度条开始变化
TimerTask task = new TimerTask() {
@Override
public void run() {
// 每隔1s获取一次当前播放时间
int currentPosition = player.getCurrentPosition();
Message msg = Message.obtain();
msg.obj = currentPosition;
msg.what = 0;
handler.sendMessage(msg );
}
};
timer.schedule(task, 0, 1000);
}
/**
* 点击按钮的监听
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
// 点击播放按钮:
if (player != null && player.isPlaying()) {// 正在播放
// 暂停音乐
player.pause();
discAnimation.pause();
needleAnimation02.start();
// 修改按钮的图片
play.setImageResource(R.drawable.ic_play);
// 修改状态值
flag = true;
} else {
if (flag) {// 处于暂停状态
player.start();// 继续播放
flag = false;// 修改状态值
// 修改按钮的图片
needleAnimation.start();
discAnimation.resume();
play.setImageResource(R.drawable.ic_pause);
} else {
play();
}
}
break;
case R.id.pre:
// 点击上一曲
// pre();
break;
case R.id.next:
// 下一曲
// next();
break;
default:
break;
}
}
/**
* 下一曲的方法
*/
private void next() {
/*index++;
index = index % list.size();
play();*/
}
/**
* 上一曲的方法
*/
private void pre() {
/* if (index <= 0) {
index = list.size();
}
index--;
play();*/
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_blackground"
>
<ImageView
android:id="@+id/disc"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_disc"
android:layout_marginTop="300dp"
/>
<ImageView
android:id="@+id/needle"
android:layout_width="350dp"
android:layout_height="350dp"
android:paddingLeft="200dp"
android:layout_alignParentRight="true"
android:layout_marginTop="81dp"
android:src="@drawable/ic_needle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#212829"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/text_star_time"
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="00:00"
android:textColor="#ffffff" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:max="1000" />
<TextView
android:id="@+id/text_end_time"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:text="03:44"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/pre"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_last" />
<ImageView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/play"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_play" />
<ImageView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/next"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_next" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>