この質問は以前に何度も出されており、いくつかの質問が寄せ集められているように思えるかもしれませんが、多くの開発者にとって関連性があり、重要であると感じています。Service
アプリケーションが終了すると終了し、次のすべての状況で一時停止する Android ゲームの複数のアクティビティで実行できるバックグラウンド ミュージックを作成する必要があります。
Activity
独自の音楽を持っているとあるが開始されます。(これが終わったら再開Activity
。たまたまAndEngine
アクティビティです。)- ホーム画面が押されてアプリがバックグラウンドになっている、またはアプリが終了している。アプリがフォアグラウンドに戻ると再開します。の使用が必要です
onUserLeaveHint()
。別の役立つリンク。 - 電話が着信し、アプリが中断されます。コールが処理されると再開します。これ
TelephonyManager
に似たものを使用する必要があります。 - 画面がロックされています。(画面のロックが解除された後に再開します。) を使用する
ACTION_USER_PRESENT
必要が あり ます。 - 基本的に、アプリが表示されていないとき、または #1 の特別なアクティビティがユーザーに表示されているときはいつでも音楽が一時停止します。
以上が、私が必要とするすべてのものと、私がまとめた情報です。私の現在のコードは基本的にこれに似ています。
彼らの音楽にこれらの問題がまったくないことに興味があるAndEngine
ので、ソースコードを調べると、答えを探している人に役立つかもしれません. Google Code の最後の機能 GLES1 バージョンを使用しています。
良い音楽を作成する方法については、次のリンクも参照してくださいService
。
- バックグラウンド サービスの音楽を停止する
- http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App
- Android BGM サービス
- Android のアクティビティ全体で BG ミュージックを再生する
- http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion
次の解決策を希望Service
します。
BroadcastReceivers
可能であれば、Android マニフェストの追加/権限の使用を最小限に抑える- 自己完結型およびエラー チェック
その他の注意事項
- 現在、バックグラウンド ミュージックを必要とするすべてのアクティビティは、すべて共通の特別なクラスを拡張しています。
- 音楽はループする必要がありますが、1 つのトラックしか実行しません。
みなさん、お早めにどうぞ!頑張ってください!
編集 - コード スニペットは次のとおりです。自由に改善または無視してください。
メディア プレーヤー ラッパー
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.preference.PreferenceManager;
import android.util.Log;
public class CarefulMediaPlayer {
final SharedPreferences sp;
final MediaPlayer mp;
private boolean isPlaying = false;
public CarefulMediaPlayer(final MediaPlayer mp, final MusicService ms) {
sp = PreferenceManager.getDefaultSharedPreferences(ms.getApplicationContext());
this.mp = mp;
}
public void start() {
if (sp.getBoolean("com.embed.candy.music", true) && !isPlaying) {
mp.start();
isPlaying = true;
}
}
public void pause() {
if (isPlaying) {
mp.pause();
isPlaying = false;
}
}
public void stop() {
isPlaying = false;
try {
mp.stop();
mp.release();
} catch (final Exception e) {}
}
}
音楽サービス
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
static CarefulMediaPlayer mPlayer = null;
@Override
public IBinder onBind(final Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
final MediaPlayer mp = MediaPlayer.create(this, R.raw.title_music);
mp.setLooping(true);
mPlayer = new CarefulMediaPlayer(mp,this);
}
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
mPlayer.start();
return 1;
}
@Override
public void onStart(final Intent intent, final int startId) {
}
public IBinder onUnBind(final Intent arg0) {
return null;
}
public static void onStop() {
mPlayer.stop();
}
public static void onPause() {
if (mPlayer!=null) {
mPlayer.pause();
}
}
public static void onResume() {
if (mPlayer!=null) {
mPlayer.start();
}
}
@Override
public void onDestroy() {
mPlayer.stop();
mPlayer = null;
}
@Override
public void onLowMemory() {
}
}
改善されたベース アクティビティ クラス
import android.app.Activity;
import android.content.Intent;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
public abstract class BetterActivity extends Activity {
private boolean isHome = true;
@Override
protected void onResume() {
System.gc();
super.onResume();
MusicService.onResume();
isHome = true;
}
@Override
protected void onPause() {
if (((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).getCallState()==TelephonyManager.CALL_STATE_RINGING
|| !((PowerManager)getSystemService(POWER_SERVICE)).isScreenOn()) {
MusicService.onPause();
}
super.onPause();
System.gc();
}
@Override
public boolean onKeyDown (final int keyCode, final KeyEvent ke) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
isHome = false;
default:
return super.onKeyDown(keyCode, ke);
}
}
@Override
public void startActivity(final Intent i) {
isHome = false;
super.startActivity(i);
}
@Override
protected void onUserLeaveHint() {
if (isHome) {
MusicService.onPause();
}
super.onUserLeaveHint();
}
}