bindService 实现音乐播放的功能

来源:互联网 发布:会员积分软件dnnyun 编辑:程序博客网 时间:2024/06/05 18:25


1:需要Activity实现ServiceConnection的接口

2:需要Service 提供一个继承了Binder的类来提供内部的方法。

3:进度条的设置

4:注册清单文件


public class MyActivity extends Activity implements ServiceConnection,Runnable {    private Thread thread;    private TextView textView;    private Handler handler;    private boolean flag;    private ProgressBar progressBar;    /**     * Called when the activity is first created.     */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Intent service = new Intent(this, PlayService.class);        bindService(service,this,BIND_AUTO_CREATE);        textView = (TextView)findViewById(R.id.main_progress_textView);        progressBar = (ProgressBar)findViewById(R.id.main_progressbar);        thread = new Thread(this);        handler = new Handler(){            @Override            public void handleMessage(Message msg) {                super.handleMessage(msg);                int position =msg.arg1;                int total = msg.arg2;                textView.setText(""+position+total);            }        };        thread.start();    }    public void run(){        while(true){            try {                Thread.sleep(1000);                if(flag!=false) {                    int postion = controller.getCurrentPosition();                    int total = controller.getDuration();//                    textView.setText(postion);                    Message msg = new Message();                    msg.arg1=postion;                    msg.arg2=total;                    handler.sendMessage(msg);                    progressBar.setProgress(postion);                }            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    private PlayService.Controller controller;    @Override    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {        if(PlayService.class.getName().equals(componentName.getClassName())){            controller = (PlayService.Controller)iBinder;            progressBar.setMax(controller.getDuration());            Log.d("151217MY", "Connected");        }    }    public void btnPlay(View view) {        if(progressBar.getVisibility()==ProgressBar.INVISIBLE){            progressBar.setVisibility(ProgressBar.VISIBLE);        }        controller.play();        flag=true;    }    public void btnPause(View view) {        controller.pause();        flag=false;    }    @Override    public void onServiceDisconnected(ComponentName componentName) {    }}


public class PlayService extends Service{    private MediaPlayer player;    @Override    public IBinder onBind(Intent intent) {        return new Controller();    }    public class Controller extends Binder{        public void play(){            player.start();        }        public void pause(){            player.pause();        }        public int getCurrentPosition(){            return player.getCurrentPosition();        }        public int getDuration(){            return player.getDuration();        }    }    @Override    public void onCreate() {        super.onCreate();        player = MediaPlayer.create(this, R.raw.nobody);    }    @Override    public void onDestroy() {        super.onDestroy();        player.stop();        player.release();    }
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="fill_parent"              android:layout_height="fill_parent"        >    <TextView            android:id="@+id/main_progress_textView"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="progress_textView"            />    <ProgressBar            android:id="@+id/main_progressbar"            android:layout_width="match_parent"            android:layout_height="wrap_content"            style="@android:style/Widget.ProgressBar.Horizontal"            android:visibility="invisible"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="btnPlay"        android:text="Play"        />    <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="btnPause"            android:text="Pause"            /></LinearLayout>
}

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.zqxue.HomeWork151216"          android:versionCode="1"          android:versionName="1.0"        ><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>    <uses-sdk android:minSdkVersion="15"/>    <application            android:label="@string/app_name"            android:icon="@drawable/ic_launcher"            >        <activity                android:name="MyActivity"                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=".service.PlayService"/>        <receiver android:name=".receiver.PlayerBroadcastReceiver">            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED"/>            </intent-filter>        </receiver>        <service android:name=".service.StartService"/>    </application></manifest>

0 0
原创粉丝点击