Android多线程:使用AIDL播放MP3音乐

一、AIDL的源代码框架


籍由开发工具自动产出Proxy及Stub类的程序代码,在分别交给ac01和Mp3Binder开发者。于是ac01和Mp3Binder两端的开发者都不必具备IPC的知识。

二、完整的程序代码

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.misoo.pkaz"
    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.pkaz.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=".Mp3Service" android:process=".remote">
            <intent-filter>
                <action android:name="com.misoo.pkaz.REMOTE_SERVICE" />
            </intent-filter>
        </service>
    </application>

</manifest>
Mp3PlayerInterface.aidl:

interface Mp3PlayerInterface {
	void play();
	void stop();
}
Mp3Service:

package com.misoo.pkaz;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class Mp3Service extends Service {
	IBinder ib = null;
	
	public void onCreate() {
		super.onCreate();
		ib = new Mp3Binder(this.getApplicationContext());
	}
	
	public void onDestroy() {
		super.onDestroy();
	}
	
	public IBinder onBind(Intent intent) {
		return ib;
	}
}
Mp3Binder:

package com.misoo.pkaz;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.RemoteException;

public class Mp3Binder extends Mp3PlayerInterface.Stub {
	private MediaPlayer mediaPlayer;
	private Context ctx;

	public Mp3Binder(Context cx) {
		ctx = cx;
	}

	public void play() throws RemoteException {
		if (mediaPlayer != null)
			return;
		mediaPlayer = MediaPlayer.create(ctx, R.raw.san);

		mediaPlayer.start();
	}

	public void stop() throws RemoteException {
		if(mediaPlayer != null){
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}
}
MyButton:

package com.misoo.pkaz;

import android.content.Context;
import android.widget.Button;

public class MyButton extends Button {
	public MyButton(Context ctx){
		super(ctx);
		super.setBackgroundResource(R.drawable.pig_egg);
	}
	
	public int get_width(){
		return 135;
	}
	
	public int get_height(){
		return 60;
	}
}
ac01:

package com.misoo.pkaz;

import android.os.Bundle;
import android.os.IBinder;
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.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 Mp3PlayerInterface playerInterface = null;
	private MyButton btn0, btn1, btn2;
	public TextView tv;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);

		btn0 = new MyButton(this);
		btn0.setId(101);
		btn0.setText("play");
		btn0.setOnClickListener(this);

		btn1 = new MyButton(this);
		btn1.setId(102);
		btn1.setText("stop");
		btn1.setOnClickListener(this);

		btn2 = new MyButton(this);
		btn2.setId(103);
		btn2.setText("exit");
		btn2.setOnClickListener(this);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				btn2.get_width(), btn2.get_height());
		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.pkaz.REMOTE_SERVICE"));
		bindService(new Intent("com.misoo.pkaz.REMOTE_SERVICE"), mConnection,
				Context.BIND_AUTO_CREATE);
	}

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceDisconnected(ComponentName name) {

		}

		public void onServiceConnected(ComponentName name, IBinder service) {
			playerInterface = Mp3PlayerInterface.Stub.asInterface(service);
		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case 101:
			tv.setText("play...");

			try {
				playerInterface.play();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			break;
		case 102:
			tv.setText("stop.");
			try {
				playerInterface.stop();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case 103:
			unbindService(mConnection);
			stopService(new Intent("com.misoo.pkaz.REMOTE_SERVICE"));
			finish();
			break;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值