Exoplayer fullscreen

This commit is contained in:
stom79 2019-01-06 13:20:06 +01:00
parent 949e8cbc5a
commit 8111b85efb
5 changed files with 307 additions and 23 deletions

View File

@ -17,6 +17,8 @@ package fr.gouv.etalab.mastodon.activities;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -32,12 +34,13 @@ import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@ -45,7 +48,9 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
@ -58,12 +63,20 @@ import com.github.se_bastiaan.torrentstream.Torrent;
import com.github.se_bastiaan.torrentstream.TorrentOptions;
import com.github.se_bastiaan.torrentstream.TorrentStream;
import com.github.se_bastiaan.torrentstream.listeners.TorrentListener;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.ui.PlaybackControlView;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.ui.TrackSelectionView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
@ -92,7 +105,6 @@ import fr.gouv.etalab.mastodon.client.TLSSocketFactory;
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.helper.FullScreenMediaController;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.helper.NotificationPanel;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeInterface;
import fr.gouv.etalab.mastodon.sqlite.PeertubeFavoritesDAO;
@ -108,7 +120,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.manageDownloads;
* Peertube activity
*/
public class PeertubeActivity extends BaseActivity implements OnRetrievePeertubeInterface, OnPostActionInterface {
public class PeertubeActivity extends BaseActivity implements View.OnClickListener, OnRetrievePeertubeInterface, OnPostActionInterface {
private String peertubeInstance, videoId;
private FullScreenMediaController.fullscreen fullscreen;
@ -120,12 +132,15 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
private Peertube peertube;
private TextView toolbar_title;
public static String video_id;
private NotificationPanel nPanel;
private TorrentStream torrentStream;
private TorrentListener torrentListener;
private PlayerView playerView;
private SimpleExoPlayerView playerView;
private SimpleExoPlayer player;
private boolean fullScreenMode;
private Dialog fullScreenDialog;
private AppCompatImageView fullScreenIcon;
private LinearLayout debugRootView;
private DefaultTrackSelector trackSelector;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -146,7 +161,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
default:
setTheme(R.style.AppThemeDark);
}
fullScreenMode = false;
setContentView(R.layout.activity_peertube);
loader = findViewById(R.id.loader);
peertube_view_count = findViewById(R.id.peertube_view_count);
@ -160,14 +175,15 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
peertube_information_container = findViewById(R.id.peertube_information_container);
debugRootView = findViewById(R.id.controls_root);
playerView = new PlayerView(this);
playerView = findViewById(R.id.media_video);
playerView.setControllerShowTimeoutMs(1000);
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
initFullscreenDialog();
initFullscreenButton();
loader.setVisibility(View.VISIBLE);
Bundle b = getIntent().getExtras();
if(b != null) {
@ -400,9 +416,23 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
.removeFilesAfterStop(true)
.build();
player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this);
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),
Util.getUserAgent(getApplicationContext(), "Mastalab"), null);
ExtractorMediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getTorrentUrl(null)));
player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this, trackSelector);
playerView.setPlayer(player);
torrentStream = TorrentStream.init(torrentOptions);
player.prepare(videoSource);
torrentStream.startStream(apiResponse.getPeertubes().get(0).getTorrentUrl(null));
torrentListener = new TorrentListener() {
@Override
@ -425,7 +455,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
Log.v(Helper.TAG,"onStreamReady");
loader.setVisibility(View.GONE);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),
Util.getUserAgent(getApplicationContext(), "Mastalab"), null);
Util.getUserAgent(getApplicationContext(), "Mastalab"), bandwidthMeter);
ExtractorMediaSource extractorMediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.fromFile(torrent.getVideoFile()));
@ -521,17 +551,19 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
openFullscreenDialog();
setFullscreen(FullScreenMediaController.fullscreen.ON);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
closeFullscreenDialog();
setFullscreen(FullScreenMediaController.fullscreen.OFF);
}
change();
fullScreenMediaController.changeIcon();
}
@Override
@ -565,8 +597,8 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
}
@Override
public void onStop() {
super.onStop();
public void onDestroy() {
super.onDestroy();
if( torrentStream != null && torrentStream.isStreaming())
torrentStream.stopStream();
}
@ -574,16 +606,18 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
@Override
protected void onPause() {
super.onPause();
player.release();
nPanel = new NotificationPanel(PeertubeActivity.this);
if( player != null) {
player.setPlayWhenReady(false);
}
}
@Override
public void onResume(){
super.onResume();
if( nPanel != null)
nPanel.notificationCancel();
if( player != null) {
player.setPlayWhenReady(true);
}
}
public void displayResolution(){
@ -666,6 +700,76 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube
}
@Override
public void onClick(View view) {
Log.v(Helper.TAG,"view.getParent(): " + view.getParent());
Log.v(Helper.TAG,"debugRootView: " + debugRootView);
if (view.getParent() == debugRootView) {
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
CharSequence title = ((Button) view).getText();
int rendererIndex = (int) view.getTag();
int rendererType = mappedTrackInfo.getRendererType(rendererIndex);
boolean allowAdaptiveSelections =
rendererType == C.TRACK_TYPE_VIDEO
|| (rendererType == C.TRACK_TYPE_AUDIO
&& mappedTrackInfo.getTypeSupport(C.TRACK_TYPE_VIDEO)
== MappingTrackSelector.MappedTrackInfo.RENDERER_SUPPORT_NO_TRACKS);
Pair<AlertDialog, TrackSelectionView> dialogPair =
TrackSelectionView.getDialog(this, title, trackSelector, rendererIndex);
dialogPair.second.setShowDisableOption(true);
dialogPair.second.setAllowAdaptiveSelections(allowAdaptiveSelections);
dialogPair.first.show();
}
}
}
private void initFullscreenDialog() {
fullScreenDialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
public void onBackPressed() {
if (fullScreenMode)
closeFullscreenDialog();
super.onBackPressed();
}
};
}
private void openFullscreenDialog() {
((ViewGroup) playerView.getParent()).removeView(playerView);
fullScreenDialog.addContentView(playerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
fullScreenIcon.setImageDrawable(ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_fullscreen_exit));
fullScreenMode = true;
fullScreenDialog.show();
}
private void closeFullscreenDialog() {
((ViewGroup) playerView.getParent()).removeView(playerView);
((FrameLayout) findViewById(R.id.main_media_frame)).addView(playerView);
fullScreenMode = false;
fullScreenDialog.dismiss();
fullScreenIcon.setImageDrawable(ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_fullscreen));
}
private void initFullscreenButton() {
PlaybackControlView controlView = playerView.findViewById(R.id.exo_controller);
fullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon);
View fullScreenButton = controlView.findViewById(R.id.exo_fullscreen_button);
fullScreenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!fullScreenMode)
openFullscreenDialog();
else
closeFullscreenDialog();
}
});
}
private void changeColor(){
if( peertube.getMyRating() != null && peertube.getMyRating().equals("like")){
changeDrawableColor(getApplicationContext(), R.drawable.ic_thumb_up_peertube,R.color.positive_thumbs);

View File

@ -32,16 +32,25 @@
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/media_video"
android:layout_centerInParent="true"
<FrameLayout
android:id="@+id/main_media_frame"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
app:layout_constraintBottom_toBottomOf="parent"
android:background="#000000">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/media_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</FrameLayout>
<!-- Main Loader -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loader"

View File

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layoutDirection="ltr"
android:background="#CC000000"
android:orientation="vertical"
tools:targetApi="28">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="4dp"
android:orientation="horizontal">
<ImageButton android:id="@id/exo_prev"
style="@style/ExoMediaButton.Previous"/>
<ImageButton android:id="@id/exo_rew"
style="@style/ExoMediaButton.Rewind"/>
<ImageButton android:id="@id/exo_shuffle"
style="@style/ExoMediaButton.Shuffle"/>
<ImageButton android:id="@id/exo_repeat_toggle"
style="@style/ExoMediaButton"/>
<ImageButton android:id="@id/exo_play"
style="@style/ExoMediaButton.Play"/>
<ImageButton android:id="@id/exo_pause"
style="@style/ExoMediaButton.Pause"/>
<ImageButton android:id="@id/exo_ffwd"
style="@style/ExoMediaButton.FastForward"/>
<ImageButton android:id="@id/exo_next"
style="@style/ExoMediaButton.Next"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView android:id="@id/exo_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:includeFontPadding="false"
android:textColor="#FFBEBEBE"/>
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="@id/exo_progress"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="26dp"/>
<TextView android:id="@id/exo_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:includeFontPadding="false"
android:textColor="#FFBEBEBE"/>
<FrameLayout android:id="@+id/exo_fullscreen_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="end"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/exo_fullscreen_icon"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:contentDescription="@string/fullscreen"
android:src="@drawable/ic_fullscreen"/>
</FrameLayout>
</LinearLayout>
</LinearLayout>

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout android:id="@id/exo_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<!-- Video surface will be inserted as the first child of the content frame. -->
<View android:id="@id/exo_shutter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>
<ImageView android:id="@id/exo_artwork"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<com.google.android.exoplayer2.ui.SubtitleView android:id="@id/exo_subtitles"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar android:id="@id/exo_buffering"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_gravity="center"/>
<TextView android:id="@id/exo_error_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/exo_error_message_background_color"
android:padding="16dp"/>
</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
<FrameLayout android:id="@id/exo_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.exoplayer2.ui.PlaybackControlView
android:id="@id/exo_controller"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View android:id="@id/exo_controller_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</merge>

View File

@ -796,6 +796,7 @@
<string name="reply">Reply</string>
<string name="delete_comment">Delete a comment</string>
<string name="delete_comment_confirm">Are you sure to delete this comment?</string>
<string name="fullscreen">Full screen video</string>
<!-- end languages -->
</resources>