使用AIDL方式实现播放音乐的案例.txt
来源:互联网 发布:银天下太极线指标源码 编辑:程序博客网 时间:2024/06/08 00:56
思路:① 新建两个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。Javapublic 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); } }
0 0
- 使用AIDL方式实现播放音乐的案例.txt
- BindService实现播放音乐的案例.txt
- 利用Service组件播放音乐的案例.txt
- Android多线程:使用AIDL播放MP3音乐
- 自己动手开发音乐播放器《六》播放方式的实现
- 使用服务实现简单的音乐播放
- 22_Android中的本地音乐播放器和网络音乐播放器的编写,本地视频播放器和网络视频播放器,照相机案例,偷拍案例实现
- Android AIDL技术实战项目-音乐播放器(二)-使用retrofit完成音乐API的封装
- 简单的音乐播放器案例
- Android音乐播放案例
- Android实现后台播放音乐(Service方式)
- 汇编实现音乐的播放
- 音乐播放的简单实现
- 使用JS播放声音/音乐的实现方法
- 使用Acitivity实现简易的音乐播放器
- 使用startservice实现一个简单的音乐播放器
- 使用绑定服务实现一个简单的音乐播放器
- 使用H5audio特性实现简单的音乐播放器
- C++实现鼠标控制 封装常见的模拟鼠标、键盘的操作函数
- C中的预编译宏定义
- github与团队的开发者github保存同步
- 三、Sybase创建数据库示例
- 公共子串
- 使用AIDL方式实现播放音乐的案例.txt
- leetcode 453. Minimum Moves to Equal Array Elements
- Python实战(四)——Python 数据分析
- 教育孩子幻想系列-品质排行榜
- 1004. Counting Leaves (30) 树的遍历 DFS BFS
- AI火爆,但你知道人工智能、机器学习和深度学习有何不同?
- 蓝桥杯历届试题——带分数(dfs+全排列)
- Android之反编译APK
- YOLO9000: Better,Faster,Stronger