Added missing files

This commit is contained in:
Görkem Güclü 2022-11-18 12:49:34 +01:00
parent 16af5b7da6
commit 158cf0c8ed
4 changed files with 1381 additions and 0 deletions

View File

@ -0,0 +1,194 @@
package com.google.android.exoplayer2.demo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.LruCache;
import android.view.View;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.source.dash.manifest.DashManifest;
import com.google.android.exoplayer2.thumbnail.ThumbnailDescription;
import com.google.android.exoplayer2.util.Log;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class DefaultThumbnailProvider implements ThumbnailProvider {
private static final String TAG_DEBUG = DefaultThumbnailProvider.class.getSimpleName();
private LruCache<String, Bitmap> bitmapCache;
private View parent;
//dummy bitmap to indicate that a download is already triggered but not finished yet
private final Bitmap dummyBitmap = Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888);
@Nullable ExoPlayer exoPlayer;
public DefaultThumbnailProvider(ExoPlayer exoPlayer, View view) {
this.exoPlayer = exoPlayer;
this.parent = view;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 4;
bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount() / 1024;
}
};
}
public Bitmap getThumbnail(long position) {
return getThumbnail(position, true);
}
private Bitmap getThumbnail(long position, boolean retrigger) {
if (exoPlayer != null) {
Object manifest = exoPlayer.getCurrentManifest();
ThumbnailDescription thumbnailDescription = null;
if (manifest instanceof DashManifest) {
DashManifest dashManifest = (DashManifest) manifest;
List<ThumbnailDescription> thumbnailDescs = dashManifest.getThumbnailDescriptions(position);
//selected thumbnail description with lowest bitrate
for (ThumbnailDescription desc : thumbnailDescs) {
if (thumbnailDescription == null || thumbnailDescription.getBitrate() > desc.getBitrate()) {
thumbnailDescription = desc;
}
}
if (bitmapNotAvailableOrDownloadNotTriggeredYet(thumbnailDescription.getUri())) {
this.initThumbnailSource(thumbnailDescription);
return null;
}
}
if (retrigger) {
//also download next and prev thumbnails to have a nicer UI user experience
getThumbnail(thumbnailDescription.getStartTimeMs() + thumbnailDescription.getDurationMs(), false);
getThumbnail(thumbnailDescription.getStartTimeMs() - thumbnailDescription.getDurationMs(), false);
}
return getThumbnailInternal(position, thumbnailDescription);
}
return null;
}
private boolean bitmapNotAvailableOrDownloadNotTriggeredYet(Uri uri) {
Bitmap tmp = bitmapCache.get(uri.toString());
if (tmp != null) return false;
return true;
}
private Bitmap getThumbnailInternal(long position, ThumbnailDescription thumbnailDescription) {
if (thumbnailDescription == null) return null;
Bitmap thumbnailSource = bitmapCache.get(thumbnailDescription.getUri().toString());
if (thumbnailSource == null || thumbnailSource.getWidth() == 1) return null;
if (position < thumbnailDescription.getStartTimeMs() || position > thumbnailDescription.getStartTimeMs() + thumbnailDescription.getDurationMs()) return null;
int count = thumbnailDescription.getColumns() * thumbnailDescription.getRows();
int durationPerImage = (int)(thumbnailDescription.getDurationMs() / count);
int imageNumberToUseWithinTile = (int)((position - thumbnailDescription.getStartTimeMs()) / durationPerImage);
//handle special case if position == duration
if (imageNumberToUseWithinTile > count-1) imageNumberToUseWithinTile = count-1;
int intRowToUse = (int)(imageNumberToUseWithinTile / thumbnailDescription.getColumns());
int intColToUse = imageNumberToUseWithinTile - intRowToUse * thumbnailDescription.getColumns();
double thumbnailWidth = (double) thumbnailDescription.getImageWidth() / thumbnailDescription.getColumns();
double thumbnailHeight = (double) thumbnailDescription.getImageHeight() / thumbnailDescription.getRows();
int cropXLeft = (int)Math.round(intColToUse * thumbnailWidth);
int cropYTop = (int)Math.round(intRowToUse * thumbnailHeight);
if (cropXLeft + thumbnailWidth <= thumbnailSource.getWidth() && cropYTop + thumbnailHeight <= thumbnailSource.getHeight()) {
return Bitmap.createBitmap(thumbnailSource
, cropXLeft, cropYTop, (int) thumbnailWidth, (int) thumbnailHeight);
}
else {
Log.d(TAG_DEBUG, "Image does not have expected (" + thumbnailDescription.getImageWidth() + "x" + thumbnailDescription.getImageHeight() + ") dimensions to crop. Source " + thumbnailDescription.getUri());
return null;
}
}
private synchronized void initThumbnailSource(ThumbnailDescription thumbnailDescription){
String path = thumbnailDescription.getUri().toString();
if (path == null) return;
if (bitmapCache.get(path) != null) return;
bitmapCache.put(path, dummyBitmap);
RetrieveThumbnailImageTask currentTask = new RetrieveThumbnailImageTask();
currentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, path);
}
class RetrieveThumbnailImageTask extends AsyncTask<String, Integer, Bitmap> {
String downloadedUrl;
RetrieveThumbnailImageTask() {
}
@Override
protected void onCancelled() {
super.onCancelled();
if (downloadedUrl != null) bitmapCache.remove(downloadedUrl);
}
protected Bitmap doInBackground(String... urls) {
downloadedUrl = urls[0];
InputStream in =null;
Bitmap thumbnailToDownload=null;
int responseCode = -1;
try{
URL url = new URL(downloadedUrl);
if (!isCancelled()) {
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
responseCode = httpURLConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK)
{
if (!isCancelled()) {
in = httpURLConnection.getInputStream();
if (!isCancelled()) {
thumbnailToDownload = BitmapFactory.decodeStream(in);
}
in.close();
}
}
}
}
catch(Exception ex){
bitmapCache.remove(downloadedUrl);
System.out.println(ex);
}
return thumbnailToDownload;
}
protected void onPostExecute(Bitmap downloadedThumbnail) {
if (downloadedThumbnail != null) {
bitmapCache.put(downloadedUrl, downloadedThumbnail);
if (parent != null) parent.invalidate();
}
else {
bitmapCache.remove(downloadedUrl);
}
}
}
}

View File

@ -0,0 +1,9 @@
package com.google.android.exoplayer2.demo;
import android.graphics.Bitmap;
public interface ThumbnailProvider {
public Bitmap getThumbnail(long position);
}

View File

@ -0,0 +1,152 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2020 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">
<!-- 0dp dimensions are used to prevent this view from influencing the size of
the parent view if it uses "wrap_content". It is expanded to occupy the
entirety of the parent in code, after the parent's size has been
determined. See: https://github.com/google/ExoPlayer/issues/8726.
-->
<View android:id="@id/exo_controls_background"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/exo_black_opacity_60"/>
<FrameLayout android:id="@id/exo_bottom_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/exo_styled_bottom_bar_height"
android:layout_marginTop="@dimen/exo_styled_bottom_bar_margin_top"
android:layout_gravity="bottom"
android:background="@color/exo_bottom_bar_background"
android:layoutDirection="ltr">
<LinearLayout android:id="@id/exo_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/exo_styled_bottom_bar_time_padding"
android:paddingEnd="@dimen/exo_styled_bottom_bar_time_padding"
android:paddingLeft="@dimen/exo_styled_bottom_bar_time_padding"
android:paddingRight="@dimen/exo_styled_bottom_bar_time_padding"
android:layout_gravity="center_vertical|start"
android:layoutDirection="ltr">
<TextView android:id="@id/exo_position"
style="@style/ExoStyledControls.TimeText.Position"/>
<TextView
style="@style/ExoStyledControls.TimeText.Separator"/>
<TextView android:id="@id/exo_duration"
style="@style/ExoStyledControls.TimeText.Duration"/>
</LinearLayout>
<LinearLayout android:id="@id/exo_basic_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layoutDirection="ltr">
<ImageButton android:id="@id/exo_vr"
style="@style/ExoStyledControls.Button.Bottom.VR"/>
<ImageButton android:id="@id/exo_shuffle"
style="@style/ExoStyledControls.Button.Bottom.Shuffle"/>
<ImageButton android:id="@id/exo_repeat_toggle"
style="@style/ExoStyledControls.Button.Bottom.RepeatToggle"/>
<ImageButton android:id="@id/exo_subtitle"
style="@style/ExoStyledControls.Button.Bottom.CC"/>
<ImageButton android:id="@id/exo_settings"
style="@style/ExoStyledControls.Button.Bottom.Settings"/>
<ImageButton android:id="@id/exo_fullscreen"
style="@style/ExoStyledControls.Button.Bottom.FullScreen"/>
<ImageButton android:id="@id/exo_overflow_show"
style="@style/ExoStyledControls.Button.Bottom.OverflowShow"/>
</LinearLayout>
<HorizontalScrollView android:id="@id/exo_extra_controls_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:visibility="invisible">
<LinearLayout android:id="@id/exo_extra_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layoutDirection="ltr">
<ImageButton android:id="@id/exo_overflow_hide"
style="@style/ExoStyledControls.Button.Bottom.OverflowHide"/>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
<com.google.android.exoplayer2.demo.DefaultThumbnailTimeBar android:id="@id/exo_progress"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="bottom"
style="@style/ExoStyledControls.TimeBar"
android:layout_marginBottom="@dimen/exo_styled_progress_margin_bottom"/>
<LinearLayout android:id="@id/exo_minimal_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="@dimen/exo_styled_minimal_controls_margin_bottom"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layoutDirection="ltr">
<ImageButton android:id="@id/exo_minimal_fullscreen"
style="@style/ExoStyledControls.Button.Bottom.FullScreen"/>
</LinearLayout>
<LinearLayout
android:id="@id/exo_center_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center"
android:padding="@dimen/exo_styled_controls_padding"
android:clipToPadding="false">
<ImageButton android:id="@id/exo_prev"
style="@style/ExoStyledControls.Button.Center.Previous"/>
<include layout="@layout/exo_styled_player_control_rewind_button" />
<ImageButton android:id="@id/exo_play_pause"
style="@style/ExoStyledControls.Button.Center.PlayPause"/>
<include layout="@layout/exo_styled_player_control_ffwd_button" />
<ImageButton android:id="@id/exo_next"
style="@style/ExoStyledControls.Button.Center.Next"/>
</LinearLayout>
</merge>