- Android comprehensive failproof music service across multiple activites
- 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();
- }
- }
- public class MusicService extends Service {
- // service binder
- private final IBinder mBinder = new LocalBinder();
- // music player controling game music
- private static CarefulMediaPlayer mPlayer = null;
- @Override
- public void onCreate() {
- // load music file and create player
- MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.title_music);
- mediaPlayer.setLooping(true);
- mPlayer = new CarefulMediaPlayer(mediaPlayer, this);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- // =========================
- // Player methods
- // =========================
- public void musicStart() {
- mPlayer.start();
- }
- public void musicStop() {
- mPlayer.stop();
- }
- public void musicPause() {
- mPlayer.pause();
- }
- /**
- * Class for clients to access. Because we know this service always runs in
- * the same process as its clients, we don't need to deal with IPC.
- */
- public class LocalBinder extends Binder {
- MusicService getService() {
- return MusicService.this;
- }
- }
- @Override
- public IBinder onBind(Intent arg0) {
- return mBinder;
- }
- }
- public class StartupActivity extends Activity {
- // bounded service
- private static MusicService mBoundService;
- // whetere service is bounded or not
- private boolean mIsBound;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_startup);
- doBindService();
- // HOW TO WORK WITH THE SERVICE:
- // call the following methods whenever
- // you want to interact with you
- // music player
- // ===================================
- // call this e.g. in onPause() of your Activities
- StartupActivity.getService().musicPause();
- // call this e.g. in onStop() of your Activities
- StartupActivity.getService().musicStop();
- // call this e.g. in onResume() of your Activities
- StartupActivity.getService().musicStart();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- doUnbindService();
- }
- private final ServiceConnection mServiceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName className, IBinder service) {
- setService(((MusicService.LocalBinder) service).getService());
- }
- @Override
- public void onServiceDisconnected(ComponentName className) {
- setService(null);
- }
- };
- private void doBindService() {
- Intent service = new Intent(getBaseContext(), MusicService.class);
- // start service and bound it
- startService(service);
- bindService(new Intent(this, MusicService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
- mIsBound = true;
- }
- private void doUnbindService() {
- if (mIsBound) {
- // Detach existing connection.
- unbindService(mServiceConnection);
- mIsBound = false;
- }
- }
- public static MusicService getService() {
- return mBoundService;
- }
- private static void setService(MusicService mBoundService) {
- StartupActivity.mBoundService = mBoundService;
- }
- public class CarefulMediaPlayer {
- final SharedPreferences sp;
- final MediaPlayer mp;
- private boolean isPlaying = false;
- private static CarefulMediaPlayer instance;
- public CarefulMediaPlayer(final MediaPlayer mp, final MusicService ms) {
- sp = PreferenceManager.getDefaultSharedPreferences(ms.getApplicationContext());
- this.mp = mp;
- instance = this;
- }
- public static CarefulMediaPlayer getInstance() {
- return instance;
- }
- 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) {}
- }