package com.bwie.day13_music_1;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private int idd;
private ListView listview;
private Button button;
private String[] names;
private int[] ids;
private MediaPlayer player;
private SeekBar seekbar;
private Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getList();
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, names));
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
idd = position;
start(idd);
button.setText("暂停");
}
});
button = (Button) findViewById(R.id.continue_);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
player.seekTo(seekBar.getProgress());
setseekbar();
}
public void onStartTrackingTouch(SeekBar seekBar) {
cancel();
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
}
/*
* 开始播放
*/
public void start(int position) {
cancel();
button.setText("暂停");
if (player != null && player.isPlaying()) {
player.stop();
player.release();
player = null;
}
if (player == null) {
player = MediaPlayer.create(MainActivity.this, ids[position]);
}
player.start();
int duration = player.getDuration();
seekbar.setMax(duration);
setseekbar();
player.setOnSeekCompleteListener(new OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
down(null);
}
});
}
public void start(View v) {
button.setText("暂停");
if (player != null) {
start(idd);
}
}
public void continue_(View v) {
if (button.getText().toString().equals("暂停")) {
player.pause();
button.setText("继续");
} else {
player.start();
button.setText("暂停");
}
}
public void stop(View v) {
if (player != null && player.isPlaying()) {
player.stop();
player = null;
}
}
public void up(View v) {
button.setText("暂停");
if (idd == 0) {
idd = names.length;
}
idd--;
start(idd);
}
public void down(View v) {
button.setText("暂停");
if (idd == names.length - 1) {
idd = -1;
}
idd++;
start(idd);
}
private void getList() {
names = new String[] { "当", "慢动作", "世界无限大" };
ids = new int[] { R.raw.dang, R.raw.tfboy, R.raw.world };
}
public void setseekbar() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
int current = player.getCurrentPosition();
seekbar.setProgress(current);
}
}, 0, 100);
}
public void cancel() {
if (timer != null) {
timer.cancel();
}
}
}