Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kirodema.icyBeatsBE;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.DeadObjectException;
- import android.os.IBinder;
- import android.os.Parcel;
- import android.os.RemoteException;
- import android.util.Log;
- public class IBEService extends Service{
- private MediaPlayer mp = new MediaPlayer();
- private List<String> songs = new ArrayList<String>();
- private int currentPosition;
- private NotificationManager nm;
- private static final int NOTIFY_ID = R.layout.songs;
- @Override
- public void onCreate() {
- super.onCreate();
- printDebug("Service wird erstellt");
- nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- }
- @Override
- public void onDestroy() {
- mp.stop();
- mp.release();
- nm.cancel(NOTIFY_ID);
- }
- private final IBEInterface.Stub mBinder = new IBEInterface.Stub() {
- @Override
- public void stop() throws RemoteException {
- nm.cancel(NOTIFY_ID);
- mp.stop();
- }
- @Override
- public void skipForward() throws RemoteException {
- nextSong();
- }
- @Override
- public void skipBack() throws RemoteException {
- prevSong();
- }
- @Override
- public void playFile(int position) throws RemoteException {
- try {
- currentPosition = position;
- playSong(songs.get(position));
- } catch (IndexOutOfBoundsException e) {
- Log.e(getString(R.string.app_name), e.getMessage());
- }
- }
- @Override
- public void pause() throws RemoteException {
- Notification notify = new Notification(R.drawable.playbackpause, null, 0);
- nm.notify(NOTIFY_ID, notify);
- mp.pause();
- }
- @Override
- public void clearPlaylist() throws RemoteException {
- songs.clear();
- }
- @Override
- public void addSongPlaylist(String song) throws RemoteException {
- songs.add(song);
- }
- };
- public IBinder getBinder() {
- return mBinder;
- }
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
- private void playSong (String song) {
- try {
- Notification notify = new Notification(R.drawable.playbackstart,
- song, 0);
- nm.notify(NOTIFY_ID, notify);
- mp.reset();
- mp.setDataSource(song);
- mp.prepare();
- mp.start();
- mp.setOnCompletionListener(new OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer arg0) {
- nextSong();
- }
- });
- } catch (IOException e) {
- Log.e(getString(R.string.app_name), e.getMessage());
- }
- }
- private void nextSong() {
- // Check if last song or not
- if (++currentPosition >= songs.size()) {
- currentPosition = 0;
- nm.cancel(NOTIFY_ID);
- } else {
- playSong(songs.get(currentPosition));
- }
- }
- private void prevSong() {
- if (mp.getCurrentPosition() < 3000 && currentPosition >= 1) {
- playSong(songs.get(--currentPosition));
- } else {
- playSong(songs.get(currentPosition));
- }
- }
- private void printDebug (String msg) {
- Log.d(getString(R.string.app_name), msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment