Guest User

IBEService.java

a guest
Nov 12th, 2010
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package com.kirodema.icyBeatsBE;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import android.app.Notification;
  8. import android.app.NotificationManager;
  9. import android.app.Service;
  10. import android.content.Intent;
  11. import android.media.MediaPlayer;
  12. import android.media.MediaPlayer.OnCompletionListener;
  13. import android.os.DeadObjectException;
  14. import android.os.IBinder;
  15. import android.os.Parcel;
  16. import android.os.RemoteException;
  17. import android.util.Log;
  18.  
  19. public class IBEService extends Service{
  20.     private MediaPlayer mp = new MediaPlayer();
  21.     private List<String> songs = new ArrayList<String>();
  22.     private int currentPosition;
  23.  
  24.     private NotificationManager nm;
  25.     private static final int NOTIFY_ID = R.layout.songs;
  26.  
  27.     @Override
  28.     public void onCreate() {
  29.         super.onCreate();
  30.         printDebug("Service wird erstellt");
  31.         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  32.     }
  33.  
  34.     @Override
  35.     public void onDestroy() {
  36.         mp.stop();
  37.         mp.release();
  38.         nm.cancel(NOTIFY_ID);
  39.     }
  40.  
  41.  
  42.     private final IBEInterface.Stub mBinder = new IBEInterface.Stub() {
  43.  
  44.         @Override
  45.         public void stop() throws RemoteException {
  46.             nm.cancel(NOTIFY_ID);
  47.             mp.stop();
  48.         }
  49.  
  50.         @Override
  51.         public void skipForward() throws RemoteException {
  52.             nextSong();
  53.         }
  54.  
  55.         @Override
  56.         public void skipBack() throws RemoteException {
  57.             prevSong();
  58.         }
  59.  
  60.         @Override
  61.         public void playFile(int position) throws RemoteException {
  62.             try {
  63.                 currentPosition = position;
  64.                 playSong(songs.get(position));
  65.             } catch (IndexOutOfBoundsException e) {
  66.                 Log.e(getString(R.string.app_name), e.getMessage());
  67.             }
  68.  
  69.         }
  70.  
  71.         @Override
  72.         public void pause() throws RemoteException {
  73.             Notification notify = new Notification(R.drawable.playbackpause, null, 0);
  74.             nm.notify(NOTIFY_ID, notify);
  75.             mp.pause();
  76.         }
  77.  
  78.         @Override
  79.         public void clearPlaylist() throws RemoteException {
  80.             songs.clear();
  81.         }
  82.  
  83.         @Override
  84.         public void addSongPlaylist(String song) throws RemoteException {
  85.             songs.add(song);
  86.         }
  87.     };
  88.  
  89.     public IBinder getBinder() {
  90.         return mBinder;
  91.     }
  92.  
  93.     @Override
  94.     public IBinder onBind(Intent arg0) {
  95.         return null;
  96.     }
  97.  
  98.     private void playSong (String song) {
  99.         try {
  100.             Notification notify = new Notification(R.drawable.playbackstart,
  101.                     song, 0);
  102.             nm.notify(NOTIFY_ID, notify);
  103.  
  104.             mp.reset();
  105.             mp.setDataSource(song);
  106.             mp.prepare();
  107.             mp.start();
  108.  
  109.             mp.setOnCompletionListener(new OnCompletionListener() {
  110.  
  111.                 @Override
  112.                 public void onCompletion(MediaPlayer arg0) {
  113.                     nextSong();
  114.                 }
  115.             });
  116.         } catch (IOException e) {
  117.             Log.e(getString(R.string.app_name), e.getMessage());
  118.         }
  119.     }
  120.  
  121.     private void nextSong() {
  122.         // Check if last song or not
  123.         if (++currentPosition >= songs.size()) {
  124.             currentPosition = 0;
  125.             nm.cancel(NOTIFY_ID);
  126.         } else {
  127.             playSong(songs.get(currentPosition));
  128.         }
  129.     }
  130.     private void prevSong() {
  131.         if (mp.getCurrentPosition() < 3000 && currentPosition >= 1) {
  132.             playSong(songs.get(--currentPosition));
  133.         } else {
  134.             playSong(songs.get(currentPosition));
  135.         }
  136.     }
  137.  
  138.     private void printDebug (String msg) {
  139.         Log.d(getString(R.string.app_name), msg);
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment