Added buttons to control playback speed

This commit is contained in:
daniel oeh 2013-05-03 18:31:39 +02:00
parent e65c1b7322
commit 62d7a927ad
7 changed files with 102 additions and 12 deletions

View File

@ -92,14 +92,12 @@
android:layout_width="80dp" android:layout_width="80dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="?attr/borderless_button" android:background="?attr/borderless_button"
android:src="?attr/av_pause" /> android:src="?attr/av_pause" />
<ImageButton <ImageButton
android:id="@+id/butRev" android:id="@+id/butRev"
android:layout_width="80dp" android:layout_width="60dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_toLeftOf="@id/butPlay" android:layout_toLeftOf="@id/butPlay"
android:background="?attr/borderless_button" android:background="?attr/borderless_button"
@ -107,11 +105,22 @@
<ImageButton <ImageButton
android:id="@+id/butFF" android:id="@+id/butFF"
android:layout_width="80dp" android:layout_width="60dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_toRightOf="@id/butPlay" android:layout_toRightOf="@id/butPlay"
android:background="?attr/borderless_button" android:background="?attr/borderless_button"
android:src="?attr/av_fast_forward" /> android:src="?attr/av_fast_forward" />
<Button
android:id="@+id/butPlaybackSpeed"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_toRightOf="@id/butFF"
android:background="?attr/borderless_button"
android:src="?attr/av_fast_forward"
android:textColor="@color/gray"
android:textSize="@dimen/text_size_medium"
android:visibility="gone" />
</RelativeLayout> </RelativeLayout>
<RelativeLayout <RelativeLayout

View File

@ -79,8 +79,6 @@
android:layout_width="80dp" android:layout_width="80dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="?attr/borderless_button" android:background="?attr/borderless_button"
android:src="?attr/av_pause" /> android:src="?attr/av_pause" />
@ -99,6 +97,17 @@
android:layout_toRightOf="@id/butPlay" android:layout_toRightOf="@id/butPlay"
android:background="?attr/borderless_button" android:background="?attr/borderless_button"
android:src="?attr/av_fast_forward" /> android:src="?attr/av_fast_forward" />
<Button
android:id="@+id/butPlaybackSpeed"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_toRightOf="@id/butFF"
android:background="?attr/borderless_button"
android:src="?attr/av_fast_forward"
android:textColor="@color/gray"
android:textSize="@dimen/text_size_medium"
android:visibility="gone" />
</RelativeLayout> </RelativeLayout>
<RelativeLayout <RelativeLayout

View File

@ -11,6 +11,7 @@ import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView.ScaleType; import android.widget.ImageView.ScaleType;
import android.widget.ListView; import android.widget.ListView;
@ -57,6 +58,7 @@ public class AudioplayerActivity extends MediaplayerActivity {
private TextView txtvTitle; private TextView txtvTitle;
private TextView txtvFeed; private TextView txtvFeed;
private Button butPlaybackSpeed;
private ImageButton butNavLeft; private ImageButton butNavLeft;
private ImageButton butNavRight; private ImageButton butNavRight;
@ -219,7 +221,7 @@ public class AudioplayerActivity extends MediaplayerActivity {
if (savedPosition != -1) { if (savedPosition != -1) {
switchToFragment(savedPosition); switchToFragment(savedPosition);
} }
} }
@Override @Override
@ -365,6 +367,7 @@ public class AudioplayerActivity extends MediaplayerActivity {
txtvFeed = (TextView) findViewById(R.id.txtvFeed); txtvFeed = (TextView) findViewById(R.id.txtvFeed);
butNavLeft = (ImageButton) findViewById(R.id.butNavLeft); butNavLeft = (ImageButton) findViewById(R.id.butNavLeft);
butNavRight = (ImageButton) findViewById(R.id.butNavRight); butNavRight = (ImageButton) findViewById(R.id.butNavRight);
butPlaybackSpeed = (Button) findViewById(R.id.butPlaybackSpeed);
butNavLeft.setOnClickListener(new OnClickListener() { butNavLeft.setOnClickListener(new OnClickListener() {
@ -392,6 +395,50 @@ public class AudioplayerActivity extends MediaplayerActivity {
} }
} }
}); });
butPlaybackSpeed.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final double PLAYBACK_SPEED_STEP = 0.5;
final double PLAYBACK_SPEED_MAX = 2.0;
final double PLAYBACK_SPEED_DEFAULT = 1.0;
if (controller != null && controller.canSetPlaybackSpeed()) {
double currentPlaybackSpeed = controller
.getCurrentPlaybackSpeedMultiplier();
if (currentPlaybackSpeed != -1) {
if (currentPlaybackSpeed >= PLAYBACK_SPEED_MAX) {
controller.setPlaybackSpeed(PLAYBACK_SPEED_DEFAULT);
} else {
controller.setPlaybackSpeed(currentPlaybackSpeed
+ PLAYBACK_SPEED_STEP);
}
} else {
controller.setPlaybackSpeed(PLAYBACK_SPEED_DEFAULT);
}
}
}
});
}
@Override
protected void onPlaybackSpeedChange() {
super.onPlaybackSpeedChange();
updateButPlaybackSpeed();
}
private void updateButPlaybackSpeed() {
double playbackSpeed;
if (controller == null
|| (playbackSpeed = controller
.getCurrentPlaybackSpeedMultiplier()) == -1) {
butPlaybackSpeed.setVisibility(View.GONE);
} else {
butPlaybackSpeed.setVisibility(View.VISIBLE);
butPlaybackSpeed.setText(String.format("%.1fx", playbackSpeed));
}
} }
@Override @Override
@ -423,7 +470,7 @@ public class AudioplayerActivity extends MediaplayerActivity {
((AudioplayerContentFragment) currentlyShownFragment) ((AudioplayerContentFragment) currentlyShownFragment)
.onDataSetChanged(media); .onDataSetChanged(media);
} }
updateButPlaybackSpeed();
} }
public void notifyMediaPositionChanged() { public void notifyMediaPositionChanged() {

View File

@ -132,10 +132,19 @@ public abstract class MediaplayerActivity extends SherlockFragmentActivity
public void onPlaybackEnd() { public void onPlaybackEnd() {
finish(); finish();
} }
@Override
public void onPlaybackSpeedChange() {
MediaplayerActivity.this.onPlaybackSpeedChange();
}
}; };
} }
protected void onPlaybackSpeedChange() {
}
protected void onServiceQueried() { protected void onServiceQueried() {
supportInvalidateOptionsMenu(); supportInvalidateOptionsMenu();
} }

View File

@ -174,6 +174,12 @@ public class ExternalPlayerFragment extends SherlockFragment {
.newOnPlayButtonClickListener()); .newOnPlayButtonClickListener());
} }
} }
@Override
public void onPlaybackSpeedChange() {
// TODO Auto-generated method stub
}
}; };
} }

View File

@ -109,6 +109,7 @@ public class PlaybackService extends Service {
public static final int NOTIFICATION_TYPE_BUFFER_END = 6; public static final int NOTIFICATION_TYPE_BUFFER_END = 6;
/** No more episodes are going to be played. */ /** No more episodes are going to be played. */
public static final int NOTIFICATION_TYPE_PLAYBACK_END = 7; public static final int NOTIFICATION_TYPE_PLAYBACK_END = 7;
public static final int NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE = 8;
/** /**
* Returned by getPositionSafe() or getDurationSafe() if the playbackService * Returned by getPositionSafe() or getDurationSafe() if the playbackService
@ -1541,6 +1542,10 @@ public class PlaybackService extends Service {
AudioPlayer audioPlayer = (AudioPlayer) player; AudioPlayer audioPlayer = (AudioPlayer) player;
if (audioPlayer.canSetSpeed()) { if (audioPlayer.canSetSpeed()) {
audioPlayer.setPlaybackSpeed((float) speed); audioPlayer.setPlaybackSpeed((float) speed);
if (AppConfig.DEBUG)
Log.d(TAG, "Playback speed was set to " + speed);
sendNotificationBroadcast(
NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE, 0);
} }
} }
} }
@ -1553,9 +1558,10 @@ public class PlaybackService extends Service {
} }
} }
} }
public double getCurrentPlaybackSpeed() { public double getCurrentPlaybackSpeed() {
if (media.getMediaType() == MediaType.AUDIO && player instanceof AudioPlayer) { if (media.getMediaType() == MediaType.AUDIO
&& player instanceof AudioPlayer) {
AudioPlayer audioPlayer = (AudioPlayer) player; AudioPlayer audioPlayer = (AudioPlayer) player;
if (audioPlayer.canSetSpeed()) { if (audioPlayer.canSetSpeed()) {
return audioPlayer.getCurrentSpeedMultiplier(); return audioPlayer.getCurrentSpeedMultiplier();

View File

@ -327,6 +327,8 @@ public abstract class PlaybackController {
break; break;
case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END: case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END:
onPlaybackEnd(); onPlaybackEnd();
case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE:
onPlaybackSpeedChange();
break; break;
} }
@ -354,6 +356,8 @@ public abstract class PlaybackController {
} }
} }
}; };
public abstract void onPlaybackSpeedChange();
public abstract void onShutdownNotification(); public abstract void onShutdownNotification();
@ -657,13 +661,13 @@ public abstract class PlaybackController {
public boolean canSetPlaybackSpeed() { public boolean canSetPlaybackSpeed() {
return playbackService != null && playbackService.canSetSpeed(); return playbackService != null && playbackService.canSetSpeed();
} }
public void setPlaybackSpeed(double speed) { public void setPlaybackSpeed(double speed) {
if (playbackService != null) { if (playbackService != null) {
playbackService.setSpeed(speed); playbackService.setSpeed(speed);
} }
} }
public double getCurrentPlaybackSpeedMultiplier() { public double getCurrentPlaybackSpeedMultiplier() {
if (canSetPlaybackSpeed()) { if (canSetPlaybackSpeed()) {
return playbackService.getCurrentPlaybackSpeed(); return playbackService.getCurrentPlaybackSpeed();