Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 7.59 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android comprehensive failproof music service across multiple activites
  2. import android.content.SharedPreferences;
  3. import android.media.MediaPlayer;
  4. import android.preference.PreferenceManager;
  5. import android.util.Log;
  6.  
  7. public class CarefulMediaPlayer {
  8.     final SharedPreferences sp;
  9.     final MediaPlayer mp;
  10.     private boolean isPlaying = false;
  11.  
  12.     public CarefulMediaPlayer(final MediaPlayer mp, final MusicService ms) {
  13.         sp = PreferenceManager.getDefaultSharedPreferences(ms.getApplicationContext());
  14.         this.mp = mp;
  15.     }
  16.  
  17.     public void start() {
  18.         if (sp.getBoolean("com.embed.candy.music", true) && !isPlaying) {
  19.             mp.start();
  20.             isPlaying = true;
  21.         }
  22.     }
  23.  
  24.     public void pause() {
  25.         if (isPlaying) {
  26.             mp.pause();
  27.             isPlaying = false;
  28.         }
  29.     }
  30.  
  31.     public void stop() {
  32.         isPlaying = false;
  33.         try {
  34.             mp.stop();
  35.             mp.release();
  36.         } catch (final Exception e) {}
  37.     }
  38. }
  39.        
  40. import android.app.Service;
  41. import android.content.Intent;
  42. import android.media.MediaPlayer;
  43. import android.os.IBinder;
  44.  
  45. public class MusicService extends Service {
  46.     static CarefulMediaPlayer mPlayer = null;
  47.  
  48.     @Override
  49.     public IBinder onBind(final Intent arg0) {
  50.         return null;
  51.     }
  52.  
  53.     @Override
  54.     public void onCreate() {
  55.         super.onCreate();
  56.         final MediaPlayer mp = MediaPlayer.create(this, R.raw.title_music);
  57.         mp.setLooping(true);
  58.         mPlayer = new CarefulMediaPlayer(mp,this);
  59.     }
  60.  
  61.     @Override
  62.     public int onStartCommand(final Intent intent, final int flags, final int startId) {
  63.         mPlayer.start();
  64.         return 1;
  65.     }
  66.  
  67.     @Override
  68.     public void onStart(final Intent intent, final int startId) {
  69.  
  70.     }
  71.  
  72.     public IBinder onUnBind(final Intent arg0) {
  73.         return null;
  74.     }
  75.  
  76.     public static void onStop() {
  77.         mPlayer.stop();
  78.     }
  79.  
  80.     public static void onPause() {
  81.         if (mPlayer!=null) {
  82.             mPlayer.pause();
  83.         }
  84.     }
  85.  
  86.     public static void onResume() {
  87.         if (mPlayer!=null) {
  88.             mPlayer.start();
  89.         }
  90.     }
  91.  
  92.     @Override
  93.     public void onDestroy() {
  94.         mPlayer.stop();
  95.         mPlayer = null;
  96.     }
  97.  
  98.     @Override
  99.     public void onLowMemory() {
  100.  
  101.     }
  102. }
  103.        
  104. import android.app.Activity;
  105. import android.content.Intent;
  106. import android.os.PowerManager;
  107. import android.telephony.TelephonyManager;
  108. import android.view.KeyEvent;
  109. import android.view.LayoutInflater;
  110. import android.view.View;
  111. import android.view.ViewGroup;
  112. import android.view.ViewGroup.LayoutParams;
  113. import android.widget.ImageView;
  114.  
  115. public abstract class BetterActivity extends Activity {
  116.  
  117.     private boolean isHome = true;
  118.  
  119.     @Override
  120.     protected void onResume() {
  121.         System.gc();
  122.         super.onResume();
  123.         MusicService.onResume();
  124.         isHome = true;
  125.     }
  126.  
  127.     @Override
  128.     protected void onPause() {
  129.         if (((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).getCallState()==TelephonyManager.CALL_STATE_RINGING
  130.                 || !((PowerManager)getSystemService(POWER_SERVICE)).isScreenOn()) {
  131.             MusicService.onPause();
  132.         }
  133.         super.onPause();
  134.         System.gc();
  135.     }
  136.  
  137.     @Override
  138.     public boolean onKeyDown (final int keyCode, final KeyEvent ke) {
  139.         switch (keyCode) {
  140.         case KeyEvent.KEYCODE_BACK:
  141.             isHome = false;
  142.         default:
  143.             return super.onKeyDown(keyCode, ke);
  144.         }
  145.     }
  146.  
  147.     @Override
  148.     public void startActivity(final Intent i) {
  149.         isHome = false;
  150.         super.startActivity(i);
  151.     }
  152.  
  153.     @Override
  154.     protected void onUserLeaveHint() {
  155.         if (isHome) {
  156.             MusicService.onPause();
  157.         }
  158.         super.onUserLeaveHint();
  159.     }
  160.  
  161. }
  162.        
  163. public class MusicService extends Service {
  164.  
  165.     // service binder
  166.     private final IBinder mBinder = new LocalBinder();
  167.  
  168.     // music player controling game music
  169.     private static CarefulMediaPlayer mPlayer = null;
  170.  
  171.     @Override
  172.     public void onCreate() {
  173.         // load music file and create player
  174.         MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.title_music);
  175.         mediaPlayer.setLooping(true);
  176.         mPlayer = new CarefulMediaPlayer(mediaPlayer, this);
  177.     }
  178.  
  179.     @Override
  180.     public void onDestroy() {
  181.         super.onDestroy();
  182.     }
  183.  
  184.     // =========================
  185.     // Player methods
  186.     // =========================
  187.     public void musicStart() {
  188.         mPlayer.start();
  189.     }
  190.  
  191.     public void musicStop() {
  192.         mPlayer.stop();
  193.     }
  194.  
  195.     public void musicPause() {
  196.         mPlayer.pause();
  197.     }
  198.  
  199.     /**
  200.      * Class for clients to access. Because we know this service always runs in
  201.      * the same process as its clients, we don't need to deal with IPC.
  202.      */
  203.     public class LocalBinder extends Binder {
  204.         MusicService getService() {
  205.             return MusicService.this;
  206.         }
  207.     }
  208.  
  209.     @Override
  210.     public IBinder onBind(Intent arg0) {
  211.         return mBinder;
  212.     }
  213.  
  214. }
  215.        
  216. public class StartupActivity extends Activity {
  217.  
  218. // bounded service
  219. private static MusicService mBoundService;
  220.  
  221. // whetere service is bounded or not
  222. private boolean mIsBound;
  223.  
  224. @Override
  225. public void onCreate(Bundle savedInstanceState) {
  226.     super.onCreate(savedInstanceState);
  227.     setContentView(R.layout.activity_startup);
  228.     doBindService();
  229.  
  230.     // HOW TO WORK WITH THE SERVICE:
  231.     // call the following methods whenever
  232.     // you want to interact with you
  233.     // music player
  234.     // ===================================
  235.  
  236.     // call this e.g. in onPause() of your Activities
  237.     StartupActivity.getService().musicPause();
  238.  
  239.     // call this e.g. in onStop() of your Activities
  240.     StartupActivity.getService().musicStop();
  241.  
  242.     // call this e.g. in onResume() of your Activities
  243.     StartupActivity.getService().musicStart();
  244. }
  245.  
  246. @Override
  247. public void onDestroy() {
  248.     super.onDestroy();
  249.     doUnbindService();
  250. }
  251.  
  252. private final ServiceConnection mServiceConnection = new ServiceConnection() {
  253.  
  254.     @Override
  255.     public void onServiceConnected(ComponentName className, IBinder service) {
  256.         setService(((MusicService.LocalBinder) service).getService());
  257.     }
  258.  
  259.     @Override
  260.     public void onServiceDisconnected(ComponentName className) {
  261.         setService(null);
  262.     }
  263. };
  264.  
  265. private void doBindService() {
  266.     Intent service = new Intent(getBaseContext(), MusicService.class);
  267.     // start service and bound it
  268.     startService(service);
  269.     bindService(new Intent(this, MusicService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
  270.     mIsBound = true;
  271. }
  272.  
  273. private void doUnbindService() {
  274.     if (mIsBound) {
  275.         // Detach existing connection.
  276.         unbindService(mServiceConnection);
  277.         mIsBound = false;
  278.     }
  279. }
  280.  
  281. public static MusicService getService() {
  282.     return mBoundService;
  283. }
  284.  
  285. private static void setService(MusicService mBoundService) {
  286.     StartupActivity.mBoundService = mBoundService;
  287. }
  288.        
  289. public class CarefulMediaPlayer {
  290. final SharedPreferences sp;
  291. final MediaPlayer mp;
  292. private boolean isPlaying = false;
  293. private static CarefulMediaPlayer instance;
  294.  
  295. public CarefulMediaPlayer(final MediaPlayer mp, final MusicService ms) {
  296.     sp = PreferenceManager.getDefaultSharedPreferences(ms.getApplicationContext());
  297.     this.mp = mp;
  298.     instance = this;
  299. }
  300.  
  301. public static CarefulMediaPlayer getInstance() {
  302.     return instance;
  303. }
  304.  
  305. public void start() {
  306.     if (sp.getBoolean("com.embed.candy.music", true) && !isPlaying) {
  307.         mp.start();
  308.         isPlaying = true;
  309.     }
  310. }
  311.  
  312. public void pause() {
  313.     if (isPlaying) {
  314.         mp.pause();
  315.         isPlaying = false;
  316.     }
  317. }
  318.  
  319. public void stop() {
  320.     isPlaying = false;
  321.     try {
  322.         mp.stop();
  323.         mp.release();
  324.     } catch (final Exception e) {}
  325. }