public class MusicService extends Service {
private MediaPlayer mediaPlayer;
private TimerTask task;
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
@Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void play(){
try {
mediaPlayer.reset();
System.out.println("音乐播放了");
mediaPlayer.setDataSource("/storage/sdcard/erke.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
updateSeekBar();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void seekTo(int position){
mediaPlayer.seekTo(position);
}
private void updateSeekBar() {
final int duration = mediaPlayer.getDuration();
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
int currentPosition = mediaPlayer.getCurrentPosition();
Message msg = Message.obtain();
Bundle budle = new Bundle();
budle.putInt("duration", duration);
budle.putInt("currentPosition", currentPosition);
msg.setData(budle);
MainActivity.handler.sendMessage(msg);
}
};
timer.schedule(task, 100, 1000);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
System.out.println("歌曲播放完成");
timer.cancel();
task.cancel();
}
});
}
public void stop(){
System.out.println("音乐暂停了");
mediaPlayer.pause();
}
public void replay(){
System.out.println("继续播放了");
mediaPlayer.start();
}
private class MyBinder extends Binder implements IService{
@Override
public void callplay() {
play();
}
@Override
public void callstop() {
stop();
}
@Override
public void callreplay() {
replay();
}
@Override
public void callseekTo(int position) {
seekTo(position);
}
}
}
public class MainActivity extends Activity {
public static Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
Bundle data = msg.getData();
int duration = data.getInt("duration");
int currentPosition = data.getInt("currentPosition");
sb.setMax(duration);
sb.setProgress(currentPosition);
};
};
private IService iservice;
private MyConn conn;
private static SeekBar sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,MusicService.class);
startService(intent);
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
sb = (SeekBar) findViewById(R.id.sb);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
iservice.callseekTo(seekBar.getProgress());
}
@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
}
});
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iservice = (IService) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
@Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}
public void click1(View v){
iservice.callplay();
}
public void click2(View v){
iservice.callstop();
}
public void click3(View v){
iservice.callreplay();
}
}
public interface IService {
public void callplay();
public void callstop();
public void callreplay();
public void callseekTo(int position);
}
<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.edu.hgu.musicbox.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="播放"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="暂停"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click3"
android:text="继续播放"
/>
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>