about summary refs log tree commit diff stats
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/GalleryVideoFragment.java
blob: 05677eecbcd47522d8052aea0d3ac2a810a37381 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package org.fox.ttrss;

import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.OptIn;
import androidx.core.view.ViewCompat;
import androidx.media3.common.MediaItem;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.Player;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;
import androidx.preference.PreferenceManager;

public class GalleryVideoFragment extends GalleryBaseFragment {
    private final String TAG = this.getClass().getSimpleName();

    String m_url;
    String m_coverUrl;
    MediaPlayer m_mediaPlayer;
    private SharedPreferences m_prefs;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());

        if (savedInstanceState != null) {
            m_url = savedInstanceState.getString("m_url");
            m_coverUrl = savedInstanceState.getString("m_coverUrl");
        }
    }

    @Override
    public void onSaveInstanceState(Bundle out) {
        super.onSaveInstanceState(out);

        out.putString("m_url", m_url);
        out.putString("m_coverUrl", m_coverUrl);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_gallery_entry, container, false);

        Log.d(TAG, "called for URL: " + m_url + " Cover: " + m_coverUrl);

        ImageView imgView = view.findViewById(R.id.flavor_image);
        ViewCompat.setTransitionName(imgView, "gallery:" + m_url);

        // ActivityCompat.startPostponedEnterTransition(m_activity);

        view.findViewById(R.id.flavor_image).setVisibility(View.GONE);

        initializeVideoPlayer(view);

        return view;
    }

    private void initializeVideoPlayer(final View view) {
        final PlayerView playerView = view.findViewById(R.id.video_view);

        ExoPlayer player = new ExoPlayer.Builder(getContext()).build();

        playerView.setPlayer(player);

        player.setMediaItem(MediaItem.fromUri(m_url));
        player.prepare();
        player.setPlayWhenReady(true);

        if (m_prefs.getBoolean("loop_videos", false))
            player.setRepeatMode(Player.REPEAT_MODE_ONE);

        player.addListener(new Player.Listener() {
            @Override
            public void onPlayerError(PlaybackException error) {
                Log.d(TAG, "onPlayerError:" + error);

                view.findViewById(R.id.flavor_image_progress).setVisibility(View.GONE);

                TextView errorView = view.findViewById(R.id.flavor_image_error);

                if (errorView != null) {
                    errorView.setText(error.getMessage());
                    errorView.setVisibility(View.VISIBLE);
                }
            }

            @UnstableApi
            @Override
            public void onIsLoadingChanged(boolean isLoading) {
                Log.d(TAG, "onIsLoadingChanged" + isLoading);

                view.findViewById(R.id.flavor_image_progress).setVisibility(View.GONE);

                playerView.hideController();
            }
        });
    }

    public void initialize(String url, String coverUrl) {
        m_url = url;
        m_coverUrl = coverUrl;
    }

}