SHARE
TWEET

Source Code

a guest Dec 5th, 2019 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.recyclerview.widget.LinearLayoutManager;
  4. import androidx.recyclerview.widget.RecyclerView;
  5.  
  6. import android.content.res.Configuration;
  7. import android.media.MediaPlayer;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Gravity;
  13. import android.view.View;
  14. import android.webkit.WebView;
  15. import android.widget.FrameLayout;
  16. import android.widget.ImageView;
  17. import android.widget.MediaController;
  18. import android.widget.VideoView;
  19.  
  20. import com.bumptech.glide.Glide;
  21. import com.devbrackets.android.exomedia.listener.OnCompletionListener;
  22. import com.devbrackets.android.exomedia.listener.OnPreparedListener;
  23. import com.example.tampilanandroidtv.adapter.OPODAdapter;
  24. import com.example.tampilanandroidtv.model.OPODDummy;
  25. import com.google.android.exoplayer2.DefaultLoadControl;
  26. import com.google.android.exoplayer2.DefaultRenderersFactory;
  27. import com.google.android.exoplayer2.ExoPlayerFactory;
  28. import com.google.android.exoplayer2.SimpleExoPlayer;
  29. import com.google.android.exoplayer2.source.ExtractorMediaSource;
  30. import com.google.android.exoplayer2.source.MediaSource;
  31. import com.google.android.exoplayer2.source.dash.DashMediaSource;
  32. import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
  33. import com.google.android.exoplayer2.source.hls.HlsMediaSource;
  34. import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
  35. import com.google.android.exoplayer2.ui.PlayerView;
  36. import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
  37. import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
  38. import com.google.android.exoplayer2.util.Util;
  39.  
  40. import java.util.ArrayList;
  41.  
  42. public class OnePhotoOneDescriptionActivity extends AppCompatActivity implements View.OnClickListener{
  43.  
  44.     private WebView webView;
  45.     private ImageView imageViewBack, imageViewOpod, imageViewPre, imageViewNext;
  46.     private VideoView videoView;
  47.     private RecyclerView recyclerView;
  48.     private LinearLayoutManager layoutManager;
  49.     private OPODAdapter adapter;
  50.     private ArrayList<OPODDummy> data = new ArrayList<>();
  51.     private MediaController mediaController;
  52.     private Uri uriVideo;
  53.     private PlayerView mPlayerView;
  54.     private com.devbrackets.android.exomedia.ui.widget.VideoView videoVieww;
  55.     private SimpleExoPlayer simpleExoPlayer;
  56.     private long mCurrentMillis;
  57.  
  58.     @Override
  59.     public void onBackPressed() {
  60.         super.onBackPressed();
  61.         finish();
  62.         overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
  63.     }
  64.  
  65.     @Override
  66.     protected void onCreate(Bundle savedInstanceState) {
  67.         super.onCreate(savedInstanceState);
  68.         setContentView(R.layout.activity_one_photo_one_description);
  69.  
  70.         webView = findViewById(R.id.wv_opod);
  71.         imageViewBack = findViewById(R.id.iv_back_opod);
  72.         imageViewPre = findViewById(R.id.iv_pre_opod);
  73.         imageViewNext = findViewById(R.id.iv_next_opod);
  74.         recyclerView = findViewById(R.id.rv_opod);
  75.         imageViewOpod = findViewById(R.id.iv_opod);
  76.         videoView = findViewById(R.id.vv_opod);
  77.  
  78.         addData();
  79.         setData();
  80.  
  81.         imageViewPre.setOnClickListener(this);
  82.         imageViewNext.setOnClickListener(this);
  83.         imageViewBack.setOnClickListener(this);
  84.         if (data != null){
  85.             if (data.get(0).getCondition().equals("photo")){
  86.                 dataPhoto(data.get(0).getImageResource(), data.get(0).getNameResource());
  87.             }
  88.             else {
  89.                 dataVideo(data.get(0).getUrlVideo(), data.get(0).getNameResource());
  90.             }
  91.         }
  92.  
  93.     }
  94.  
  95.     private void setData() {
  96.         layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
  97.         adapter = new OPODAdapter(this, new OPODAdapter.OnItemClickListener() {
  98.             @Override
  99.             public void onItemClicked(OPODDummy opodDummy) {
  100.                 if (opodDummy.getCondition().equals("photo")){
  101.                     dataPhoto(opodDummy.getImageResource(), opodDummy.getNameResource());
  102.                 }
  103.                 else {
  104.                     dataVideo(opodDummy.getUrlVideo(), opodDummy.getNameResource());
  105.                 }
  106.             }
  107.         });
  108.         adapter.updateData(data);
  109.         recyclerView.setLayoutManager(layoutManager);
  110.         recyclerView.setHasFixedSize(true);
  111.         recyclerView.setAdapter(adapter);
  112.     }
  113.  
  114.     private void dataVideo(String urlVideo, String nameResource) {
  115.         imageViewOpod.setVisibility(View.INVISIBLE);
  116.         videoView.setVisibility(View.VISIBLE);
  117.  
  118.         try {
  119.             // Memulai MediaController
  120.             mediaController = new MediaController(this);
  121.             mediaController.setAnchorView(videoView);
  122.             mediaController.setMediaPlayer(videoView);
  123.             // Video URL
  124.             String videoUrl = java.net.URLDecoder.decode(urlVideo, "UTF-8");
  125.             uriVideo = Uri.parse(videoUrl);
  126.             videoView.setMediaController(mediaController);
  127.             videoView.setVideoURI(uriVideo);
  128.             videoView.requestFocus();
  129.             videoView.start();
  130.             videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  131.                 public void onPrepared(MediaPlayer mp) {
  132.                     mp.start();
  133.                 }
  134.             });
  135.             videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  136.                 @Override
  137.                 public void onCompletion(MediaPlayer mp) {
  138.                     mp.reset();
  139.                 }
  140.             });
  141.             videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
  142.                 @Override
  143.                 public boolean onError(MediaPlayer mp, int what, int extra) {
  144.                     Log.e("errornya", String.valueOf(extra));
  145.                     return true;
  146.                 }
  147.             });
  148.  
  149.         } catch (Exception e) {
  150.             Log.e("Error", e.getMessage());
  151.             e.printStackTrace();
  152.         }
  153.  
  154.  
  155.  
  156.         webView.loadData(
  157.                 "<html><body><p align=\"justify\">" + nameResource + "</p></body></html>", "text/html", "utf-8"
  158.         );
  159.     }
  160.  
  161.     private void dataPhoto(int imageResource, String nameResource) {
  162.  
  163.         imageViewOpod.setVisibility(View.VISIBLE);
  164.         videoView.stopPlayback();
  165.         videoView.setVisibility(View.INVISIBLE);
  166.         Glide.with(this)
  167.                 .load(imageResource)
  168.                 .placeholder(R.drawable.ic_launcher_background)
  169.                 .into(imageViewOpod);
  170.         webView.loadData(
  171.                 "<html><body><p align=\"justify\" style='font-size:11px'>" + nameResource + "</p></body></html>", "text/html", "utf-8"
  172.         );
  173.     }
  174.  
  175.     private void addData() {
  176.         data.add(new OPODDummy(R.drawable.ic_arrow_forward_black_24dp, getString(R.string.testing_webview), "", "photo"));
  177.         data.add(new OPODDummy(R.drawable.ic_restore_black_24dp, getString(R.string.testing_webvieww), "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", "video"));
  178.         data.add(new OPODDummy(R.drawable.ic_play_circle_filled_black_24dp, getString(R.string.testing_webview), "http://twit.cachefly.net/video/aaa/aaa0033/aaa0033_h264b_640x368_256.mp4", "video"));
  179.         data.add(new OPODDummy(R.drawable.ic_arrow_back_black_24dp, getString(R.string.testing_webvieww), "https://s3.amazonaws.com/playbackacp.com/865/challenge/865-01-03.mp4", "video"));
  180.         data.add(new OPODDummy(R.drawable.ic_arrow_forward_black_24dp, getString(R.string.testing_webview), "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", "video"));
  181.         data.add(new OPODDummy(R.drawable.ic_remove_from_queue_black_24dp, getString(R.string.testing_webvieww), "", "video"));
  182.     }
  183.  
  184.     @Override
  185.     public void applyOverrideConfiguration(Configuration overrideConfiguration) {
  186.         if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 25) {
  187.             overrideConfiguration.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
  188.         }
  189.         super.applyOverrideConfiguration(overrideConfiguration);
  190.     }
  191.  
  192.     @Override
  193.     public void onClick(View v) {
  194.         switch (v.getId()){
  195.             case R.id.iv_pre_opod:
  196.                 recyclerView.getLayoutManager().scrollToPosition(layoutManager.findFirstVisibleItemPosition() - 1);
  197.                 break;
  198.             case R.id.iv_next_opod:
  199.                 recyclerView.getLayoutManager().scrollToPosition(layoutManager.findLastVisibleItemPosition() + 1);
  200.                 break;
  201.             case R.id.iv_back_opod:
  202.                 onBackPressed();
  203.                 break;
  204.         }
  205.     }
  206.  
  207. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top