在简单游戏中播放 Android 的背景音乐
Playing Background Music in Android in a Simple Game
我正在尝试使用 Services
在 Android 上的简单游戏中播放背景音乐。
使用 link: Android Life Cycles
但是此代码无法正常工作,调用了 onResumeActivity, onPauseActivity
,但即使调用了 onPauseActivity
方法,音乐也会在后台保持 运行。
当应用程序处于后台时,音乐会继续播放。
有没有其他方法可以在 Android App/Game 中播放背景音乐?
我认为这段代码适合你。添加此 class(附在您的 activity class 中)。
public class BackgroundMusic extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
MediaPlayer backgroundmusic = MediaPlayer.create(YourActivity.this, R.raw.yourbackgroundmusic);
player.setVolume(100,100);
player.setLooping(true);
player.start();
return null;
}
}
并创建它
BackgroundMusic bm = new BackgroundMusic();
关于 onResume 方法:
public void onResume() {
super.onResume();
bm.execute(null);
}
和 onPause 方法:
public void onPause() {
super.onPause();
bm.cancel(true);
}
希望对您有所帮助!
public class SoundGameBaseActivity extends Activity {
public static boolean isSoundPaused = false;
public static MediaPlayer mp;
protected static final String TAG = SoundGameBaseActivity.class.getName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playMusic();
}
public static boolean isAppWentToBg = false;
public static boolean isWindowFocused = false;
public static boolean isMenuOpened = false;
public static boolean isBackPressed = false;
@Override
protected void onStart() {
Log.d(TAG, "onStart isAppWentToBg " + isAppWentToBg);
applicationWillEnterForeground();
super.onStart();
}
private void applicationWillEnterForeground() {
if (isAppWentToBg) {
//Google Analytics
MyApp.getInstance().trackScreenView("ApplicationActivated");
isAppWentToBg = false;
// Toast.makeText(getApplicationContext(), "App is in foreground",
// Toast.LENGTH_SHORT).show();
playMusic();
}
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop ");
applicationdidenterbackground();
}
public void applicationdidenterbackground() {
if (!isWindowFocused) {
isAppWentToBg = true;
Toast.makeText(getApplicationContext(),
"App is Going to Background", Toast.LENGTH_SHORT).show();
stopMusic();
}
}
public void TurnOnMusicAgain() {
}
void playMusic() {
if (mp == null) {
mp = MediaPlayer.create(this, R.raw.background1);
// mp.prepare();
mp.start();
mp.setLooping(true);
} else {
if (mp.isPlaying()) {
} else {
Log.e("", "coming back");
mp.start();
}
}
}
void stopMusic() {
if (mp != null)
mp.pause();
}
@Override
public void onBackPressed() {
if (this instanceof StartScreen) {
} else {
isBackPressed = true;
}
Log.d(TAG,
"onBackPressed " + isBackPressed + ""
+ this.getLocalClassName());
super.onBackPressed();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
isWindowFocused = hasFocus;
Log.e("Is Window Focus", "" + isWindowFocused);
if (isBackPressed && !hasFocus) {
isBackPressed = false;
isWindowFocused = true;
}
super.onWindowFocusChanged(hasFocus);
}
}
我正在尝试使用 Services
在 Android 上的简单游戏中播放背景音乐。
使用 link: Android Life Cycles
但是此代码无法正常工作,调用了 onResumeActivity, onPauseActivity
,但即使调用了 onPauseActivity
方法,音乐也会在后台保持 运行。
当应用程序处于后台时,音乐会继续播放。
有没有其他方法可以在 Android App/Game 中播放背景音乐?
我认为这段代码适合你。添加此 class(附在您的 activity class 中)。
public class BackgroundMusic extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
MediaPlayer backgroundmusic = MediaPlayer.create(YourActivity.this, R.raw.yourbackgroundmusic);
player.setVolume(100,100);
player.setLooping(true);
player.start();
return null;
}
}
并创建它
BackgroundMusic bm = new BackgroundMusic();
关于 onResume 方法:
public void onResume() {
super.onResume();
bm.execute(null);
}
和 onPause 方法:
public void onPause() {
super.onPause();
bm.cancel(true);
}
希望对您有所帮助!
public class SoundGameBaseActivity extends Activity {
public static boolean isSoundPaused = false;
public static MediaPlayer mp;
protected static final String TAG = SoundGameBaseActivity.class.getName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playMusic();
}
public static boolean isAppWentToBg = false;
public static boolean isWindowFocused = false;
public static boolean isMenuOpened = false;
public static boolean isBackPressed = false;
@Override
protected void onStart() {
Log.d(TAG, "onStart isAppWentToBg " + isAppWentToBg);
applicationWillEnterForeground();
super.onStart();
}
private void applicationWillEnterForeground() {
if (isAppWentToBg) {
//Google Analytics
MyApp.getInstance().trackScreenView("ApplicationActivated");
isAppWentToBg = false;
// Toast.makeText(getApplicationContext(), "App is in foreground",
// Toast.LENGTH_SHORT).show();
playMusic();
}
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop ");
applicationdidenterbackground();
}
public void applicationdidenterbackground() {
if (!isWindowFocused) {
isAppWentToBg = true;
Toast.makeText(getApplicationContext(),
"App is Going to Background", Toast.LENGTH_SHORT).show();
stopMusic();
}
}
public void TurnOnMusicAgain() {
}
void playMusic() {
if (mp == null) {
mp = MediaPlayer.create(this, R.raw.background1);
// mp.prepare();
mp.start();
mp.setLooping(true);
} else {
if (mp.isPlaying()) {
} else {
Log.e("", "coming back");
mp.start();
}
}
}
void stopMusic() {
if (mp != null)
mp.pause();
}
@Override
public void onBackPressed() {
if (this instanceof StartScreen) {
} else {
isBackPressed = true;
}
Log.d(TAG,
"onBackPressed " + isBackPressed + ""
+ this.getLocalClassName());
super.onBackPressed();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
isWindowFocused = hasFocus;
Log.e("Is Window Focus", "" + isWindowFocused);
if (isBackPressed && !hasFocus) {
isBackPressed = false;
isWindowFocused = true;
}
super.onWindowFocusChanged(hasFocus);
}
}