switch to surfaceview from videoview
This commit is contained in:
parent
5e5c76c7e2
commit
b30de39dd5
@ -2,23 +2,31 @@ package org.fox.ttrss;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Rect;
|
||||
import android.media.MediaPlayer;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.Display;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.MediaController;
|
||||
import android.widget.VideoView;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
public class VideoPlayerActivity extends CommonActivity {
|
||||
|
||||
private final String TAG = this.getClass().getSimpleName();
|
||||
private String m_streamUri;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private SurfaceView surfaceView;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@ -36,8 +44,8 @@ public class VideoPlayerActivity extends CommonActivity {
|
||||
if (!isPortrait())
|
||||
getSupportActionBar().hide();
|
||||
|
||||
VideoView videoView = (VideoView) findViewById(R.id.video_player);
|
||||
registerForContextMenu(videoView); // doesn't work :[
|
||||
surfaceView = (SurfaceView) findViewById(R.id.video_player);
|
||||
registerForContextMenu(surfaceView);
|
||||
|
||||
setTitle(getIntent().getStringExtra("title"));
|
||||
|
||||
@ -48,26 +56,120 @@ public class VideoPlayerActivity extends CommonActivity {
|
||||
}
|
||||
|
||||
final MediaController mediaController = new MediaController(this);
|
||||
mediaController.setAnchorView(videoView);
|
||||
videoView.setMediaController(mediaController);
|
||||
videoView.setVideoURI(Uri.parse(m_streamUri));
|
||||
|
||||
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
||||
surfaceView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onPrepared(MediaPlayer mp) {
|
||||
mp.setLooping(true);
|
||||
public void onClick(View v) {
|
||||
if (!mediaController.isShowing())
|
||||
mediaController.show(5000);
|
||||
else
|
||||
mediaController.hide();
|
||||
}
|
||||
});
|
||||
|
||||
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
||||
mediaPlayer = new MediaPlayer();
|
||||
|
||||
mediaController.setMediaPlayer(new MediaController.MediaPlayerControl() {
|
||||
@Override
|
||||
public void onCompletion(MediaPlayer mp) {
|
||||
mp.seekTo(0);
|
||||
public void start() {
|
||||
mediaPlayer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {
|
||||
mediaPlayer.pause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDuration() {
|
||||
return mediaPlayer.getDuration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCurrentPosition() {
|
||||
return mediaPlayer.getCurrentPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void seekTo(int pos) {
|
||||
mediaPlayer.seekTo(pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlaying() {
|
||||
return mediaPlayer.isPlaying();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBufferPercentage() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPause() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSeekBackward() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSeekForward() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAudioSessionId() {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
videoView.start();
|
||||
}
|
||||
|
||||
SurfaceHolder sh = surfaceView.getHolder();
|
||||
|
||||
try {
|
||||
mediaPlayer.setDataSource(this, Uri.parse(m_streamUri));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Log.d(TAG, surfaceView.getWidth() + " " + surfaceView.getHeight());
|
||||
|
||||
final FrameLayout.LayoutParams svLayoutParams = new FrameLayout.LayoutParams(surfaceView.getWidth(), surfaceView.getHeight());
|
||||
|
||||
sh.addCallback(new SurfaceHolder.Callback() {
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
mediaPlayer.setDisplay(holder);
|
||||
mediaPlayer.prepareAsync();
|
||||
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
||||
@Override
|
||||
public void onPrepared(MediaPlayer mp) {
|
||||
resizeSurface();
|
||||
mp.setLooping(true);
|
||||
mp.start();
|
||||
}
|
||||
}
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
//
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
//
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
mediaController.setAnchorView(surfaceView);
|
||||
}
|
||||
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
@ -76,6 +178,8 @@ public class VideoPlayerActivity extends CommonActivity {
|
||||
getSupportActionBar().hide();
|
||||
else
|
||||
getSupportActionBar().show();
|
||||
|
||||
resizeSurface();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -143,4 +247,29 @@ public class VideoPlayerActivity extends CommonActivity {
|
||||
}
|
||||
}
|
||||
|
||||
protected void resizeSurface() {
|
||||
// get the dimensions of the video (only valid when surfaceView is set)
|
||||
float videoWidth = mediaPlayer.getVideoWidth();
|
||||
float videoHeight = mediaPlayer.getVideoHeight();
|
||||
|
||||
Rect rectangle = new Rect();
|
||||
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
|
||||
|
||||
int actionBarHeight = isPortrait() ? getSupportActionBar().getHeight() : 0;
|
||||
|
||||
Display display = getWindowManager().getDefaultDisplay();
|
||||
float containerWidth = display.getWidth();
|
||||
float containerHeight = display.getHeight() - rectangle.top - actionBarHeight;
|
||||
|
||||
// set dimensions to surfaceView's layout params (maintaining aspect ratio)
|
||||
android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();
|
||||
lp.width = (int) containerWidth;
|
||||
lp.height = (int) ((videoHeight / videoWidth) * containerWidth);
|
||||
if(lp.height > containerHeight) {
|
||||
lp.width = (int) ((videoWidth / videoHeight) * containerHeight);
|
||||
lp.height = (int) containerHeight;
|
||||
}
|
||||
surfaceView.setLayoutParams(lp);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,19 +5,12 @@
|
||||
android:background="?android:colorBackground"
|
||||
tools:context="org.fox.ttrss.VideoPlayerActivity">
|
||||
|
||||
<!--
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
-->
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@+id/toolbar">
|
||||
|
||||
<VideoView
|
||||
<SurfaceView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="center"
|
||||
|
Loading…
Reference in New Issue
Block a user