Implemented landscape layout for videoplayer

This commit is contained in:
daniel oeh 2012-06-27 17:30:54 +02:00
parent 2ba8847610
commit 0eded316b3
4 changed files with 189 additions and 65 deletions

View File

@ -30,7 +30,7 @@
<activity android:name="de.podfetcher.activity.ItemviewActivity"/>
<activity android:name="de.podfetcher.activity.DownloadActivity"
android:label="@string/downloads_label"/>
<activity android:name="de.podfetcher.activity.MediaplayerActivity" android:launchMode="singleTask"/>
<activity android:name="de.podfetcher.activity.MediaplayerActivity" android:launchMode="singleTask" android:configChanges="orientation"/>
<service android:enabled="true" android:name="de.podfetcher.service.DownloadService" />
<service android:enabled="true" android:name="de.podfetcher.service.PlaybackService" />

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- Mediaplayer controls -->
<RelativeLayout
android:id="@+id/playercontrols"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="bottom|center"
android:background="@color/gray" >
<TextView
android:id="@+id/txtvPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:textColor="@color/white"
android:text="@string/position_default_label" />
<TextView
android:id="@+id/txtvLength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/position_default_label"
android:textColor="@color/white" />
<SeekBar
android:id="@+id/sbPosition"
android:layout_height="wrap_content"
android:layout_width="0px"
android:layout_toLeftOf="@id/txtvLength"
android:layout_toRightOf="@id/txtvPosition"
android:layout_alignParentBottom="true"
android:max="500" />
</RelativeLayout>
</FrameLayout>

View File

@ -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;
}
}

View File

@ -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) {