Activities interact with services and play songs

Source: Internet
Author: User

Activity interacts with the service and plays songs. For convenience, I have fixed the song to be played. You can flexibly improve it.
 

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;
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;
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 = 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 ){}
59
60 @ override
61 Public void onstarttrackingtouch (seekbar ){}
62
63 @ override
64 public void onprogresschanged (seekbar, int progress,
65 Boolean fromuser ){
66 If (fromuser ){
67 mservice. seekto (Progress SS * 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 ("paused ");
80 mhandler. Post (seek1_read );
81} else {
82 mservice. Pause ();
83 isplaying = false;
84 playbt. settext ("play ");
85 mhandler. removecallbacks (seek1_read );
86}
87}
88 });
89
90}
91
92 runnable seek1_read = 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 (seekreceivread, 200 );
99}
100 };
101
102}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.