package com.example.day12musicplay2;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
MediaPlayer mediaPlayer;
Timer timer;
private SeekBar sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb=(SeekBar)findViewById(R.id.sb);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int po=sb.getProgress();
mediaPlayer.seekTo(po);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
}
public void musicplay(View view) {
if (mediaPlayer!=null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
}
mediaPlayer=MediaPlayer.create(this, R.raw.zxc);
mediaPlayer.start();
if (timer==null) {
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
int pos=mediaPlayer.getDuration();
int posi=mediaPlayer.getCurrentPosition();
sb.setMax(pos);
sb.setProgress(posi);
}
}, 0, 200);
}
}
public void puase(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}else {
mediaPlayer.start();
}
}
public void stop(View view) {
mediaPlayer.stop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
super.onDestroy();
}
}
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
MediaPlayer mediaPlayer;
Timer timer;
private SeekBar sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb=(SeekBar)findViewById(R.id.sb);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int po=sb.getProgress();
mediaPlayer.seekTo(po);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
}
public void musicplay(View view) {
if (mediaPlayer!=null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
}
mediaPlayer=MediaPlayer.create(this, R.raw.zxc);
mediaPlayer.start();
if (timer==null) {
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
int pos=mediaPlayer.getDuration();
int posi=mediaPlayer.getCurrentPosition();
sb.setMax(pos);
sb.setProgress(posi);
}
}, 0, 200);
}
}
public void puase(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}else {
mediaPlayer.start();
}
}
public void stop(View view) {
mediaPlayer.stop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
super.onDestroy();
}
}