From 0eded316b37cfd6328d528d1af2ea5e0aa716f35 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 27 Jun 2012 17:30:54 +0200 Subject: [PATCH] Implemented landscape layout for videoplayer --- AndroidManifest.xml | 2 +- res/layout-land/mediaplayer_activity.xml | 51 ++++++ .../activity/MediaplayerActivity.java | 147 ++++++++++++------ .../podfetcher/service/PlaybackService.java | 54 +++++-- 4 files changed, 189 insertions(+), 65 deletions(-) create mode 100644 res/layout-land/mediaplayer_activity.xml diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 7b83099e2..aa7bcf22a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -30,7 +30,7 @@ - + diff --git a/res/layout-land/mediaplayer_activity.xml b/res/layout-land/mediaplayer_activity.xml new file mode 100644 index 000000000..52c64d4ca --- /dev/null +++ b/res/layout-land/mediaplayer_activity.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/de/podfetcher/activity/MediaplayerActivity.java b/src/de/podfetcher/activity/MediaplayerActivity.java index 04b5b897d..fd1dce669 100644 --- a/src/de/podfetcher/activity/MediaplayerActivity.java +++ b/src/de/podfetcher/activity/MediaplayerActivity.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.SharedPreferences; +import android.content.res.Configuration; import android.graphics.PixelFormat; import android.media.MediaPlayer; import android.os.AsyncTask; @@ -38,12 +39,15 @@ import de.podfetcher.util.Converter; public class MediaplayerActivity extends SherlockActivity implements SurfaceHolder.Callback { + private final String TAG = "MediaplayerActivity"; private static final int DEFAULT_SEEK_DELTA = 30000; // Seek-Delta to use // when using FF or // Rev Buttons + private int orientation; + private PlaybackService playbackService; private MediaPositionObserver positionObserver; @@ -96,6 +100,26 @@ public class MediaplayerActivity extends SherlockActivity implements } + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + Log.d(TAG, "Configuration changed"); + orientation = newConfig.orientation; + positionObserver.cancel(true); + if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + setContentView(R.layout.mediaplayer_activity); + } else { + setContentView(R.layout.mediaplayer_activity); + } + setupGUI(); + if (playbackService != null && playbackService.isPlayingVideo()) { + playbackService.resetVideoSurface(); + if (!videoview.isShown()) { + viewswitcher.showNext(); + } + } + } + @Override protected void onPause() { super.onPause(); @@ -109,6 +133,7 @@ public class MediaplayerActivity extends SherlockActivity implements protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "Creating Activity"); + orientation = getResources().getConfiguration().orientation; getWindow().setFormat(PixelFormat.TRANSPARENT); this.setContentView(R.layout.mediaplayer_activity); manager = FeedManager.getInstance(); @@ -163,13 +188,17 @@ public class MediaplayerActivity extends SherlockActivity implements positionObserver.cancel(true); positionObserver = null; } - butPlay.setImageResource(android.R.drawable.ic_media_play); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + butPlay.setImageResource(android.R.drawable.ic_media_play); + } break; case PLAYING: setStatusMsg(R.string.player_playing_msg, View.INVISIBLE); loadMediaInfo(); setupPositionObserver(); - butPlay.setImageResource(android.R.drawable.ic_media_pause); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + butPlay.setImageResource(android.R.drawable.ic_media_pause); + } break; case PREPARING: setStatusMsg(R.string.player_preparing_msg, View.VISIBLE); @@ -181,7 +210,9 @@ public class MediaplayerActivity extends SherlockActivity implements case PREPARED: loadMediaInfo(); setStatusMsg(R.string.player_ready_msg, View.VISIBLE); - butPlay.setImageResource(android.R.drawable.ic_media_play); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + butPlay.setImageResource(android.R.drawable.ic_media_play); + } break; case SEEKING: setStatusMsg(R.string.player_seeking_msg, View.VISIBLE); @@ -189,16 +220,23 @@ public class MediaplayerActivity extends SherlockActivity implements case AWAITING_VIDEO_SURFACE: Log.d(TAG, "Preparing video playback"); SurfaceHolder holder = videoview.getHolder(); + playbackService.setVideoSurface(holder); holder.addCallback(this); - viewswitcher.showNext(); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + if (!videoview.isShown()) { + viewswitcher.showNext(); + } + } } } private void setStatusMsg(int resId, int visibility) { - if (visibility == View.VISIBLE) { - txtvStatus.setText(resId); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + if (visibility == View.VISIBLE) { + txtvStatus.setText(resId); + } + txtvStatus.setVisibility(visibility); } - txtvStatus.setVisibility(visibility); } private void setupPositionObserver() { @@ -221,6 +259,7 @@ public class MediaplayerActivity extends SherlockActivity implements } private void updateProgressbarPosition() { + Log.d(TAG, "Updating progressbar info"); MediaPlayer player = playbackService.getPlayer(); float progress = ((float) player.getCurrentPosition()) / player.getDuration(); @@ -228,15 +267,17 @@ public class MediaplayerActivity extends SherlockActivity implements } private void loadMediaInfo() { + Log.d(TAG, "Loading media info"); if (media != null) { MediaPlayer player = playbackService.getPlayer(); - getSupportActionBar().setSubtitle(media.getItem().getTitle()); - getSupportActionBar() - .setTitle(media.getItem().getFeed().getTitle()); - - imgvCover.setImageBitmap(media.getItem().getFeed().getImage() - .getImageBitmap()); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + getSupportActionBar().setSubtitle(media.getItem().getTitle()); + getSupportActionBar().setTitle( + media.getItem().getFeed().getTitle()); + imgvCover.setImageBitmap(media.getItem().getFeed().getImage() + .getImageBitmap()); + } txtvPosition.setText(Converter.getDurationStringLong((player .getCurrentPosition()))); @@ -252,15 +293,10 @@ public class MediaplayerActivity extends SherlockActivity implements private void setupGUI() { viewswitcher = (ViewSwitcher) findViewById(R.id.viewswitcher); - imgvCover = (ImageView) findViewById(R.id.imgvCover); + sbPosition = (SeekBar) findViewById(R.id.sbPosition); videoview = (VideoView) findViewById(R.id.videoview); 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; @@ -294,37 +330,50 @@ public class MediaplayerActivity extends SherlockActivity implements } }); - butPlay.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (status == PlayerStatus.PLAYING) { - playbackService.pause(); - } else if (status == PlayerStatus.PAUSED - || status == PlayerStatus.PREPARED) { - playbackService.play(); - } - } - }); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + imgvCover = (ImageView) findViewById(R.id.imgvCover); + txtvStatus = (TextView) findViewById(R.id.txtvStatus); + butPlay = (ImageButton) findViewById(R.id.butPlay); + butRev = (ImageButton) findViewById(R.id.butRev); + butFF = (ImageButton) findViewById(R.id.butFF); - butFF.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (status == PlayerStatus.PLAYING) { - playbackService.seekDelta(DEFAULT_SEEK_DELTA); - } - } - }); + butPlay.setOnClickListener(playbuttonListener); - butRev.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (status == PlayerStatus.PLAYING) { - playbackService.seekDelta(-DEFAULT_SEEK_DELTA); + butFF.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + if (status == PlayerStatus.PLAYING) { + playbackService.seekDelta(DEFAULT_SEEK_DELTA); + } } - } - }); + }); + + butRev.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + if (status == PlayerStatus.PLAYING) { + playbackService.seekDelta(-DEFAULT_SEEK_DELTA); + } + } + }); + } else { + setTheme(R.style.Theme_Sherlock_Light_NoActionBar); + videoview.setOnClickListener(playbuttonListener); + } } + private OnClickListener playbuttonListener = new OnClickListener() { + @Override + public void onClick(View v) { + if (status == PlayerStatus.PLAYING) { + playbackService.pause(); + } else if (status == PlayerStatus.PAUSED + || status == PlayerStatus.PREPARED) { + playbackService.play(); + } + } + }; + private void handleError() { // TODO implement } @@ -381,6 +430,7 @@ public class MediaplayerActivity extends SherlockActivity implements } catch (InterruptedException e) { Log.d(TAG, "Thread was interrupted while waiting. Finishing now"); + return null; } publishProgress(); } @@ -389,14 +439,17 @@ public class MediaplayerActivity extends SherlockActivity implements } } + private boolean holderCreated; + @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { - + holder.setFixedSize(width, height); } @Override public void surfaceCreated(SurfaceHolder holder) { + holderCreated = true; Log.d(TAG, "Videoview holder created"); if (status == PlayerStatus.AWAITING_VIDEO_SURFACE) { playbackService.setVideoSurface(holder); @@ -406,7 +459,7 @@ public class MediaplayerActivity extends SherlockActivity implements @Override public void surfaceDestroyed(SurfaceHolder holder) { - + holderCreated = false; } } diff --git a/src/de/podfetcher/service/PlaybackService.java b/src/de/podfetcher/service/PlaybackService.java index 026ada192..8faf46345 100644 --- a/src/de/podfetcher/service/PlaybackService.java +++ b/src/de/podfetcher/service/PlaybackService.java @@ -188,28 +188,47 @@ public class PlaybackService extends Service { * mediaplayer. */ public void setVideoSurface(SurfaceHolder sh) { + Log.d(TAG, "Setting display"); + player.setDisplay(null); player.setDisplay(sh); - try { - if (shouldStream) { - player.setDataSource(media.getDownload_url()); - setStatus(PlayerStatus.PREPARING); - player.prepareAsync(); - } else { - player.setDataSource(media.getFile_url()); - setStatus(PlayerStatus.PREPARING); - player.prepare(); + if (status == PlayerStatus.STOPPED + || status == PlayerStatus.AWAITING_VIDEO_SURFACE) { + try { + if (shouldStream) { + player.setDataSource(media.getDownload_url()); + setStatus(PlayerStatus.PREPARING); + player.prepareAsync(); + } else { + player.setDataSource(media.getFile_url()); + setStatus(PlayerStatus.PREPARING); + player.prepare(); + } + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalStateException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (IllegalStateException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); } } + + /** Called when the surface holder of the mediaplayer has to be changed. */ + public void resetVideoSurface() { + positionSaver.cancel(true); + player.setDisplay(null); + player.reset(); + player.release(); + player = new MediaPlayer(); + player.setOnPreparedListener(preparedListener); + player.setOnCompletionListener(completionListener); + player.setOnSeekCompleteListener(onSeekCompleteListener); + status = PlayerStatus.STOPPED; + setupMediaplayer(); + } /** Called after service has extracted the media it is supposed to play. */ private void setupMediaplayer() { @@ -228,6 +247,7 @@ public class PlaybackService extends Service { } else if (media.getMime_type().startsWith("video")) { playingVideo = true; setStatus(PlayerStatus.AWAITING_VIDEO_SURFACE); + player.setScreenOnWhilePlaying(true); } } catch (IllegalArgumentException e) {