PlayFragment.java 9.42 KB
Newer Older
konghaorui committed
1 2 3
package com.ydl.component.music;

import android.os.Bundle;
YKai committed
4 5
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
konghaorui committed
6 7 8 9 10 11 12 13 14 15
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.ydl.component.R;
konghaorui committed
16 17 18 19 20 21 22
import com.ydl.media.audio.AudioPlayer;
import com.ydl.media.audio.OnPlayerEventListener;
import com.ydl.media.audio.enums.PlayModeEnum;
import com.ydl.media.audio.model.Music;
import com.ydl.media.audio.utils.CoverImageUtils;
import com.ydl.media.view.PlayTypeEnum;
import com.ydl.media.view.PlayerFloatHelper;
严久程 committed
23
import com.ydl.ydlcommon.utils.LogUtil;
konghaorui committed
24
import com.yidianling.common.tools.ToastUtil;
范玉宾 committed
25
import com.yidianling.muse.event.MeditationFloatEvent;
konghaorui committed
26

konghaorui committed
27
import java.util.HashMap;
konghaorui committed
28
import java.util.Locale;
29
import java.util.Objects;
konghaorui committed
30

范玉宾 committed
31 32
import de.greenrobot.event.EventBus;

konghaorui committed
33
/**
34 35
 * Created by haorui on 2019-10-28 .
 * Des:
konghaorui committed
36 37 38
 */
public class PlayFragment extends Fragment implements View.OnClickListener,
        SeekBar.OnSeekBarChangeListener, OnPlayerEventListener {
万齐军 committed
39 40 41 42 43 44 45 46 47 48 49 50 51
    private LinearLayout llContent;
    private ImageView ivPlayingBg;
    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvArtist;
    private SeekBar sbProgress;
    private TextView tvCurrentTime;
    private TextView tvTotalTime;
    private ImageView ivMode;
    private ImageView ivPlay;
    private ImageView ivNext;
    private ImageView ivPrev;
    private ImageView ivCover;
konghaorui committed
52 53 54 55 56 57 58 59

    int mLastProgress;
    boolean isDraggingProgress;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_play, container, false);
万齐军 committed
60
        bindView(rootView);
konghaorui committed
61 62 63 64 65 66
        return rootView ;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
范玉宾 committed
67
        EventBus.getDefault().register(this);
konghaorui committed
68
        initPlayMode();
范玉宾 committed
69

范玉宾 committed
70
        MeditationFloatEvent event = new MeditationFloatEvent(false,true,null,null,null);
范玉宾 committed
71 72
        EventBus.getDefault().post(event);

konghaorui committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        onChangeImpl(AudioPlayer.Companion.get().getPlayMusic());
        AudioPlayer.Companion.get().addOnPlayEventListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        setListener();
    }

    protected void setListener() {
        ivBack.setOnClickListener(this);
        ivMode.setOnClickListener(this);
        ivPlay.setOnClickListener(this);
        ivPrev.setOnClickListener(this);
        ivNext.setOnClickListener(this);
        sbProgress.setOnSeekBarChangeListener(this);
    }

    void initPlayMode() {
        int mode = AudioPlayer.Companion.get().getPlayMode().value();
        ivMode.setImageLevel(mode);
95 96
        AudioPlayer.Companion.get().play();
        showFloatView();
konghaorui committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    }

    @Override
    public void onChange(Music music) {
        onChangeImpl(music);
    }

    @Override
    public void onPlayerStart() {
        ivPlay.setSelected(true);
    }

    @Override
    public void onPlayerPause() {
        ivPlay.setSelected(false);
    }

    /**
     * 更新播放进度
     */
    @Override
118
    public void onPublish(int percent,long currentPosition) {
konghaorui committed
119
        if (!isDraggingProgress) {
120
            sbProgress.setProgress((int) currentPosition);
konghaorui committed
121 122 123 124 125
        }
    }

    @Override
    public void onBufferingUpdate(int percent) {
126 127 128
        if(percent==0) {
            return;
        }
konghaorui committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142
        sbProgress.setSecondaryProgress(sbProgress.getMax() * 100 / percent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.iv_mode:
                switchPlayMode();
                break;
            case R.id.iv_play:
                play();
143
                showFloatView();
konghaorui committed
144 145 146 147 148 149 150 151 152 153
                break;
            case R.id.iv_next:
                next();
                break;
            case R.id.iv_prev:
                prev();
                break;
        }
    }

154 155 156 157 158 159 160 161
    private void showFloatView() {
        if(!PlayerFloatHelper.Companion.isShow(getActivity())) {
            PlayerFloatHelper.Companion.show(getActivity(), PlayTypeEnum.PLAY_TYPE_FM,new HashMap<>());
        }else {
            PlayerFloatHelper.Companion.showIfPlaying(getActivity());
        }
    }

konghaorui committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar == sbProgress) {
            if (Math.abs(progress - mLastProgress) >= DateUtils.SECOND_IN_MILLIS) {
                tvCurrentTime.setText(formatTime(progress));
                mLastProgress = progress;
            }
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if (seekBar == sbProgress) {
            isDraggingProgress = true;
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (seekBar == sbProgress) {
            isDraggingProgress = false;
            if (AudioPlayer.Companion.get().isPlaying() || AudioPlayer.Companion.get().isPausing()) {
                int progress = seekBar.getProgress();
185
                AudioPlayer.Companion.get().seekTo(-1, progress);
konghaorui committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            } else {
                seekBar.setProgress(0);
            }
        }
    }

    void onChangeImpl(Music music) {
        if (music == null) {
            return;
        }

        tvTitle.setText(music.getTitle());
        tvArtist.setText(music.getArtist());
        sbProgress.setProgress((int) AudioPlayer.Companion.get().getAudioPosition());
        sbProgress.setSecondaryProgress(0);
201
//        sbProgress.setMax((int) AudioPlayer.Companion.get().getDuration());
202

konghaorui committed
203 204
        mLastProgress = 0;
        tvCurrentTime.setText(R.string.play_time_start);
205
//        tvTotalTime.setText(formatTime(AudioPlayer.Companion.get().getDuration()));
konghaorui committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        setCoverAndBg(music);
        if (AudioPlayer.Companion.get().isPlaying() || AudioPlayer.Companion.get().isPreparing()) {
            ivPlay.setSelected(true);
        } else {
            ivPlay.setSelected(false);
        }
    }

    void play() {
        AudioPlayer.Companion.get().playPause();
    }

    void next() {
        AudioPlayer.Companion.get().next();
    }

    void prev() {
        AudioPlayer.Companion.get().prev();
    }

    void switchPlayMode() {
        PlayModeEnum mode = AudioPlayer.Companion.get().getPlayMode();
        switch (mode) {
            case LIST_LOOP:
                mode = PlayModeEnum.SHUFFLE;
                ToastUtil.toastShort(getString(R.string.mode_shuffle));
                break;
            case SHUFFLE:
                mode = PlayModeEnum.SINGLE;
                ToastUtil.toastShort(getString(R.string.mode_one));
                break;
            case SINGLE_LOOP:
                mode = PlayModeEnum.LIST_LOOP;
                ToastUtil.toastShort(getString(R.string.mode_loop));
                break;
        }
        AudioPlayer.Companion.get().setPlayMode(mode);
        initPlayMode();
    }

    void onBackPressed() {
        getActivity().onBackPressed();
        ivBack.setEnabled(false);
    }

    void setCoverAndBg(Music music) {
252 253
        CoverImageUtils.INSTANCE.loadRound(Objects.requireNonNull(music.getCoverPath()), bitmap -> ivCover.setImageBitmap(bitmap));
        CoverImageUtils.INSTANCE.loadBlur(music.getCoverPath(), bitmap -> ivPlayingBg.setImageBitmap(bitmap));
konghaorui committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    }

    String formatTime(long time) {
        return formatTime("mm:ss", time);
    }

    public static String formatTime(String pattern, long milli) {
        int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
        int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
        String mm = String.format(Locale.getDefault(), "%02d", m);
        String ss = String.format(Locale.getDefault(), "%02d", s);
        return pattern.replace("mm", mm).replace("ss", ss);
    }

    @Override
    public void onDestroy() {
        AudioPlayer.Companion.get().removeOnPlayEventListener(this);
konghaorui committed
271
        PlayerFloatHelper.Companion.onDestroy();
范玉宾 committed
272 273 274
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
konghaorui committed
275 276
        super.onDestroy();
    }
277 278 279 280 281 282

    @Override
    public void onPrepared(long duration) {
        sbProgress.setMax((int) duration);
        tvTotalTime.setText(formatTime(duration));
    }
konghaorui committed
283 284 285

    @Override
    public void onComplete() {
严久程 committed
286
        LogUtil.e("onComplete");
konghaorui committed
287
    }
万齐军 committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

    private void bindView(View bindSource) {
        llContent = bindSource.findViewById(R.id.ll_content);
        ivPlayingBg = bindSource.findViewById(R.id.iv_play_page_bg);
        ivBack = bindSource.findViewById(R.id.iv_back);
        tvTitle = bindSource.findViewById(R.id.tv_title);
        tvArtist = bindSource.findViewById(R.id.tv_artist);
        sbProgress = bindSource.findViewById(R.id.sb_progress);
        tvCurrentTime = bindSource.findViewById(R.id.tv_current_time);
        tvTotalTime = bindSource.findViewById(R.id.tv_total_time);
        ivMode = bindSource.findViewById(R.id.iv_mode);
        ivPlay = bindSource.findViewById(R.id.iv_play);
        ivNext = bindSource.findViewById(R.id.iv_next);
        ivPrev = bindSource.findViewById(R.id.iv_prev);
        ivCover = bindSource.findViewById(R.id.iv_cover);
    }
范玉宾 committed
304 305 306 307 308

    public void onEventMainThread(MeditationFloatEvent event){

    }

konghaorui committed
309
}