码迷,mamicode.com
首页 > 其他好文 > 详细

使用绑定服务实现一个简单的音乐播放器

时间:2015-05-13 21:56:18      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

效果

技术分享

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center_horizontal"
        >

    <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"
            android:onClick="stopOnClick"
            />
    <Button
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放"
            android:onClick="playOnClick"

            />
    <Button
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暂停"
            android:onClick="pauseOnClick"

            />
</LinearLayout>

服务类书写

package com.ht.bindservicedemo;

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

/**
 * Created by IntelliJ IDEA
 * Project: com.ht.bindservicedemo
 * Author: 安诺爱成长
 * Email: 1399487511@qq.com
 * Date: 2015/5/13
 */
public class PlayService extends Service {

    private MediaPlayer player;

    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.ai);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyControl();
    }

    /**
     * 用于交给当前服绑定的组件
     */
    class MyControl extends Binder{
        public void play() {
            if (player != null) {
                player.start();
            }
        }

        public void stop() {
            if (player != null) {
                player.stop();
            }
        }

        public void pause() {
            if (player != null) {
                player.pause();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        player.stop();
        player.release();
        player = null;
    }
}

activity书写

package com.ht.bindservicedemo;

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.view.View;

public class MainActivity extends Activity implements ServiceConnection {

    private PlayService.MyControl myControl;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //绑定服务
        Intent intent = new Intent(this, PlayService.class);
        bindService(intent, this, BIND_AUTO_CREATE);
    }

    //绑定服务使用的方法
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        myControl = (PlayService.MyControl) service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        myControl = null;
    }

    //取消绑定
    @Override
    protected void onStop() {
        super.onStop();
        unbindService(this);
    }

    //三个按钮的监听器
    public void stopOnClick(View view) {
        if (myControl != null) {
            myControl.stop();
        }
    }

    public void playOnClick(View view) {
        if (myControl != null) {
            myControl.play();
        }
    }

    public void pauseOnClick(View view) {
        if (myControl != null) {
            myControl.pause();
        }
    }
}

使用绑定服务实现一个简单的音乐播放器

标签:

原文地址:http://blog.csdn.net/a910626/article/details/45697627

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!