package ***.***.***;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private Button btnPause,btnPlayUrl,btnStop;
private SeekBar skbProgress;
private Player player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPause = (Button) findViewById(R.id.btnPause);
btnPause.setOnClickListener(new ClickEvent());
btnPlayUrl= (Button) findViewById(R.id.btnPlayUrl);
btnPlayUrl.setOnClickListener(new ClickEvent());
btnStop= (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new ClickEvent());
skbProgress= (SeekBar) findViewById(R.id.skbProgress);
skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
player=new Player(skbProgress);
}
class ClickEvent implements View.OnClickListener{
@Override
public void onClick(View view) {
if (view==btnPause){
if (!isPause) {
player.pause();
btnPause.setText("继续");
isPause = true;
btnPlayUrl.setEnabled(true);
} else {
isPause = false;
player.play();
btnPause.setText("暂停");
btnPlayUrl.setEnabled(false);
}
}else if (view==btnPlayUrl){
player.play();
new Thread(new Runnable() {
@Override
public void run() {
String url = "http://music.wufazhuce.com/lkX5S8HVBeQglFRGjZKVodnz-25C";
player.playUrl(url);
}
}).start();
btnPause.setText("暂停");
btnPlayUrl.setEnabled(false);
btnPause.setEnabled(true);
btnStop.setEnabled(true);
}else if (view==btnStop){
player.stop();
isPause = false;
btnPause.setText("暂停");
btnPause.setEnabled(false);
btnPlayUrl.setEnabled(true);
btnStop.setEnabled(false);
}
}
}
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{
int progress;
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
this.progress=i*player.mediaPlayer.getDuration()/seekBar.getMax();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
player.mediaPlayer.seekTo(progress);
}
}
}
package ***.***.***;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.SeekBar;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by user on 2016/9/4.
*/
public class Player implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener,
MediaPlayer.OnPreparedListener {
public MediaPlayer mediaPlayer;
private SeekBar skbProgress;
private Timer mTimer = new Timer();
public Player(SeekBar skbProgress) {
this.skbProgress = skbProgress;
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mTimer.schedule(mTimerTask, 0, 1000);
}
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
if (mediaPlayer == null) {
return;
}
if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
handlerProgress.sendEmptyMessage(0);
}
}
};
Handler handlerProgress = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int position = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
if (duration > 0) {
long pos = skbProgress.getMax() * position / duration;
skbProgress.setProgress((int) pos);
}
}
};
public void play() {
mediaPlayer.start();
}
public void playUrl(String vidoUrl) {
try {
mediaPlayer.setDataSource(vidoUrl);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
public void pause() {
mediaPlayer.pause();
}
public void stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
}
}
@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
skbProgress.setSecondaryProgress(i);
int currentProgress = skbProgress.getMax() * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
Log.e(currentProgress + "% play", i + "% buffer");
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
}
这里有demo