Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进
MService:
View Code
1 package com.tiantian.test;
2
3 import android.app.Service;
4 import android.content.Intent;
5 import android.media.MediaPlayer;
6 import android.os.Binder;
7 import android.os.Environment;
8 import android.os.IBinder;
9 import android.util.Log;
10
11 public class MService extends Service{
12 MyBinder myBinder = new MyBinder();
13 private MediaPlayer mediaPlayer;
14
15 public class MyBinder extends Binder{
16 MService getService(){
17 return MService.this;
18 }
19 }
20 @Override
21 public IBinder onBind(Intent intent) {
22 // TODO Auto-generated method stub
23 Log.v("CAT", "onBind");
24 return myBinder;
25 }
26
27 @Override
28 public void onCreate() {
29 // TODO Auto-generated method stub
30 super.onCreate();
31 Log.v("CAT", "onCreate");
32 try {
33 mediaPlayer = new MediaPlayer();
34 mediaPlayer.setDataSource(Environment.getExternalStorageDirectory() + "/mp3/trhxn.mp3");
35 mediaPlayer.prepare();
36 } catch (Exception e) {
37 // TODO Auto-generated catch block
38 Log.v("CAT", "fail");
39 e.printStackTrace();
40 }
41 }
42
43 @Override
44 public void onDestroy() {
45 // TODO Auto-generated method stub
46 super.onDestroy();
47 Log.v("CAT", "onDestroy");
48 }
49
50 @Override
51 public int onStartCommand(Intent intent, int flags, int startId) {
52 // TODO Auto-generated method stub
53 Log.v("CAT", "onStartCommand");
54 return super.onStartCommand(intent, flags, startId);
55 }
56
57 @Override
58 public boolean onUnbind(Intent intent) {
59 // TODO Auto-generated method stub
60 Log.v("CAT", "onUnbind");
61 return false;
62 }
63
64 public void start(){
65 mediaPlayer.start();
66 }
67 public void pause(){
68 mediaPlayer.pause();
69 }
70 public void stop(){
71 mediaPlayer.stop();
72 mediaPlayer.release();
73 }
74 public int getDuration(){
75 return mediaPlayer.getDuration();
76 }
77 public int getCurrentPosition(){
78 return mediaPlayer.getCurrentPosition();
79 }
80 public void seekTo(int position){
81 mediaPlayer.seekTo(position);
82 }
83 }
MusicPlayActivity:
View Code
1 package com.tiantian.test;
2
3 import android.app.Activity;
4 import android.content.ComponentName;
5 import android.content.Intent;
6 import android.content.ServiceConnection;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.IBinder;
10 import android.util.Log;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.SeekBar;
15
16 public class MusicPlayActivity extends Activity {
17 /** Called when the activity is first created. */
18 MService mService;
19 private ServiceConnection conn = new ServiceConnection(){
20
21 @Override
22 public void onServiceConnected(ComponentName arg0, IBinder arg1) {
23 // TODO Auto-generated method stub
24 mService = ((MService.MyBinder)arg1).getService();
25 Log.v("CAT", "getServiced");
26 }
27
28 @Override
29 public void onServiceDisconnected(ComponentName name) {
30 // TODO Auto-generated method stub
31 mService = null;
32 }
33
34 };
35
36 private SeekBar seekBar;
37 private Button playBT;
38
39 private boolean isPlaying = false;
40 private boolean isBinded = false;
41
42 private Handler mHandler;
43 @Override
44 public void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46 setContentView(R.layout.main);
47 Intent intent = new Intent(MusicPlayActivity.this, MService.class);
48 if(!isBinded){
49 bindService(intent, conn, BIND_AUTO_CREATE);
50 isBinded = true;
51 }
52 seekBar = (SeekBar) findViewById(R.id.seekBar);
53 playBT = (Button) findViewById(R.id.startBT);
54 mHandler = new Handler();
55 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
56
57 @Override
58 public void onStopTrackingTouch(SeekBar seekBar) {}
59
60 @Override
61 public void onStartTrackingTouch(SeekBar seekBar) {}
62
63 @Override
64 public void onProgressChanged(SeekBar seekBar, int progress,
65 boolean fromUser) {
66 if(fromUser){
67 mService.seekTo((progress*mService.getDuration())/100);
68 }
69 }
70 });
71 playBT.setOnClickListener(new OnClickListener() {
72
73 @Override
74 public void onClick(View v) {
75 // TODO Auto-generated method stub
76 if(!isPlaying){
77 mService.start();
78 isPlaying = true;
79 playBT.setText("暂停");
80 mHandler.post(seekBarThread);
81 }else{
82 mService.pause();
83 isPlaying = false;
84 playBT.setText("播放");
85 mHandler.removeCallbacks(seekBarThread);
86 }
87 }
88 });
89
90 }
91
92 Runnable seekBarThread = new Runnable() {
93
94 @Override
95 public void run() {
96 // TODO Auto-generated method stub
97 seekBar.setProgress((mService.getCurrentPosition()*100)/mService.getDuration());
98 mHandler.postDelayed(seekBarThread, 200);
99 }
100 };
101
102 }