一下,我只把主要的代码贴出来给大家,如果哪里写的不好,希望大家能多多指教,灰常感谢。
/** 定义播放小孩声音的ACTION */
public static String SOUND_ACTION = "com.vtion.ym.box.sound";
XxxActivity.java Activity里面中的代码
View Code
1 @Override 2 public void onClick(View v) { 3 Intent intent = new Intent(Constant.SOUND_ACTION); 4 switch (v.getId()) { 5 case R.id.box_bottom1_lishi: 6 intent.putExtra("play_sound", 1); 7 break; 8 case R.id.box_bottom2_lishi: 9 intent.putExtra("play_sound", 2); 10 break; 11 case R.id.box_bottom3_lishi: 12 intent.putExtra("play_sound", 3); 13 break; 14 case R.id.box_bottom4_lishi: 15 intent.putExtra("play_sound", 4); 16 break; 17 case R.id.box_bottom5_lishi: 18 intent.putExtra("play_sound", 5); 19 break; 20 case R.id.box_bottom6_lishi: 21 intent.putExtra("play_sound", 6); 22 break; 23 case R.id.box_bottom7_lishi: 24 intent.putExtra("play_sound", 7); 25 break; 26 case R.id.box_bottom8_lishi: 27 intent.putExtra("play_sound", 8); 28 break; 29 case R.id.box_bottom9_lishi: 30 intent.putExtra("play_sound", 9); 31 break; 32 case R.id.box_bottom10_lishi: 33 intent.putExtra("play_sound", 10); 34 break; 35 case R.id.box_bottom11_lishi: 36 intent.putExtra("play_sound", 11); 37 break; 38 case R.id.box_bottom12_lishi: 39 intent.putExtra("play_sound", 12); 40 break; 41 case R.id.box_bottom13_lishi: 42 intent.putExtra("play_sound", 13); 43 break; 44 case R.id.box_bottom14_lishi: 45 intent.putExtra("play_sound", 14); 46 break; 47 default: 48 break; 49 } 50 startService(intent); 51 } 52 @Override 53 protected void onStop() { 54 Intent intent = new Intent(Constant.SOUND_ACTION); 55 stopService(intent); 56 super.onStop(); 57 }
XxxService.java Service里面中的代码
View Code
public class SoundService extends Service { private MediaPlayer mediaplayer; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { int play_sound = getSoundId(intent); if (play_sound != 0) { if (mediaplayer != null && mediaplayer.isPlaying()) { mediaplayer.stop(); mediaplayer.release(); mediaplayer = null; mediaplayer = MediaPlayer.create(this, play_sound); mediaplayer.start(); } else if (mediaplayer != null && !mediaplayer.isPlaying()) { mediaplayer = MediaPlayer.create(this, play_sound); mediaplayer.start(); } else { mediaplayer = MediaPlayer.create(this, play_sound); mediaplayer.start(); } } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); try { if (mediaplayer != null || mediaplayer.isPlaying()) { mediaplayer.stop(); mediaplayer.release(); mediaplayer = null; } } catch (Exception e) { Log.i("SERVICE", e.getMessage()); } } /** 得到声音的Id */ private int getSoundId(Intent intent) { int musicId = 0; if(intent != null){ musicId = intent.getIntExtra("play_sound", -1); } switch (musicId) { case 1: return R.raw.wawa_01; case 2: return R.raw.wawa_02; case 3: return R.raw.wawa_03; case 4: return R.raw.wawa_04; case 5: return R.raw.wawa_05; case 6: return R.raw.wawa_06; case 7: return R.raw.wawa_07; case 8: return R.raw.wawa_08; case 9: return R.raw.wawa_09; case 10: return R.raw.wawa_10; case 11: return R.raw.wawa_11; case 12: return R.raw.wawa_12; case 13: return R.raw.wawa_13; case 14: return R.raw.wawa_14; default: return 0; } } }
AndroidManifest.xml
View Code
<service android:name=".box.service.SoundService"> <intent-filter > <action android:name="com.vtion.ym.box.sound" /> <category android:name="android.intent.category.default" /> </intent-filter> </service>