4.Android添加背景音乐的方法

 1 public class music {
2
3 private static MediaPlayer mp =null;
4 public static void play(Context context,int resource){
5 stop(context);
6 mp = MediaPlayer.create(context, resource);
7 mp.setLooping(true);
8 mp.start();
9 }
10 public static void stop(Context context) {
11 // TODO Auto-generated method stub
12 if(mp!= null){
13 mp.stop();
14 mp.release();
15 mp = null;
16 }
17 }
18 }

重写下面两个方法

 1 @Override
2 protected void onPause() {
3 // TODO Auto-generated method stub
4 super.onPause();
5 music.stop(this);
6 }
7
8
9 @Override
10 protected void onResume() {
11 // TODO Auto-generated method stub
12 super.onResume();
13 music.play(this, R.raw.game);
14 }



原文地址:https://www.cnblogs.com/renkangke/p/2416344.html