123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package com.bgy.autosale.helpers;
- import android.content.Context;
- import android.graphics.Color;
- import android.media.MediaPlayer;
- import android.support.constraint.ConstraintLayout;
- import android.util.Log;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.VideoView;
- import com.bumptech.glide.Glide;
- import com.bgy.autosale.Constant;
- import com.youth.banner.Banner;
- import com.youth.banner.BannerConfig;
- import com.youth.banner.Transformer;
- import com.youth.banner.loader.ImageLoader;
- import java.io.File;
- import java.util.ArrayList;
- /**
- * Created by cjx on 2020-06-03
- * 说明:
- */
- public class BannerMediaHelper {
- private static final String TAG = "BannerMediaHelper";
- private Banner bannerView;
- private VideoView videoView;
- private boolean isVisible = true;
- public BannerMediaHelper(ViewGroup viewGroup) {
- //放图片地址的集合
- ArrayList<Object> imagePath = new ArrayList<>();
- String videoPath = null;
- String bgVoicePath = null;
- String path = Constant.DIR_BANNER;
- File dir = new File(path);
- Log.d(TAG, "BannerMediaHelper: "+dir);
- if (!dir.exists()) {
- dir.mkdirs();
- } else {
- if (dir.listFiles() != null) {
- File[] bannerFile = dir.listFiles();
- for (File f : bannerFile) {
- String name = f.getName().toLowerCase();
- if (name.endsWith(".mp4")) {
- videoPath = f.getAbsolutePath();
- break;
- }
- if (name.endsWith(".jpg") || name.endsWith(".png")) {
- imagePath.add(f.getAbsolutePath());
- } else if (name.endsWith(".mp3") && bgVoicePath == null) {
- bgVoicePath = f.getAbsolutePath();
- }
- }
- }
- }
- if (videoPath != null) {
- createVideoView(viewGroup, videoPath);
- return;
- }
- createImageView(viewGroup, imagePath);
- UISoundHelper.getInstance().setBgVoicePath(bgVoicePath);
- UISoundHelper.getInstance().playBgVoice();
- }
- private void createVideoView(ViewGroup view, String videoPath) {
- videoView = new VideoView(view.getContext());
- videoView.setBackgroundColor(Color.YELLOW);
- ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(0, 0);
- lp.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
- lp.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
- lp.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
- lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
- view.addView(videoView, 0, lp);
- videoView.setVideoPath(videoPath);
- videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- if (!isVisible) {
- return;
- }
- videoView.start();
- }
- });
- videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
- @Override
- public void onPrepared(MediaPlayer mp) {
- UISoundHelper.getInstance().setVideoMediaPlayer(mp);
- videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
- @Override
- public boolean onInfo(MediaPlayer mp, int what, int extra) {
- videoView.setBackgroundColor(Color.TRANSPARENT);
- return false;
- }
- });
- }
- });
- videoView.start();
- }
- private void createImageView(ViewGroup view, ArrayList<Object> listPath) {
- bannerView = new Banner(view.getContext());
- ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(0, 0);
- lp.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
- lp.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
- lp.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
- lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
- view.addView(bannerView, 0, lp);
- //设置图片加载器,图片加载器在下方
- bannerView.setImageLoader(new ImageLoader() {
- @Override
- public void displayImage(Context context, Object path, ImageView imageView) {
- Glide.with(context).load(path).into(imageView);
- }
- });
- bannerView.setBannerStyle(BannerConfig.NOT_INDICATOR);
- //设置图片网址或地址的集合
- bannerView.setImages(listPath);
- //设置轮播的动画效果,内含多种特效,可点入方法内查找后内逐一体验
- bannerView.setBannerAnimation(Transformer.Default);
- //设置轮播间隔时间
- bannerView.setDelayTime(8000);
- bannerView.start();
- }
- public void setVisible(boolean visible) {
- isVisible = visible;
- UISoundHelper.getInstance().setVisible(visible);
- if (visible) {
- start();
- } else {
- stop();
- }
- }
- private void start() {
- if (videoView != null) {
- videoView.start();
- } else {
- bannerView.startAutoPlay();
- UISoundHelper.getInstance().playBgVoice();
- }
- }
- public void stop() {
- if (videoView != null) {
- videoView.pause();
- } else {
- bannerView.stopAutoPlay();
- }
- }
- }
|