use textureview for inline video player

This commit is contained in:
Andrew Dolgov 2017-06-02 14:46:01 +03:00
parent 047bb1cc37
commit 0f764c3f61
4 changed files with 70 additions and 60 deletions

View File

@ -140,7 +140,7 @@ public class ArticleVideoFragment extends GalleryBaseFragment {
private void initializeVideoPlayer(final View view) { private void initializeVideoPlayer(final View view) {
Log.d(TAG, "initializeVideoPlayer: " + m_activity + " " + view); //Log.d(TAG, "initializeVideoPlayer: " + m_activity + " " + view);
final MediaController m_mediaController = new MediaController(m_activity); final MediaController m_mediaController = new MediaController(m_activity);
final TextureView textureView = (TextureView) view.findViewById(R.id.flavor_video); final TextureView textureView = (TextureView) view.findViewById(R.id.flavor_video);
@ -148,10 +148,14 @@ public class ArticleVideoFragment extends GalleryBaseFragment {
textureView.setOnClickListener(new View.OnClickListener() { textureView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (!m_mediaController.isShowing()) try {
m_mediaController.show(5000); if (!m_mediaController.isShowing())
else m_mediaController.show(5000);
m_mediaController.hide(); else
m_mediaController.hide();
} catch (Exception e) {
e.printStackTrace();
}
} }
}); });
@ -255,7 +259,11 @@ public class ArticleVideoFragment extends GalleryBaseFragment {
@Override @Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
m_mediaPlayer.release(); try {
m_mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
}
return false; return false;
} }

View File

@ -11,12 +11,12 @@ import android.content.pm.ApplicationInfo;
import android.content.res.Resources.Theme; import android.content.res.Resources.Theme;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
@ -41,8 +41,9 @@ import android.view.Display;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import android.view.SurfaceView; import android.view.TextureView;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -76,6 +77,7 @@ import org.fox.ttrss.util.HeaderViewRecyclerAdapter;
import org.fox.ttrss.util.HeadlinesRequest; import org.fox.ttrss.util.HeadlinesRequest;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -124,7 +126,7 @@ public class HeadlinesFragment extends Fragment {
private View m_amrFooterView; private View m_amrFooterView;
private MediaPlayer m_mediaPlayer; private MediaPlayer m_mediaPlayer;
private SurfaceView m_activeSurface; private TextureView m_activeTexture;
public ArticleList getSelectedArticles() { public ArticleList getSelectedArticles() {
ArticleList tmp = new ArticleList(); ArticleList tmp = new ArticleList();
@ -764,7 +766,7 @@ public class HeadlinesFragment extends Fragment {
public View headlineHeader; public View headlineHeader;
public View topChangedMessage; public View topChangedMessage;
public View flavorImageOverflow; public View flavorImageOverflow;
public SurfaceView flavorVideoView; public TextureView flavorVideoView;
//public int position; //public int position;
public boolean flavorImageEmbedded; public boolean flavorImageEmbedded;
@ -793,7 +795,7 @@ public class HeadlinesFragment extends Fragment {
headlineHeader = v.findViewById(R.id.headline_header); headlineHeader = v.findViewById(R.id.headline_header);
topChangedMessage = v.findViewById(R.id.headlines_row_top_changed); topChangedMessage = v.findViewById(R.id.headlines_row_top_changed);
flavorImageOverflow = v.findViewById(R.id.flavor_image_overflow); flavorImageOverflow = v.findViewById(R.id.flavor_image_overflow);
flavorVideoView = (SurfaceView) v.findViewById(R.id.flavor_video); flavorVideoView = (TextureView) v.findViewById(R.id.flavor_video);
} }
public void clearAnimation() { public void clearAnimation() {
@ -1227,14 +1229,6 @@ public class HeadlinesFragment extends Fragment {
releaseSurface(); releaseSurface();
m_mediaPlayer = new MediaPlayer(); m_mediaPlayer = new MediaPlayer();
try {
m_mediaPlayer.setDataSource(article.flavorStreamUri);
} catch (IOException e) {
e.printStackTrace();
}
SurfaceHolder sh = holder.flavorVideoView.getHolder();
holder.flavorVideoView.setVisibility(View.VISIBLE); holder.flavorVideoView.setVisibility(View.VISIBLE);
final ProgressBar bar = holder.flavorImageLoadingBar; final ProgressBar bar = holder.flavorImageLoadingBar;
@ -1255,9 +1249,9 @@ public class HeadlinesFragment extends Fragment {
} }
}); });
m_activeSurface = holder.flavorVideoView; m_activeTexture = holder.flavorVideoView;
android.view.ViewGroup.LayoutParams lp = m_activeSurface.getLayoutParams(); android.view.ViewGroup.LayoutParams lp = m_activeTexture.getLayoutParams();
Drawable drawable = holder.flavorImageView.getDrawable(); Drawable drawable = holder.flavorImageView.getDrawable();
@ -1268,48 +1262,56 @@ public class HeadlinesFragment extends Fragment {
lp.height = holder.flavorImageView.getMeasuredHeight(); lp.height = holder.flavorImageView.getMeasuredHeight();
lp.width = (int) (lp.height * aspect); lp.width = (int) (lp.height * aspect);
m_activeSurface.setLayoutParams(lp); m_activeTexture.setLayoutParams(lp);
} }
sh.addCallback(new SurfaceHolder.Callback() { holder.flavorVideoView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override @Override
public void surfaceCreated(SurfaceHolder sh) { public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
m_mediaPlayer.setSurface(new Surface(surface));
try { try {
m_mediaPlayer.setDisplay(sh); m_mediaPlayer.setDataSource(article.flavorStreamUri);
m_mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
e.printStackTrace();
}
m_mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
bar.setVisibility(View.GONE); m_mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//resizeSurface(); @Override
mp.setLooping(true); public void onPrepared(MediaPlayer mp) {
mp.start();
}
}
);
}
@Override bar.setVisibility(View.GONE);
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //resizeSurface();
bar.setVisibility(View.GONE); mp.setLooping(true);
} mp.start();
}
});
@Override m_mediaPlayer.prepareAsync();
public void surfaceDestroyed(SurfaceHolder holder) { } catch (Exception e) {
bar.setVisibility(View.GONE); e.printStackTrace();
}
try { }
m_mediaPlayer.release();
} catch (IllegalStateException e) { @Override
e.printStackTrace(); public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
} }
});
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
try {
m_mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
);
} }
}); });
@ -1606,8 +1608,8 @@ public class HeadlinesFragment extends Fragment {
} }
try { try {
if (m_activeSurface != null) { if (m_activeTexture != null) {
m_activeSurface.setVisibility(View.GONE); m_activeTexture.setVisibility(View.GONE);
} }
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -57,7 +57,7 @@
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:visibility="visible" /> android:visibility="visible" />
<SurfaceView <TextureView
android:id="@+id/flavor_video" android:id="@+id/flavor_video"
android:foreground="@drawable/ripple" android:foreground="@drawable/ripple"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -58,7 +58,7 @@
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:visibility="visible" /> android:visibility="visible" />
<SurfaceView <TextureView
android:id="@+id/flavor_video" android:id="@+id/flavor_video"
android:foreground="@drawable/ripple" android:foreground="@drawable/ripple"
android:layout_width="match_parent" android:layout_width="match_parent"