Implemented seeking and pausing/resuming
This commit is contained in:
parent
f2a54f8505
commit
76f1f7054d
|
@ -12,9 +12,11 @@ import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.View.OnClickListener;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.SeekBar;
|
import android.widget.SeekBar;
|
||||||
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.actionbarsherlock.app.SherlockActivity;
|
import com.actionbarsherlock.app.SherlockActivity;
|
||||||
|
@ -35,8 +37,6 @@ public class MediaplayerActivity extends SherlockActivity {
|
||||||
private FeedMedia media;
|
private FeedMedia media;
|
||||||
private PlayerStatus status;
|
private PlayerStatus status;
|
||||||
|
|
||||||
private boolean guiSetup;
|
|
||||||
|
|
||||||
|
|
||||||
// Widgets
|
// Widgets
|
||||||
private ImageView imgvCover;
|
private ImageView imgvCover;
|
||||||
|
@ -65,19 +65,32 @@ public class MediaplayerActivity extends SherlockActivity {
|
||||||
return super.onCreateOptionsMenu(menu);
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
Log.d(TAG, "Creating Activity");
|
||||||
this.setContentView(R.layout.mediaplayer_activity);
|
this.setContentView(R.layout.mediaplayer_activity);
|
||||||
|
|
||||||
guiSetup = false;
|
|
||||||
setupGUI();
|
setupGUI();
|
||||||
|
bindToService();
|
||||||
|
registerReceiver(statusUpdate, new IntentFilter(PlaybackService.ACTION_PLAYER_STATUS_CHANGED));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindToService() {
|
||||||
if(!bindService(new Intent(this, PlaybackService.class), mConnection, 0)) {
|
if(!bindService(new Intent(this, PlaybackService.class), mConnection, 0)) {
|
||||||
status = PlayerStatus.STOPPED;
|
status = PlayerStatus.STOPPED;
|
||||||
handleStatus();
|
handleStatus();
|
||||||
}
|
}
|
||||||
IntentFilter filter = new IntentFilter(PlaybackService.ACTION_PLAYER_STATUS_CHANGED);
|
|
||||||
registerReceiver(statusUpdate, filter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleStatus() {
|
private void handleStatus() {
|
||||||
|
@ -90,11 +103,17 @@ public class MediaplayerActivity extends SherlockActivity {
|
||||||
case PAUSED:
|
case PAUSED:
|
||||||
setStatusMsg(R.string.player_paused_msg, View.VISIBLE);
|
setStatusMsg(R.string.player_paused_msg, View.VISIBLE);
|
||||||
loadMediaInfo();
|
loadMediaInfo();
|
||||||
|
if (positionObserver != null) {
|
||||||
|
positionObserver.cancel(true);
|
||||||
|
positionObserver = null;
|
||||||
|
}
|
||||||
|
butPlay.setImageResource(android.R.drawable.ic_media_play);
|
||||||
break;
|
break;
|
||||||
case PLAYING:
|
case PLAYING:
|
||||||
setStatusMsg(0, View.INVISIBLE);
|
setStatusMsg(0, View.INVISIBLE);
|
||||||
loadMediaInfo();
|
loadMediaInfo();
|
||||||
setupPositionObserver();
|
setupPositionObserver();
|
||||||
|
butPlay.setImageResource(android.R.drawable.ic_media_pause);
|
||||||
break;
|
break;
|
||||||
case PREPARING:
|
case PREPARING:
|
||||||
setStatusMsg(R.string.player_preparing_msg, View.VISIBLE);
|
setStatusMsg(R.string.player_preparing_msg, View.VISIBLE);
|
||||||
|
@ -114,14 +133,17 @@ public class MediaplayerActivity extends SherlockActivity {
|
||||||
positionObserver = new MediaPositionObserver() {
|
positionObserver = new MediaPositionObserver() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(Long... values) {
|
protected void onProgressUpdate(Integer... values) {
|
||||||
super.onProgressUpdate(values);
|
super.onProgressUpdate(values);
|
||||||
txtvPosition.setText(
|
txtvPosition.setText(
|
||||||
Converter.getDurationStringLong(playbackService.getPlayer().getCurrentPosition()));
|
Converter.getDurationStringLong(values[0]));
|
||||||
|
|
||||||
|
float progress = ((float) values[0]) / getDuration();
|
||||||
|
sbPosition.setProgress((int) (progress * 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
positionObserver.execute(playbackService);
|
positionObserver.execute(playbackService.getPlayer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +173,47 @@ public class MediaplayerActivity extends SherlockActivity {
|
||||||
butRev = (ImageButton) findViewById(R.id.butRev);
|
butRev = (ImageButton) findViewById(R.id.butRev);
|
||||||
butFF = (ImageButton) findViewById(R.id.butFF);
|
butFF = (ImageButton) findViewById(R.id.butFF);
|
||||||
|
|
||||||
this.guiSetup = true;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -181,44 +243,43 @@ public class MediaplayerActivity extends SherlockActivity {
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
Log.d(TAG, "Received statusUpdate Intent.");
|
Log.d(TAG, "Received statusUpdate Intent.");
|
||||||
status = playbackService.getStatus();
|
status = playbackService.getStatus();
|
||||||
|
handleStatus();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Refreshes the current position of the media file that is playing. */
|
||||||
public class MediaPositionObserver extends AsyncTask<PlaybackService, Long, Long> {
|
public class MediaPositionObserver extends AsyncTask<MediaPlayer, Integer, Boolean> {
|
||||||
|
|
||||||
private static final int WAITING_INTERVALL = 1000;
|
private static final int WAITING_INTERVALL = 1000;
|
||||||
private long position;
|
private MediaPlayer player;
|
||||||
private long length;
|
private int duration;
|
||||||
private PlaybackService service;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCancelled(Long result) {
|
protected void onCancelled(Boolean result) {
|
||||||
Log.d(TAG, "Task was cancelled");
|
Log.d(TAG, "Task was cancelled");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Long doInBackground(PlaybackService... services) {
|
@Override
|
||||||
|
protected Boolean doInBackground(MediaPlayer... p) {
|
||||||
Log.d(TAG, "Background Task started");
|
Log.d(TAG, "Background Task started");
|
||||||
service = services[0];
|
player = p[0];
|
||||||
getProgress();
|
duration = player.getDuration();
|
||||||
while(!isCancelled()) {
|
|
||||||
|
while(player.isPlaying() && !isCancelled()) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(WAITING_INTERVALL);
|
Thread.sleep(WAITING_INTERVALL);
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {
|
||||||
Log.d(TAG, "Thread was interrupted while waiting");
|
Log.d(TAG, "Thread was interrupted while waiting. Finishing now");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
publishProgress(player.getCurrentPosition());
|
||||||
getProgress();
|
|
||||||
publishProgress(position);
|
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Background Task finished");
|
Log.d(TAG, "Background Task finished");
|
||||||
return Long.valueOf(position);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getProgress() {
|
public int getDuration() {
|
||||||
FeedMedia media = service.getMedia();
|
return duration;
|
||||||
position = media.getPosition();
|
|
||||||
length = media.getDuration();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,6 +96,24 @@ public class PlaybackService extends Service {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public void pause() {
|
||||||
|
if (player.isPlaying()) {
|
||||||
|
Log.d(TAG, "Pausing playback.");
|
||||||
|
player.pause();
|
||||||
|
setStatus(PlayerStatus.PAUSED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void play() {
|
||||||
|
if (status == PlayerStatus.PAUSED) {
|
||||||
|
Log.d(TAG, "Resuming playback");
|
||||||
|
player.start();
|
||||||
|
setStatus(PlayerStatus.PLAYING);
|
||||||
|
} else if (status == PlayerStatus.STOPPED) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void setStatus(PlayerStatus newStatus) {
|
private void setStatus(PlayerStatus newStatus) {
|
||||||
status = newStatus;
|
status = newStatus;
|
||||||
sendBroadcast(new Intent(ACTION_PLAYER_STATUS_CHANGED));
|
sendBroadcast(new Intent(ACTION_PLAYER_STATUS_CHANGED));
|
||||||
|
@ -132,4 +150,9 @@ public class PlaybackService extends Service {
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void seek(int i) {
|
||||||
|
Log.d(TAG, "Seeking position " + i);
|
||||||
|
player.seekTo(i);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue