summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/service/PlaybackService.java
blob: 81889462e55073da21e7e08971f6cdccdc21ed52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package de.podfetcher.service;

import java.io.File;

import android.R;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;

import de.podfetcher.activity.MediaplayerActivity;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedManager;

/** Controls the MediaPlayer that plays a FeedMedia-file */
public class PlaybackService extends Service {
	/** Logging tag */
	private static final String TAG = "PlaybackService";
	/** Contains the id of the FeedMedia object. */
	public static final String EXTRA_MEDIA_ID = "extra.de.podfetcher.service.mediaId";
	/** Contains the id of the Feed object of the FeedMedia. */
	public static final String EXTRA_FEED_ID = "extra.de.podfetcher.service.feedId";

	public static final String ACTION_PLAYER_STATUS_CHANGED =
			"action.de.podfetcher.service.playerStatusChanged";
	
	private static final int NOTIFICATION_ID = 1;
	private NotificationCompat.Builder notificationBuilder;
	
	private Notification notification;
	private MediaPlayer player;
	private FeedMedia media;
	private Feed feed;
	private FeedManager manager;
	private PlayerStatus status;
	private PositionSaver positionSaver;

	private final IBinder mBinder = new LocalBinder();
	
	public class LocalBinder extends Binder {
        public PlaybackService getService() {
            return PlaybackService.this;
        }
    }
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(TAG, "Service created.");

		manager = FeedManager.getInstance();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (player != null) {
			player.stop();
		}
		long mediaId = intent.getLongExtra(EXTRA_MEDIA_ID, -1);
		long feedId = intent.getLongExtra(EXTRA_FEED_ID, -1);
		if (mediaId == -1 || feedId == -1) {
			Log.e(TAG, "Media ID or Feed ID wasn't provided to the Service.");
		} else {
			feed = manager.getFeed(feedId);
			media = manager.getFeedMedia(mediaId, feed);
			
			player = MediaPlayer.create(this, Uri.fromFile(new File(media.getFile_url())));
			setStatus(PlayerStatus.PREPARING);
			player.setOnPreparedListener(preparedListener);
			Log.d(TAG, "Preparing to play file");
		}
		setupNotification();
		return Service.START_STICKY;
	}
	
	private void setupPositionSaver() {
		if (positionSaver == null) {
			positionSaver = new PositionSaver() {
				@Override
				protected void onCancelled(Void result) {
					super.onCancelled(result);
					positionSaver = null;
				}

				@Override
				protected void onPostExecute(Void result) {
					super.onPostExecute(result);
					positionSaver = null;
				}				
			};
			positionSaver.execute();
		}
	}

	private MediaPlayer.OnPreparedListener preparedListener = new MediaPlayer.OnPreparedListener() {
		@Override
		public void onPrepared(MediaPlayer mp) {
			Log.d(TAG, "Resource prepared");
			setStatus(PlayerStatus.PREPARED);
			play();
		}
	};
	
	public void pause() {
		if (player.isPlaying()) {
			Log.d(TAG, "Pausing playback.");
			player.pause();
			saveCurrentPosition();
			setStatus(PlayerStatus.PAUSED);
		}
	}
	
	public void play() {
		if (status == PlayerStatus.PAUSED || status == PlayerStatus.PREPARED) {
			Log.d(TAG, "Resuming/Starting playback");
			player.start();
			setStatus(PlayerStatus.PLAYING);
			setupPositionSaver();
		} else if (status == PlayerStatus.STOPPED) {
			
		}
	}
	
	private void setStatus(PlayerStatus newStatus) {
		status = newStatus;
		sendBroadcast(new Intent(ACTION_PLAYER_STATUS_CHANGED));
	}
	
	private void setupNotification() {
		PendingIntent pIntent = PendingIntent.getActivity(
				this, 0, new Intent(this, MediaplayerActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
		
		Bitmap icon = BitmapFactory.decodeResource(null, R.drawable.stat_notify_sdcard);
		notificationBuilder = new NotificationCompat.Builder(this)
			.setContentTitle("Mediaplayer Service")
			.setContentInfo("Click here for more info")
			.setOngoing(true)
			.setContentIntent(pIntent)
			.setLargeIcon(icon)
			.setSmallIcon(R.drawable.stat_notify_sdcard);
			
					
		startForeground(NOTIFICATION_ID, notificationBuilder.getNotification());
		Log.d(TAG, "Notification set up");
	}
	
	/** Seek a specific position from the current position
	 * 	@param delta offset from current position (positive or negative)
	 * */
	public void seekDelta(int delta) {
		seek(player.getCurrentPosition() + delta);
	}
	
	public void seek(int i) {
		Log.d(TAG, "Seeking position " + i);
		player.seekTo(i);
		saveCurrentPosition();
	}
	
	/** Saves the current position of the media file to the DB */
	private synchronized void saveCurrentPosition() {
		Log.d(TAG, "Saving current position to " + player.getCurrentPosition());
		media.setPosition(player.getCurrentPosition());
		manager.setFeedMedia(this, media);
	}
	
	public PlayerStatus getStatus() {
		return status;
	}
	
	public FeedMedia getMedia() {
		return media;
	}
	
	public MediaPlayer getPlayer() {
		return player;
	}
	
	/** Periodically saves the position of the media file */
	class PositionSaver extends AsyncTask<Void, Void, Void> {
		private static final int WAITING_INTERVALL = 5000;
		
		@Override
		protected Void doInBackground(Void... params) {
			while (!isCancelled() && player.isPlaying()) {
				try {
					Thread.sleep(WAITING_INTERVALL);
				} catch (InterruptedException e) {
					Log.d(TAG, "Thread was interrupted while waiting. Finishing now...");
					return null;
				}
				saveCurrentPosition();
			}
			return null;
		}
		
	}

}