音乐播放器

布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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=".MainActivity" >
  
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
  
        <Button
            android:id="@+id/bt_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放" />
  
        <Button
            android:id="@+id/bt_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暂停" />
  
        <Button
            android:id="@+id/bt_replay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="重放" />
  
        <Button
            android:id="@+id/bt_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />
    </LinearLayout>
  
</LinearLayout>

MainActivty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.sanya.musicplayer;
  
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
  
public class MainActivity extends Activity implements OnClickListener {
  
    private Button bt_play;
    private Button bt_pause;
    private Button bt_replay;
    private Button bt_stop;
    private MyConn myConn;
    private IService myBind;
    private Intent intent;
    private TelephonyManager tm;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        bt_play = (Button) findViewById(R.id.bt_play);
        bt_pause = (Button) findViewById(R.id.bt_pause);
        bt_replay = (Button) findViewById(R.id.bt_replay);
        bt_stop = (Button) findViewById(R.id.bt_stop);
  
        bt_play.setOnClickListener(this);
        bt_pause.setOnClickListener(this);
        bt_replay.setOnClickListener(this);
        bt_stop.setOnClickListener(this);
          
        intent = new Intent(getApplicationContext(), MuserService.class);
  
        startService(intent);
        myConn = new MyConn();
        bindService(intent, myConn, BIND_AUTO_CREATE);
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
          
          
    }
    private class MyListener extends PhoneStateListener{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
      System.out.println("电话的状态:"+state);
            switch (state) {
            //来电
            case TelephonyManager.CALL_STATE_RINGING:
                System.out.println("来电1");
                myBind.pause();
                break;
                //挂断
            case TelephonyManager.CALL_STATE_IDLE:
                System.out.println("来电2");
                myBind.start();
                break;
                //接通
            case TelephonyManager.CALL_STATE_OFFHOOK:
                System.out.println("来电3");
                myBind.pause();
                break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
          
    }
    private class MyConn implements ServiceConnection {
  
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBind = (IService) service;
        }
  
        @Override
        public void onServiceDisconnected(ComponentName name) {
  
        }
  
    }
  
    @Override
    public void onClick(View v) {
        tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);
        switch (v.getId()) {
        case R.id.bt_play:
            myBind.play();
            Toast.makeText(MainActivity.this, "播放", 1).show();
            break;
        case R.id.bt_pause:
            if ("暂停".equals(bt_pause.getText().toString())) {
                myBind.pause();
                bt_pause.setText("继续");
                Toast.makeText(MainActivity.this, "暂停", 1).show();
            } else {
                myBind.start();
                bt_pause.setText("暂停");
                Toast.makeText(MainActivity.this, "继续", 1).show();
            }
  
            break;
        case R.id.bt_replay:
            myBind.replay();
            Toast.makeText(MainActivity.this, "重播", 1).show();
            break;
        case R.id.bt_stop:
            myBind.stop();
            Toast.makeText(MainActivity.this, "停止", 1).show();
            break;
  
        }
  
    }
  
    // 退出时解除服务的绑定
    @Override
    protected void onDestroy() {
        myBind.stop();
        unbindService(myConn);
        System.out.println("解除绑定服务");
        stopService(intent);
        System.out.println("停止服务");
        super.onDestroy();
    }
  
}

服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.sanya.musicplayer;
  
public interface IService {
  
    void play();
  
    void pause();
  
    void replay();
  
    void stop();
    void start();
  
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.sanya.musicplayer;
  
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
  
public class MuserService extends Service {
  
    private MediaPlayer mediaPlayer;
  
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBind();
    }
  
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
  
        return super.onStartCommand(intent, flags, startId);
    }
  
    private class MyBind extends Binder implements IService {
  
        @Override
        public void play() {
  
            bt_play();
  
        }
  
        @Override
        public void pause() {
            bt_pause();
        }
  
        @Override
        public void replay() {
            bt_replay();
  
        }
  
        @Override
        public void stop() {
            bt_stop();
  
        }
  
        public void start() {
            if (mediaPlayer != null) {
  
                mediaPlayer.start();
                System.out.println("service开始播放");
            }
        }
  
    }
  
    private void bt_play() {
  
        String path = "/sdcard/1.mp3";
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            System.out.println("资源初始化");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mediaPlayer != null) {
  
            mediaPlayer.start();
            System.out.println("service开始播放");
        }
    }
  
    private void bt_pause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            System.out.println("暂停重新播放");
        }
    }
  
    private void bt_replay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            System.out.println("service重新播放");
        } else {
            bt_play();
        }
  
    }
  
    private void bt_stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
            System.out.println("service停止播放");
        }
  
    }
}

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
    package="com.sanya.musicplayer"
    android:versionCode="1"
    android:versionName="1.0" >
  
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sanya.musicplayer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MuserService" >
        </service>
    </application>
  
</manifest>

 

 

 

 



来自为知笔记(Wiz)


posted on 2014-04-28 13:59  转折点人生  阅读(152)  评论(0)    收藏  举报