//MainActivity
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private TextView textView;
private Intent intent;
private boolean isPlay = false;
private MyReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekbar);
textView = (TextView) findViewById(R.id.tv);
intent = new Intent(this, PlayService.class);
receiver = new MyReceiver();
registerReceiver(receiver, new IntentFilter(Config.ACTION_PLAY_PRG));
event();
}
private void event() {
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
Intent intent = new Intent(Config.ACTION_PLAY_SEEK);
intent.putExtra(Config.EXTRA_CURRENT, progress);
sendBroadcast(intent);
}
});
}
public void playMusic(View view) {
Button button = (Button) view;
if (!isPlay) {
button.setText("暂停");
} else {
button.setText("播放");
}
isPlay = !isPlay;
startService(intent);
}
public void stopMusic(View view) {
stopService(intent);
seekBar.setProgress(0);
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int maxLen = intent.getIntExtra(Config.EXTRA_MAX, 0);
int curProgress = intent.getIntExtra(Config.EXTRA_CURRENT, 0);
seekBar.setMax(maxLen);
seekBar.setProgress(curProgress);
StringBuilder stringBuilder = new StringBuilder();
int m = curProgress / 1000 / 60;
int s = curProgress / 1000 % 60;
int maxM = maxLen / 1000 / 60;
int maxS = maxLen / 1000 % 60;
stringBuilder.append(m / 10).append(m % 10)
.append(":").append(s / 10).append(s % 10)
.append("/").append(maxM / 10).append(maxM % 10)
.append(":")
.append(maxS / 10).append(maxS % 10);
textView.setText(stringBuilder.toString());
}
}
}
//PlayService
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.system.Os;
/**
-
Created by Administrator on 2016/7/25 0025.
*/
public class PlayService extends Service{
private MediaPlayer mediaPlayer;
private MySeekReceiver receiver;@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}@Override
public void onCreate() {
super.onCreate();mediaPlayer=MediaPlayer.create(PlayService.this,R.raw.music); receiver=new MySeekReceiver(); registerReceiver(receiver,new IntentFilter(Config.ACTION_PLAY_SEEK));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}else {
mediaPlayer.start();
}
new MyThread().start();
return super.onStartCommand(intent, flags, startId);
}@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
unregisterReceiver(receiver);
}class MySeekReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int seekProgress=intent.getIntExtra(Config.EXTRA_CURRENT,0);
if(mediaPlayer!=null){
mediaPlayer.seekTo(seekProgress);
}
}
}class MyThread extends Thread{
@Override
public void run() {
super.run();
int maxLen=mediaPlayer.getDuration();//获取总长度
while (mediaPlayer != null && mediaPlayer.isPlaying()) {
int progress=mediaPlayer.getCurrentPosition();
Intent intent=new Intent(Config.ACTION_PLAY_PRG);
intent.putExtra(Config.EXTRA_MAX,maxLen);
intent.putExtra(Config.EXTRA_CURRENT,progress);
sendBroadcast(intent);
}
}
}
}