Implemented PlaybackController in External Player

This commit is contained in:
daniel oeh 2012-09-06 15:30:00 +02:00
parent e2e26d9f48
commit 8a6413c6f8
2 changed files with 100 additions and 163 deletions

View File

@ -3,7 +3,8 @@
android:id="@+id/fragmentLayout" android:id="@+id/fragmentLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" > android:orientation="vertical"
android:visibility="gone" >
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -1,13 +1,6 @@
package de.danoeh.antennapod.fragment; package de.danoeh.antennapod.fragment;
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.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -23,10 +16,9 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.receiver.PlayerWidget;
import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.service.PlayerStatus;
import de.danoeh.antennapod.util.Converter; import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.util.PlaybackController;
/** /**
* Fragment which is supposed to be displayed outside of the MediaplayerActivity * Fragment which is supposed to be displayed outside of the MediaplayerActivity
@ -42,10 +34,7 @@ public class ExternalPlayerFragment extends SherlockFragment {
private TextView txtvPosition; private TextView txtvPosition;
private ImageButton butPlay; private ImageButton butPlay;
private PlaybackService playbackService; private PlaybackController controller;
private BroadcastReceiver playbackServiceNotificationReceiver;
private boolean mediaInfoLoaded = false;
public ExternalPlayerFragment() { public ExternalPlayerFragment() {
super(); super();
@ -70,138 +59,126 @@ public class ExternalPlayerFragment extends SherlockFragment {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "layoutInfo was clicked"); Log.d(TAG, "layoutInfo was clicked");
if (playbackService != null if (controller.getMedia() != null) {
&& playbackService.getMedia() != null) {
startActivity(PlaybackService.getPlayerActivityIntent( startActivity(PlaybackService.getPlayerActivityIntent(
getActivity(), playbackService.getMedia())); getActivity(), controller.getMedia()));
}
}
});
butPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (AppConfig.DEBUG)
Log.d(TAG, "butPlay was clicked");
if (playbackService != null) {
PlayerStatus status = playbackService.getStatus();
if (status == PlayerStatus.PLAYING) {
playbackService.pause(true);
} else if (status == PlayerStatus.PAUSED) {
playbackService.play();
}
} }
} }
}); });
return root; return root;
} }
private void setupReceiver() {
playbackServiceNotificationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null) {
if (intent.getAction().equals(
PlaybackService.ACTION_PLAYER_STATUS_CHANGED)) {
refreshFragmentState();
} else if (intent.getAction().equals(
PlayerWidget.FORCE_WIDGET_UPDATE)) {
refreshFragmentState();
} else if (intent.getAction().equals(
PlaybackService.ACTION_PLAYER_NOTIFICATION)) {
int type = intent.getIntExtra(
PlaybackService.EXTRA_NOTIFICATION_TYPE, -1);
if (type == PlaybackService.NOTIFICATION_TYPE_RELOAD) {
mediaInfoLoaded = false;
refreshFragmentState();
}
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(PlaybackService.ACTION_PLAYER_STATUS_CHANGED);
filter.addAction(PlayerWidget.FORCE_WIDGET_UPDATE);
filter.addAction(PlaybackService.ACTION_PLAYER_NOTIFICATION);
getActivity().registerReceiver(playbackServiceNotificationReceiver,
filter);
}
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
setupReceiver(); controller = new PlaybackController(getActivity()) {
refreshFragmentState();
@Override
public void setupGUI() {
}
@Override
public void onPositionObserverUpdate() {
int duration = controller.getDuration();
int position = controller.getPosition();
if (duration != PlaybackController.INVALID_TIME
&& position != PlaybackController.INVALID_TIME) {
txtvPosition.setText(getPositionString(position, duration));
}
}
@Override
public void onReloadNotification(int code) {
}
@Override
public void onBufferStart() {
// TODO Auto-generated method stub
}
@Override
public void onBufferEnd() {
// TODO Auto-generated method stub
}
@Override
public void onBufferUpdate(float progress) {
}
@Override
public void onSleepTimerUpdate() {
}
@Override
public void handleError(int code) {
}
@Override
public ImageButton getPlayButton() {
return butPlay;
}
@Override
public void postStatusMsg(int msg) {
}
@Override
public void clearStatusMsg() {
}
@Override
public void loadMediaInfo() {
ExternalPlayerFragment.this.loadMediaInfo();
}
@Override
public void onAwaitingVideoSurface() {
}
@Override
public void onServiceQueried() {
}
};
butPlay.setOnClickListener(controller.newOnPlayButtonClickListener());
controller.init();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
getActivity().unbindService(mConnection);
try {
getActivity().unregisterReceiver(
playbackServiceNotificationReceiver);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Fragment is about to be destroyed"); Log.d(TAG, "Fragment is about to be destroyed");
if (controller != null) {
controller.release();
}
} }
private void refreshPlayButtonAppearance() { @Override
if (AppConfig.DEBUG) public void onPause() {
Log.d(TAG, "Refreshing playbutton appearance"); super.onPause();
if (playbackService != null) { if (controller != null) {
if (!PlaybackService.isPlayingVideo()) { controller.pause();
PlayerStatus status = playbackService.getStatus();
switch (status) {
case PLAYING:
butPlay.setImageResource(R.drawable.av_pause);
butPlay.setVisibility(View.VISIBLE);
break;
case PAUSED:
butPlay.setImageResource(R.drawable.av_play);
butPlay.setVisibility(View.VISIBLE);
break;
default:
butPlay.setVisibility(View.GONE);
}
} else {
butPlay.setVisibility(View.GONE);
}
} else {
Log.w(TAG,
"refreshPlayButtonAppearance was called while playbackService was null!");
} }
} }
private void loadMediaInfo() { private void loadMediaInfo() {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Loading media info"); Log.d(TAG, "Loading media info");
if (playbackService != null) { if (controller.serviceAvailable()) {
FeedMedia media = playbackService.getMedia(); FeedMedia media = controller.getMedia();
if (media != null) { if (media != null) {
if (!mediaInfoLoaded) { txtvTitle.setText(media.getItem().getTitle());
txtvTitle.setText(media.getItem().getTitle()); FeedImageLoader.getInstance().loadThumbnailBitmap(
FeedImageLoader.getInstance().loadThumbnailBitmap( media.getItem().getFeed().getImage(),
media.getItem().getFeed().getImage(), imgvCover); imgvCover,
mediaInfoLoaded = true; (int) getActivity().getResources().getDimension(
} R.dimen.external_player_height));
PlayerStatus status = playbackService.getStatus();
refreshPlayButtonAppearance();
if (status == PlayerStatus.PLAYING
|| status == PlayerStatus.PAUSED) {
txtvPosition.setText(Converter
.getDurationStringLong(playbackService.getPlayer()
.getCurrentPosition())
+ " / "
+ Converter.getDurationStringLong(playbackService
.getPlayer().getDuration()));
}
txtvPosition.setText(getPositionString(media.getPosition(),
media.getDuration()));
fragmentLayout.setVisibility(View.VISIBLE);
} else { } else {
Log.w(TAG, Log.w(TAG,
"loadMediaInfo was called while the media object of playbackService was null!"); "loadMediaInfo was called while the media object of playbackService was null!");
@ -212,49 +189,8 @@ public class ExternalPlayerFragment extends SherlockFragment {
} }
} }
/** private String getPositionString(int position, int duration) {
* Creates a connection to the playbackService if necessary and refreshes return Converter.getDurationStringLong(position) + " / "
* the fragment's state. + Converter.getDurationStringLong(duration);
*/
private void refreshFragmentState() {
if (AppConfig.DEBUG)
Log.d(TAG, "Refreshing fragment state");
if (playbackService == null) {
fragmentLayout.setVisibility(View.GONE);
if (PlaybackService.isRunning) {
getActivity().bindService(
new Intent(getActivity(), PlaybackService.class),
mConnection, 0);
}
} else {
PlayerStatus status = playbackService.getStatus();
if ((status == PlayerStatus.PAUSED || status == PlayerStatus.PLAYING)) {
if (fragmentLayout.getVisibility() != View.VISIBLE) {
fragmentLayout.setVisibility(View.VISIBLE);
}
loadMediaInfo();
} else if (fragmentLayout.getVisibility() != View.GONE) {
fragmentLayout.setVisibility(View.GONE);
}
}
} }
protected ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
playbackService = ((PlaybackService.LocalBinder) service)
.getService();
refreshFragmentState();
if (AppConfig.DEBUG)
Log.d(TAG, "Connection to Service established");
}
@Override
public void onServiceDisconnected(ComponentName name) {
playbackService = null;
if (AppConfig.DEBUG)
Log.d(TAG, "Disconnected from Service");
}
};
} }