要播放的媒體播放器......我們需要兩個類.. 讓我們假設mainactivity.java
是我們的第一個文件.. 這裏我們定義兩個按鈕 - start_button
& stop_button
mButton_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mIntent=new Intent(MainActivity.this,maservice.class);
startService(mIntent);
}
});
mButton_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mIntent=new Intent(MainActivity.this,maservice.class);
stopService(mIntent);
}
});
maservice.java是我們的另一個java文件。在這裏我們定義媒體播放器,也應該有3個方法:onCreate(),onStart(),onDestroy()。
下面是代碼:
MediaPlayer mPlayer;
@Override
public void onCreate()
{
super.onCreate();
mPlayer=MediaPlayer.create(this, R.raw.kyun);
mPlayer.setLooping(true);
}
@Override
public void onStart(Intent miIntent, int startid)
{
super.onStart(miIntent, startid);
mPlayer.start();
}
@Override
public void onDestroy()
{
super.onDestroy();
mPlayer.stop();
}
我們也有清單文件來定義
- mainactivity.java下活動標籤
- 定義這些Java文件,但maservice.java定義根據服務標籤
把您的代碼放在這裏,以便我們可以幫到你 –
我編輯了m y question –
我已經發布了我的答案請嘗試一下 –