首页 > 系统 > Android > 正文

Android使用AIDL方式实现播放音乐案例

2019-10-22 18:12:51
字体:
来源:转载
供稿:网友

本文实例为大家分享了Android使用AIDL方式实现播放音乐的具体代码,供大家参考,具体内容如下

思路:

① 新建两个APP项目或者Module,一个作为服务端,一个作为客户端,在服务端创建service  

② 在两个module的main文件中分别新建两个aidl文件(接口),里边定义处理音乐的方法

③ 在两个AIDL文件定义过方法后在任务栏给他们makeproject,编译成Java文件,才能在service和acvitity中使用
interface.Stub需要实例化,实现远程方法

④.Service中onbind方法,返回的是:interface.Stub。

sevice需要设置action,不然客户端service运行时会报空指针异常
先处理服务端:

Mainfest文件中,为intent隐式调用添加action 

 <service   android:name=".MusicService"   android:enabled="true"   android:exported="true">   <intent-filter>   <action android:name="com.work.MusicService"></action> </intent-filter>

MusicService。Java

public class MusicService extends Service {  private MediaPlayer player = null;  public MusicService() {  }  //①实现已经makeproject过的adil接口,重写里边自定义定义的三个方法  MusicAidlInterface.Stub stub = new MusicAidlInterface.Stub() {    @Override    public void paly() throws RemoteException {      if (player == null) {        player = MediaPlayer.create(MusicService.this, R.raw.hckz);      }      if (player != null && !player.isPlaying()){        player.start();      }    }    @Override    public void paus() throws RemoteException {      if(player!=null&&player.isPlaying()){        player.pause();      }    }    @Override    public void stop() throws RemoteException {      if(player!=null){        player.stop();      }      try {        player.prepare();//重新准备下次播放      } catch (IOException e) {        e.printStackTrace();      }    }  };  @Override  public IBinder onBind(Intent intent) {    return stub;  }  //② 重写service销毁时的方法  @Override  public void onDestroy() {    super.onDestroy();    if(player!=null){      player.stop();      player.release();//释放资源,防止失去依托溢出,发生异常    }  }

处理客户端

public class MainActivity extends AppCompatActivity implements View.OnClickListener {  private Button btn_play, btn_pause, btn_stop, btn_stopservice, btn_stopacvitity;   MusicAidlInterface service = null;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    intiView();    connect();  }  private void intiView() {    btn_play = (Button) findViewById(R.id.btn_play);    btn_pause = (Button) findViewById(R.id.btn_pause);    btn_stopservice = (Button) findViewById(R.id.btn_stopservice);    btn_stopacvitity = (Button) findViewById(R.id.btn_exitacvitity);    btn_stop = (Button) findViewById(R.id.btn_stop);    btn_play.setOnClickListener(this);    btn_pause.setOnClickListener(this);    btn_stop.setOnClickListener(this);    btn_stopservice.setOnClickListener(this);    btn_stopacvitity.setOnClickListener(this);  }  @Override  public void onClick(View v) {    try {      switch (v.getId()) {        case R.id.btn_play:          service.paly();          break;        case R.id.btn_pause:          service.paus();          break;        case R.id.btn_stop:          service.stop();          break;        case R.id.btn_stopservice:          if (conn != null) {            unbindService(conn);          }          break;        case R.id.btn_exitacvitity:          finish();          break;      }    } catch (RemoteException e) {      e.printStackTrace();    }  }  //连接service  ServiceConnection conn = new ServiceConnection() {    @Override    public void onServiceConnected(ComponentName name, IBinder iBinder) {      service = MusicAidlInterface.Stub.asInterface(iBinder);    }    @Override    public void onServiceDisconnected(ComponentName name) {      service = null;    }  };  //获取连接BindService的方法  private void connect() {    //使用intent的隐式调用方法    Intent intent = new Intent("com.work.MusicService");    //设置目标service的包名    intent.setPackage("com.work.musicservice");    bindService(intent, conn, BIND_AUTO_CREATE);  }  //重写acvitity销毁时候解除绑定的方法  @Override  protected void onDestroy() {    super.onDestroy();    if (conn != null) {//解除绑定      unbindService(conn);    }  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表