android开发 利用Service给游戏添加背景音乐
android开发利用Service给游戏添加背景音乐
1、增加一个类,这个类是继承Service的,如下。
import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MusicServer extends Service { private MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onStart(Intent intent,int startId){ super.onStart(intent, startId); if(mediaPlayer==null){ // R.raw.mmp是资源文件,MP3格式的 mediaPlayer = MediaPlayer.create(this, R.raw.mmp); mediaPlayer.setLooping(true); mediaPlayer.start(); } } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mediaPlayer.stop(); } }2、在AndroidManifest.xml中添加如下代码。
<service android:name=".MusicServer"> <intent-filter> <action android:name="com.angel.Android.MUSIC"/> <category android:name="android.intent.category.default" /> </intent-filter> </service>3、在activity中用
//start background music startService(intent);开启音乐
stopService(intent);关闭音乐
相关推荐
Nostalgiachild 2020-11-13
韩伟佳 2020-10-09
wuleihenbang 2020-09-16
zzqLivecn 2020-07-09
chenjinlong 2020-06-10
yinbaoshiguang 2020-06-09
sgafdsg 2020-06-04
ustcrding 2020-06-03
chenjinlong 2020-06-03
AndroidGA 2020-06-01
安辉 2020-05-27
绿豆饼 2020-05-26
CNETNews 2020-05-26
xilove0 2020-05-12
绿豆饼 2020-05-12
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
hqulyc 2020-05-05
lyccsu 2020-04-30