2010年1月31日 星期日

Mp3Player With Service

Activity:

public class Mp3PlayerWithService extends Activity implements OnClickListener {
private static final String TAG = "Mp3Interface";
private static Mp3PlayerWithService appRef = null;

private ImageButton btn, btn2, btn3;
public TextView tv;
private IBinder ib;

public static Mp3PlayerWithService getApp() {
return appRef;
}

public void btEvent(String data) {
setTitle(data);
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
appRef = this;

btn = (ImageButton) findViewById(R.id.PlayMP3);
btn2 = (ImageButton) findViewById(R.id.Stop);
btn3 = (ImageButton) findViewById(R.id.exit);
btn.setId(101);
btn2.setId(102);
btn3.setId(103);

btn.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

tv = (TextView) findViewById(R.id.TextView01);
tv.setText("Ready");

bindService(new Intent(Mp3PlayerWithService.this, mp3Service.class),
mContection, Context.BIND_AUTO_CREATE);
}

private ServiceConnection mContection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder ibinder) {
ib = ibinder;
tv.setText(className.toString());
}

@Override
public void onServiceDisconnected(ComponentName className) {
}
};

public void onClick(View v) {
switch (v.getId()) {
case 101:
try {
ib.transact(1, null, null, 0);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 102:
try {
ib.transact(2, null, null, 0);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 103:
finish();
break;
}
}
}

Binder:


public class mp3PlayerBinder extends Binder{
private MediaPlayer mPlayer = null;
private Context ctx;

public mp3PlayerBinder(Context cx){
ctx = cx; }
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags){
if(code == 1)
this.play();
else if(code == 2)
this.stop();
return true;
}
public void play(){
if(mPlayer != null)
return;
mPlayer = MediaPlayer.create(ctx, R.raw.test);
mPlayer.start();
}
public void stop(){
if(mPlayer != null){
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
}

Service:


public class mp3Service extends Service{
private IBinder mBinder = null;
@Override
public void onCreate(){
mBinder = new mp3PlayerBinder(getApplicationContext());
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
}

layout:



?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
LinearLayout android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_width="fill_parent">
ImageButton android:id="@+id/PlayMP3"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:src="@drawable/play" />
ImageButton android:id="@+id/Stop" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:src="@drawable/stop" />
ImageButton android:id="@+id/exit" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:src="@drawable/exit" />
      /LinearLayout>
/LinearLayout>

沒有留言:

張貼留言