summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/activity/MediaplayerActivity.java
blob: 974e7052393d9007c776bc89e55c02d5f5cb3443 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package de.podfetcher.activity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

import de.podfetcher.R;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.service.PlayerStatus;
import de.podfetcher.util.Converter;

public class MediaplayerActivity extends SherlockActivity {
	private final String TAG = "MediaplayerActivity";
	
	private PlaybackService playbackService;
	private MediaPositionObserver positionObserver;
	
	private FeedMedia media;
	private PlayerStatus status;
	
	
	// Widgets
	private ImageView imgvCover;
	private TextView txtvStatus;
	private TextView txtvPosition;
	private TextView txtvLength;
	private SeekBar sbPosition;
	private ImageButton butPlay;
	private ImageButton butRev;
	private ImageButton butFF;
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "Activity stopped");
		unregisterReceiver(statusUpdate);
		unbindService(mConnection);
		if (positionObserver != null) {
			positionObserver.cancel(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}
	
	

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "Resuming Activity");
		bindToService();
		registerReceiver(statusUpdate, new IntentFilter(PlaybackService.ACTION_PLAYER_STATUS_CHANGED));
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "Creating Activity");
		this.setContentView(R.layout.mediaplayer_activity);
		
		setupGUI();
		bindToService();
		registerReceiver(statusUpdate, new IntentFilter(PlaybackService.ACTION_PLAYER_STATUS_CHANGED));
	}
	
	private void bindToService() {
		if(!bindService(new Intent(this, PlaybackService.class), mConnection, 0)) {
			status = PlayerStatus.STOPPED;
			handleStatus();
		}
	}
	
	private void handleStatus() {
		switch (status) {
			
		case ERROR:
			setStatusMsg(R.string.player_error_msg, View.VISIBLE);
			handleError();
			break;
		case PAUSED:
			setStatusMsg(R.string.player_paused_msg, View.VISIBLE);
			loadMediaInfo();
			if (positionObserver != null) {
				positionObserver.cancel(true);
				positionObserver = null;
			}
			butPlay.setImageResource(android.R.drawable.ic_media_play);
			break;
		case PLAYING:
			setStatusMsg(0, View.INVISIBLE);
			loadMediaInfo();
			setupPositionObserver();
			butPlay.setImageResource(android.R.drawable.ic_media_pause);
			break;
		case PREPARING:
			setStatusMsg(R.string.player_preparing_msg, View.VISIBLE);
			break;
		}
	}
	
	private void setStatusMsg(int resId, int visibility) {
		if(visibility == View.VISIBLE) {
			txtvStatus.setText(resId);
		}
		txtvStatus.setVisibility(visibility);
	}
	
	private void setupPositionObserver() {
		if (positionObserver == null) {
			positionObserver = new MediaPositionObserver() {

				@Override
				protected void onProgressUpdate(Integer... values) {
					super.onProgressUpdate(values);
					txtvPosition.setText(
							Converter.getDurationStringLong(values[0]));
					
					float progress = ((float) values[0]) / getDuration();
					sbPosition.setProgress((int) (progress * 100));
				}
				
			};
			positionObserver.execute(playbackService.getPlayer());
		}
	}
	
	private void loadMediaInfo() {
		if (media != null) {
			MediaPlayer player = playbackService.getPlayer();
			
			getSupportActionBar().setTitle(media.getItem().getTitle());
			getSupportActionBar().setSubtitle(
					media.getItem().getFeed().getTitle());
			
			imgvCover.setImageBitmap(
					media.getItem().getFeed().getImage().getImageBitmap());
			
			txtvPosition.setText(Converter.getDurationStringLong((player.getCurrentPosition())));
			txtvLength.setText(Converter.getDurationStringLong(player.getDuration()));
		}
	}
	
	private void setupGUI() {
		imgvCover = (ImageView) findViewById(R.id.imgvCover);
		txtvPosition = (TextView) findViewById(R.id.txtvPosition);
		txtvLength = (TextView) findViewById(R.id.txtvLength);
		txtvStatus = (TextView) findViewById(R.id.txtvStatus);
		sbPosition = (SeekBar) findViewById(R.id.sbPosition);
		butPlay = (ImageButton) findViewById(R.id.butPlay);
		butRev = (ImageButton) findViewById(R.id.butRev);
		butFF = (ImageButton) findViewById(R.id.butFF);
		
		sbPosition.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			int duration;
			float prog;
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (fromUser) {
					prog = progress / 100.0f;
					duration = playbackService.getPlayer().getDuration();
					txtvPosition.setText(Converter.getDurationStringLong((int) (prog * duration)));
				}
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// interrupt position Observer, restart later
				if (positionObserver != null) {
					positionObserver.cancel(true);
					positionObserver = null;
				}
			}
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				playbackService.seek((int) (prog * duration));
				setupPositionObserver();
			}
		});
		
		butPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (status == PlayerStatus.PLAYING) {
					playbackService.pause();
				} else if (status == PlayerStatus.PAUSED) {
					playbackService.play();
				}
			}	
		});
	}
	
	
	private void handleError() {
		// TODO implement
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			playbackService = ((PlaybackService.LocalBinder)service).getService();
			status = playbackService.getStatus();
			media = playbackService.getMedia();
			handleStatus();
			Log.d(TAG, "Connection to Service established");
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			playbackService = null;
			Log.d(TAG, "Disconnected from Service");
			
		}
	};
	
	private BroadcastReceiver statusUpdate = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Received statusUpdate Intent.");
			status = playbackService.getStatus();
			handleStatus();
		}
	};
	
	/** Refreshes the current position of the media file that is playing. */
	public class MediaPositionObserver extends AsyncTask<MediaPlayer, Integer, Boolean> {
		
		private static final int WAITING_INTERVALL = 1000;
		private MediaPlayer player;
		private int duration;
		
		@Override
		protected void onCancelled(Boolean result) {
			Log.d(TAG, "Task was cancelled");
		}
		
		@Override
		protected Boolean doInBackground(MediaPlayer... p) {
			Log.d(TAG, "Background Task started");
			player = p[0];
			duration = player.getDuration();
			
			while(player.isPlaying() && !isCancelled()) {
				try {
					Thread.sleep(WAITING_INTERVALL);
				} catch(InterruptedException e) {
					Log.d(TAG, "Thread was interrupted while waiting. Finishing now");
					return false;
				}
				publishProgress(player.getCurrentPosition());
			}
			Log.d(TAG, "Background Task finished");
			return true;
		}
		
		public int getDuration() {
			return duration;
		}
	}
	
	
	
}