Service 音乐播放器

package com.example.lenovo.musicplay;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PlayActivity1 extends AppCompatActivity {

    TextView tv_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play1);
        tv_1=(TextView)findViewById(R.id.tv_1);
        Intent intent=new Intent(this,MusicService1.class);
    }
    Intent intent;
    public void bt_1(View v)
    {
        //向service发送播放指令
        intent.putExtra("action", "play");
        startService(intent);
        tv_1.setText("播放状态:正在播放");
    }
    public void bt_2(View v)
    {
        //向service发送停止指令
        intent.putExtra("action","stop");
        startService(intent);
        tv_1.setText("播放状态:停止播放");
    }
    public void bt_3(View v)
    {
        //向service发送暂停指令
        intent.putExtra("action","pause");
        startService(intent);
        tv_1.setText("播放状态:暂停播放");
    }
    public void bt_4(View v)
    {
        //向service发送退出指令
        bt_3(v);
        stopService(intent);
        finish();
    }
}
playactivity
package com.example.lenovo.musicplay;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MusicService1 extends Service {
    public MusicService1() {
    }

    //播放器对象
    MediaPlayer mediaPlayer;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //处理指令
        String ac=intent.getStringExtra("action");
        switch (ac)
        {
            case "play":
                if (mediaPlayer == null) {
                    //用静态方法构造
                    mediaPlayer = MediaPlayer.create(this, R.raw.fuqin);
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mediaPlayer.reset();
                            mediaPlayer.release();
                            mediaPlayer = null;
                        }
                    });
                }
                mediaPlayer.start();
                break;
            case "stop":
                if (mediaPlayer!=null) {
                    mediaPlayer.stop();//停止
                    mediaPlayer.reset();//重置
                    mediaPlayer.release();//释放
                    mediaPlayer = null;//置空
                }
                break;
            case "pause":
                if (mediaPlayer!=null&&mediaPlayer.isPlaying())
                {
                    mediaPlayer.pause();//暂停
                }
                break;
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
musicservice.java

 

 

 

package com.example.lenovo.musicplay;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Vector;

public class Play2Activity extends AppCompatActivity {
    TextView tv_1;
    ProgressBar pb_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play2);
        pb_1 = (ProgressBar) findViewById(R.id.pb_1);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        Intent intent=new Intent(this,MusicService2.class);
        playServiceConnection=new PlayServiceConnection();
        //绑定
        bindService(intent,playServiceConnection, Context.BIND_AUTO_CREATE);
    }

    //代理对象
    MusicService2.PlayBinder playBinder;
    PlayServiceConnection playServiceConnection;

    //服务连接类
    class PlayServiceConnection implements ServiceConnection
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //得到代理对象
            playBinder=(MusicService2.PlayBinder) service;
            Log.e("TAG","得到代理对象");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
    public void bt_1(View v)
    {
        if (playBinder!=null) {
            playBinder.play();
        }
    }
    public void bt_2(View v)
    {
        if (playBinder!=null) {
            playBinder.pause();
        }
    }
    public void bt_3(View v)
    {
        if (playBinder!=null) {
            playBinder.stop();
        }
    }
    public void bt_4(View v)
    {
        if (playServiceConnection!=null) {
            bt_3(v);
            unbindService(playServiceConnection);//解绑
            finish();
        }
    }

}
playactivity2
package com.example.lenovo.musicplay;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class MusicService2 extends Service {

    //播放器对象
    MediaPlayer mediaPlayer;
    public class PlayBinder extends Binder
    {
        public void play()
        {
            if (mediaPlayer == null) {
                //用静态方法构造
                mediaPlayer = MediaPlayer.create(MusicService2.this, R.raw.fuqin);
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mediaPlayer.reset();
                        mediaPlayer.release();
                        mediaPlayer = null;
                    }
                });
            }
            mediaPlayer.start();
        }

        public void pause()
        {
            if (mediaPlayer!=null&&mediaPlayer.isPlaying())
            {
                mediaPlayer.pause();//暂停
            }
        }
        public void stop()
        {
            if (mediaPlayer!=null) {
                mediaPlayer.stop();//停止
                mediaPlayer.reset();//重置
                mediaPlayer.release();//释放
                mediaPlayer = null;//置空
            }
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return new PlayBinder();
       }
}
musicservice2

 

 

 

加进度条(Service   跨线程+消息机制)

<?xml version="1.0" encoding="utf-8"?>
<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: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="com.example.lenovo.musicplay.PlayActivity1"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放状态:未播放"
        android:id="@+id/tv_1"/>
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/pb_1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放"
            android:onClick="bt_1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止"
            android:onClick="bt_2"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停"
            android:onClick="bt_3"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="退出"
            android:onClick="bt_4"/>
    </LinearLayout>

</LinearLayout>
activity
package com.example.lenovo.musicplay;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class MusicService1 extends Service {
    public MusicService1() {
    }

    //播放器对象
    MediaPlayer mediaPlayer;
    String strPlay="等待播放";
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //处理指令
        String ac=intent.getStringExtra("action");
        switch (ac)
        {
            case "play":
                if (mediaPlayer == null) {
                    //用静态方法构造
                    mediaPlayer = MediaPlayer.create(this, R.raw.fuqin);
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mediaPlayer.reset();
                            mediaPlayer.release();
                            mediaPlayer = null;
                            strPlay="完成播放";
                        }
                    });
                }
                mediaPlayer.start();
                strPlay="正在播放";
                break;
            case "stop":
                if (mediaPlayer!=null) {
                    mediaPlayer.stop();//停止
                    mediaPlayer.reset();//重置
                    mediaPlayer.release();//释放
                    mediaPlayer = null;//置空
                    strPlay="停止播放";
                }
                break;
            case "pause":
                if (mediaPlayer!=null&&mediaPlayer.isPlaying())
                {
                    mediaPlayer.pause();//暂停
                    strPlay="暂停播放";
                }
                break;
        }

        return super.onStartCommand(intent, flags, startId);
    }
    //代理对象
    public class PlayBinder extends Binder
    {
        //得到进度条总长度
        public int getMusicDuration()
        {
            int rtn=100;
            if (mediaPlayer!=null)
            {
                rtn=mediaPlayer.getDuration();
            }
            return rtn;
        }
        //得到当前播放进度
        public int getMusicCurrentPosition()
        {
            int rtn=0;
            if (mediaPlayer!=null)
            {
                rtn=mediaPlayer.getCurrentPosition();
            }
            return rtn;
        }
        //得到当前状态
        public String getPlayState()
        {
            return strPlay;
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        return new PlayBinder();
    }
}
musicservice
package com.example.lenovo.musicplay;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PlayActivity1 extends AppCompatActivity {

    TextView tv_1;
    ProgressBar pb_1;
    Intent intent;
    PlaySC playSC;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play1);
        tv_1=(TextView)findViewById(R.id.tv_1);
        pb_1=(ProgressBar)findViewById(R.id.pb_1);
        intent=new Intent(this,MusicService1.class);
        playSC=new PlaySC();
        //绑定
        bindService(intent,playSC, Context.BIND_AUTO_CREATE);
        handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg){
                if (msg.what==1)
                {
                    String str=msg.obj.toString();
                    tv_1.setText("播放状态:"+str);
                }
            }
        };
    }
    public void bt_1(View v)
    {
        //向service发送播放指令
        intent.putExtra("action", "play");
        startService(intent);
        //tv_1.setText("播放状态:正在播放");
    }
    public void bt_2(View v)
    {
        //向service发送停止指令
        intent.putExtra("action","stop");
        startService(intent);
        //tv_1.setText("播放状态:停止播放");
    }
    public void bt_3(View v)
    {
        //向service发送暂停指令
        intent.putExtra("action","pause");
        startService(intent);
        //tv_1.setText("播放状态:暂停播放");
    }
    public void bt_4(View v)
    {
        //向service发送退出指令
        bt_3(v);
        stopService(intent);
        if (playSC!=null)
        {
            unbindService(playSC);//解绑
            playSC=null;
        }
        finish();
    }

    class PlaySC implements ServiceConnection
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            final MusicService1.PlayBinder playBinder=(MusicService1.PlayBinder)service;
            //启动子线程
            new Thread()
            {
                @Override
                public void run() {
                    while (true)
                    {
                        int max=playBinder.getMusicDuration();
                        pb_1.setMax(max);
                        int c=playBinder.getMusicCurrentPosition();
                        pb_1.setProgress(c);
                        //设置播放状态
                        Message msg=new Message();
                        msg.what=1;
                        msg.obj=playBinder.getPlayState();
                        handler.sendMessage(msg);
                        try {
                            Thread.sleep(500);
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}
playactivity

 

posted @ 2016-06-28 14:15  百事没事  阅读(293)  评论(0编辑  收藏  举报