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.Intent;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
|
import android.graphics.Rect;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.ContextMenu;
|
import android.view.ContextMenu;
|
||||||
|
import android.view.Display;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
import android.view.SurfaceView;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
import android.widget.MediaController;
|
import android.widget.MediaController;
|
||||||
import android.widget.VideoView;
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
public class VideoPlayerActivity extends CommonActivity {
|
public class VideoPlayerActivity extends CommonActivity {
|
||||||
|
|
||||||
private final String TAG = this.getClass().getSimpleName();
|
private final String TAG = this.getClass().getSimpleName();
|
||||||
private String m_streamUri;
|
private String m_streamUri;
|
||||||
|
private MediaPlayer mediaPlayer;
|
||||||
|
private SurfaceView surfaceView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
@ -36,8 +44,8 @@ public class VideoPlayerActivity extends CommonActivity {
|
|||||||
if (!isPortrait())
|
if (!isPortrait())
|
||||||
getSupportActionBar().hide();
|
getSupportActionBar().hide();
|
||||||
|
|
||||||
VideoView videoView = (VideoView) findViewById(R.id.video_player);
|
surfaceView = (SurfaceView) findViewById(R.id.video_player);
|
||||||
registerForContextMenu(videoView); // doesn't work :[
|
registerForContextMenu(surfaceView);
|
||||||
|
|
||||||
setTitle(getIntent().getStringExtra("title"));
|
setTitle(getIntent().getStringExtra("title"));
|
||||||
|
|
||||||
@ -48,26 +56,120 @@ public class VideoPlayerActivity extends CommonActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final MediaController mediaController = new MediaController(this);
|
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
|
@Override
|
||||||
public void onPrepared(MediaPlayer mp) {
|
public void onClick(View v) {
|
||||||
mp.setLooping(true);
|
if (!mediaController.isShowing())
|
||||||
|
mediaController.show(5000);
|
||||||
|
else
|
||||||
|
mediaController.hide();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
mediaPlayer = new MediaPlayer();
|
||||||
|
|
||||||
|
mediaController.setMediaPlayer(new MediaController.MediaPlayerControl() {
|
||||||
@Override
|
@Override
|
||||||
public void onCompletion(MediaPlayer mp) {
|
public void start() {
|
||||||
mp.seekTo(0);
|
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) {
|
public void onConfigurationChanged(Configuration newConfig) {
|
||||||
super.onConfigurationChanged(newConfig);
|
super.onConfigurationChanged(newConfig);
|
||||||
@ -76,6 +178,8 @@ public class VideoPlayerActivity extends CommonActivity {
|
|||||||
getSupportActionBar().hide();
|
getSupportActionBar().hide();
|
||||||
else
|
else
|
||||||
getSupportActionBar().show();
|
getSupportActionBar().show();
|
||||||
|
|
||||||
|
resizeSurface();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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"
|
android:background="?android:colorBackground"
|
||||||
tools:context="org.fox.ttrss.VideoPlayerActivity">
|
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
|
<FrameLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_below="@+id/toolbar">
|
android:layout_below="@+id/toolbar">
|
||||||
|
|
||||||
<VideoView
|
<SurfaceView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_gravity="center"
|
android:layout_gravity="center"
|
||||||
|
Loading…
Reference in New Issue
Block a user