BannerMediaHelper.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.bgy.autosale.helpers;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.media.MediaPlayer;
  5. import android.support.constraint.ConstraintLayout;
  6. import android.util.Log;
  7. import android.view.ViewGroup;
  8. import android.widget.ImageView;
  9. import android.widget.VideoView;
  10. import com.bumptech.glide.Glide;
  11. import com.bgy.autosale.Constant;
  12. import com.youth.banner.Banner;
  13. import com.youth.banner.BannerConfig;
  14. import com.youth.banner.Transformer;
  15. import com.youth.banner.loader.ImageLoader;
  16. import java.io.File;
  17. import java.util.ArrayList;
  18. /**
  19. * Created by cjx on 2020-06-03
  20. * 说明:
  21. */
  22. public class BannerMediaHelper {
  23. private static final String TAG = "BannerMediaHelper";
  24. private Banner bannerView;
  25. private VideoView videoView;
  26. private boolean isVisible = true;
  27. public BannerMediaHelper(ViewGroup viewGroup) {
  28. //放图片地址的集合
  29. ArrayList<Object> imagePath = new ArrayList<>();
  30. String videoPath = null;
  31. String bgVoicePath = null;
  32. String path = Constant.DIR_BANNER;
  33. File dir = new File(path);
  34. Log.d(TAG, "BannerMediaHelper: "+dir);
  35. if (!dir.exists()) {
  36. dir.mkdirs();
  37. } else {
  38. if (dir.listFiles() != null) {
  39. File[] bannerFile = dir.listFiles();
  40. for (File f : bannerFile) {
  41. String name = f.getName().toLowerCase();
  42. if (name.endsWith(".mp4")) {
  43. videoPath = f.getAbsolutePath();
  44. break;
  45. }
  46. if (name.endsWith(".jpg") || name.endsWith(".png")) {
  47. imagePath.add(f.getAbsolutePath());
  48. } else if (name.endsWith(".mp3") && bgVoicePath == null) {
  49. bgVoicePath = f.getAbsolutePath();
  50. }
  51. }
  52. }
  53. }
  54. if (videoPath != null) {
  55. createVideoView(viewGroup, videoPath);
  56. return;
  57. }
  58. createImageView(viewGroup, imagePath);
  59. UISoundHelper.getInstance().setBgVoicePath(bgVoicePath);
  60. UISoundHelper.getInstance().playBgVoice();
  61. }
  62. private void createVideoView(ViewGroup view, String videoPath) {
  63. videoView = new VideoView(view.getContext());
  64. videoView.setBackgroundColor(Color.YELLOW);
  65. ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(0, 0);
  66. lp.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
  67. lp.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
  68. lp.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
  69. lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
  70. view.addView(videoView, 0, lp);
  71. videoView.setVideoPath(videoPath);
  72. videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  73. @Override
  74. public void onCompletion(MediaPlayer mp) {
  75. if (!isVisible) {
  76. return;
  77. }
  78. videoView.start();
  79. }
  80. });
  81. videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  82. @Override
  83. public void onPrepared(MediaPlayer mp) {
  84. UISoundHelper.getInstance().setVideoMediaPlayer(mp);
  85. videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
  86. @Override
  87. public boolean onInfo(MediaPlayer mp, int what, int extra) {
  88. videoView.setBackgroundColor(Color.TRANSPARENT);
  89. return false;
  90. }
  91. });
  92. }
  93. });
  94. videoView.start();
  95. }
  96. private void createImageView(ViewGroup view, ArrayList<Object> listPath) {
  97. bannerView = new Banner(view.getContext());
  98. ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(0, 0);
  99. lp.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
  100. lp.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
  101. lp.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
  102. lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
  103. view.addView(bannerView, 0, lp);
  104. //设置图片加载器,图片加载器在下方
  105. bannerView.setImageLoader(new ImageLoader() {
  106. @Override
  107. public void displayImage(Context context, Object path, ImageView imageView) {
  108. Glide.with(context).load(path).into(imageView);
  109. }
  110. });
  111. bannerView.setBannerStyle(BannerConfig.NOT_INDICATOR);
  112. //设置图片网址或地址的集合
  113. bannerView.setImages(listPath);
  114. //设置轮播的动画效果,内含多种特效,可点入方法内查找后内逐一体验
  115. bannerView.setBannerAnimation(Transformer.Default);
  116. //设置轮播间隔时间
  117. bannerView.setDelayTime(8000);
  118. bannerView.start();
  119. }
  120. public void setVisible(boolean visible) {
  121. isVisible = visible;
  122. UISoundHelper.getInstance().setVisible(visible);
  123. if (visible) {
  124. start();
  125. } else {
  126. stop();
  127. }
  128. }
  129. private void start() {
  130. if (videoView != null) {
  131. videoView.start();
  132. } else {
  133. bannerView.startAutoPlay();
  134. UISoundHelper.getInstance().playBgVoice();
  135. }
  136. }
  137. public void stop() {
  138. if (videoView != null) {
  139. videoView.pause();
  140. } else {
  141. bannerView.stopAutoPlay();
  142. }
  143. }
  144. }