Android background and music
foreword
Simply add background images and music to the app
1. Add a background image
- Prepare a suitable background image
- Create a new Empty Activity project and select the Java language
- Create a mipmap file in res, and paste the prepared picture into the file directory
- Add code in activity_main.xml to insert the background image
android:background="@mipmap/picture_1"
- The effect is as follows
2. Add background music
- Prepare the right music first
- Create a raw file in res
- Put the music into the raw file directory
- Create a Java file named MusicServer.java, add code
package com.example.myapplication;
import android.content.Context;
import android.media.MediaPlayer;
public class MusicServer{
private static MediaPlayer mp =null;
public static void play(Context context, int resource){
stop(context);
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
public static void stop(Context context) {
if(mp!= null){
mp.stop();
mp.release();
mp = null;
}
}
}
- Add code to MainActivity to make background music take effect
@Override
protected void onPause() {
super.onPause();
stop(this);
}
@Override
protected void onResume() {
super.onResume();
play(this, R.raw.music_1);
}
3123d36dae40ede36fb8572032b26b64
In addition to this method, StartService can also control the background music very well
Lin Huan
Original link: https://blog.csdn.net/qq_52972314/article/details/128177466?spm=1001.2014.3001.5501