Optimize video wall

release
jhengazuki 6 months ago
parent 98a83356d1
commit 2246af2a03
  1. 54
      app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWallView.java
  2. 12
      app/src/main/res/layout/view_wall.xml
  3. 10
      app/src/main/res/layout/view_wall_video.xml

@ -13,7 +13,9 @@ import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.media3.common.MediaItem;
import androidx.media3.common.Player;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
@ -35,6 +37,7 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
private ViewWallBinding binding;
private GifDrawable drawable;
private PlayerView video;
private ExoPlayer player;
private Drawable cache;
@ -57,7 +60,7 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
private void createPlayer() {
player = new ExoPlayer.Builder(getContext()).build();
player.setRepeatMode(ExoPlayer.REPEAT_MODE_ALL);
player.setRepeatMode(Player.REPEAT_MODE_ALL);
player.setPlayWhenReady(true);
player.setVolume(0);
}
@ -67,7 +70,23 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
if (event.getType() == RefreshEvent.Type.WALL) refresh();
}
private void stopAll() {
if (player.isPlaying()) {
player.stop();
player.clearMediaItems();
}
if (video != null) {
video.setPlayer(null);
video.setVisibility(GONE);
}
if (drawable != null) {
drawable.stop();
drawable.recycle();
}
}
private void refresh() {
stopAll();
cache = Drawable.createFromPath(FileUtil.getWallCache().getAbsolutePath());
load(FileUtil.getWall(Setting.getWall()));
}
@ -80,33 +99,24 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
}
private void loadRes(int resId) {
player.clearMediaItems();
binding.video.setPlayer(null);
binding.video.setVisibility(GONE);
binding.image.setImageResource(resId);
}
private void loadVideo(File file) {
binding.video.setPlayer(player);
binding.video.setVisibility(VISIBLE);
ensureVideoView();
video.setPlayer(player);
video.setVisibility(VISIBLE);
binding.image.setImageDrawable(cache);
player.setMediaItem(MediaItem.fromUri(Uri.fromFile(file)));
player.prepare();
}
private void loadGif(File file) {
player.clearMediaItems();
binding.video.setPlayer(null);
binding.video.setVisibility(GONE);
binding.image.setImageDrawable(cache);
if (drawable != null) drawable.recycle();
binding.image.setImageDrawable(drawable = gif(file));
}
private void loadImage() {
player.clearMediaItems();
binding.video.setPlayer(null);
binding.video.setVisibility(GONE);
if (cache != null) binding.image.setImageDrawable(cache);
else binding.image.setImageResource(R.drawable.wallpaper_1);
}
@ -119,6 +129,13 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
}
}
private void ensureVideoView() {
if (video != null) return;
LayoutInflater inflater = LayoutInflater.from(getContext());
video = (PlayerView) inflater.inflate(R.layout.view_wall_video, this, false);
addView(video, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
EventBus.getDefault().register(this);
@ -127,28 +144,25 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
@Override
public void onResume(@NonNull LifecycleOwner owner) {
if (drawable != null) drawable.start();
if (binding.video.getVisibility() != VISIBLE || player == null || player.getMediaItemCount() == 0) return;
binding.video.setPlayer(player);
player.play();
if (video != null && video.getVisibility() == VISIBLE && !player.isPlaying()) player.play();
}
@Override
public void onPause(@NonNull LifecycleOwner owner) {
if (drawable != null) drawable.pause();
if (binding.video.getVisibility() != VISIBLE || player == null || player.getMediaItemCount() == 0) return;
binding.video.setPlayer(null);
player.pause();
if (player.isPlaying()) player.pause();
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
EventBus.getDefault().unregister(this);
if (drawable != null) drawable.recycle();
binding.video.setPlayer(null);
if (video != null) removeView(video);
player.release();
drawable = null;
binding = null;
player = null;
cache = null;
video = null;
}
}

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -10,15 +9,4 @@
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<androidx.media3.ui.PlayerView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:keep_content_on_player_reset="false"
app:resize_mode="zoom"
app:shutter_background_color="@color/transparent"
app:surface_type="texture_view"
app:use_controller="false" />
</FrameLayout>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.media3.ui.PlayerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:keep_content_on_player_reset="false"
app:resize_mode="zoom"
app:shutter_background_color="@color/transparent"
app:surface_type="texture_view"
app:use_controller="false" />
Loading…
Cancel
Save