android多线程:自己撰写Proxy/Stub播放MP3音乐
2013-09-16 17:43
507 查看
一、架构设计
在这个范例中,定义了一个IPlayer接口,然后规划了PlayerProxy和PlayerStub两个类,如下图:
于是,PlayerProxy与PlayerStub分别位于跨进程IPC的两端。
二、完整的实现程序
AndroidManifest.xml:
IPlayer:
Mp3RemoteService :
PlayerStub:
Mp3Binder:
ac01:
在这个范例中,定义了一个IPlayer接口,然后规划了PlayerProxy和PlayerStub两个类,如下图:
于是,PlayerProxy与PlayerStub分别位于跨进程IPC的两端。
二、完整的实现程序
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.misoo.pxgx" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.misoo.pxgx.ac01" 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=".Mp3RemoteService" android:process=".remote" > <intent-filter> <action android:name="com.misoo.pkgx.REMOTE_SERVICE" /> </intent-filter> </service> </application> </manifest>
IPlayer:
package com.misoo.pxgx; public interface IPlayer { void play(); void stop(); String getStatus(); }
Mp3RemoteService :
package com.misoo.pxgx; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class Mp3RemoteService extends Service { private IBinder mBinder = null; public void onCreate() { mBinder = new Mp3Binder(getApplicationContext()); } public IBinder onBind(Intent intent) { return mBinder; } }
PlayerStub:
package com.misoo.pxgx; import android.os.Binder; import android.os.Parcel; import android.os.RemoteException; public abstract class PlayerStub extends Binder implements IPlayer { protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { reply.writeString(data.readString() + " mp3"); if(code == 1) this.play(); else if(code == 2) this.stop(); return true; } public abstract void play(); public abstract void stop(); public abstract String getStatus(); }
Mp3Binder:
package com.misoo.pxgx; import android.content.Context; import android.media.MediaPlayer; import android.util.Log; public class Mp3Binder extends PlayerStub { private MediaPlayer mediaPlayer = null; private Context ctx; public Mp3Binder(Context cx) { ctx = cx; } public void play() { if(mediaPlayer != null) return; mediaPlayer = MediaPlayer.create(ctx, R.raw.san); try { mediaPlayer.start(); } catch (IllegalStateException e) { Log.e("StartPlay", "error:" + e.getMessage(), e); } } public void stop() { if(mediaPlayer != null){ mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } public String getStatus() { return null; } }
ac01:
package com.misoo.pxgx; import android.os.Bundle; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class ac01 extends Activity implements OnClickListener{ private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int MP = LinearLayout.LayoutParams.MATCH_PARENT; private Button btn0, btn1, btn2; public TextView tv; private PlayerProxy playerProxy = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn0 = new Button(this); btn0.setId(101); btn0.setText("play"); btn0.setOnClickListener(this); btn1 = new Button(this); btn1.setId(102); btn1.setText("stop"); btn1.setOnClickListener(this); btn2 = new Button(this); btn2.setId(103); btn2.setText("exit"); btn2.setOnClickListener(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(135, 60); params.topMargin = 10; layout.addView(btn0, params); layout.addView(btn1, params); layout.addView(btn2, params); tv = new TextView(this); tv.setText("Ready"); LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(MP, WC); params2.topMargin = 10; layout.addView(tv, params2); setContentView(layout); startService(new Intent("com.misoo.pkgx.REMOTE_SERVICE")); bindService(new Intent("com.misoo.pkgx.REMOTE_SERVICE"), mConnection, Context.BIND_AUTO_CREATE); } private ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { } public void onServiceConnected(ComponentName name, IBinder service) { playerProxy = new PlayerProxy(service); } }; public void onClick(View v) { switch(v.getId()){ case 101: playerProxy.play(); tv.setText(playerProxy.getStatus()); break; case 102: playerProxy.stop(); tv.setText(playerProxy.getStatus()); break; case 103: unbindService(mConnection); stopService(new Intent("com.misoo.pxgx.REMOTE_SERVICE")); finish(); break; } } private class PlayerProxy implements IPlayer{ private IBinder ib; private String mStatus; public PlayerProxy(IBinder ib) { super(); this.ib = ib; } public void play() { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeString("playing"); try { ib.transact(1, data, reply, 0); mStatus = reply.readString(); } catch (RemoteException e) { e.printStackTrace(); } } public void stop() { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeString("stop"); try { ib.transact(2, data, reply, 0); mStatus = reply.readString(); } catch (RemoteException e) { e.printStackTrace(); } } public String getStatus() { return mStatus; } } }
相关文章推荐
- Android多线程:使用IBinder播放MP3音乐
- Android平台mp3音乐播放流程分析 ----从AP到audioflinger
- android mp3 播放系统基本原理,可以播放音乐的哦
- Android实例:多线程文件下载+在线音乐播放+清除下载文件
- Android命令行播放MP3音乐
- Android 音视频深入 十四 FFmpeg与OpenSL ES 播放mp3音乐,能暂停(附源码
- Android 音视频深入 十四 FFmpeg与OpenSL ES 播放mp3音乐,能暂停(附源码下载)
- android的service学习案例------自己做的音乐播放器,让service后台播放mp3文件(是burning.mp3哦!)
- html制作简单框架网页 实现自己的音乐驿站 操作步骤及源文件下载 (播放功能限mp3文件)
- Android实现本地音乐播放(一、实现音频的读取)
- Android 监听系统音乐播放器播放的音乐的方法
- 【Android学习笔记】Android播放音乐示例代码
- MFC编程如何播放mp3音乐
- android 实现流媒体播放远程mp3文件代码
- android实现开机自动播放音乐实例(Broadcast、Service)
- [置顶] 【Android】Android开发实现进度条效果,SeekBar的简单使用。音量,音乐播放进度,视频播放进度等
- Android如何判断当前手机是否正在播放音乐,并获取到正在播放的音乐的信息
- 【Android】入门级连接网络示例: 网页浏览和播放网络MP3
- 【Android】简易音乐播放器(介绍使用Service和Broadcast播放音乐文件)
- 改造taglib彻底解决amarok等kde下音乐播放程序播放mp3时的中文乱码问题!