PlayFragment.java 9.05 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 25
import com.yidianling.common.tools.ToastUtil;

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

范玉宾 committed
30

konghaorui committed
31
/**
32 33
 * Created by haorui on 2019-10-28 .
 * Des:
konghaorui committed
34 35 36
 */
public class PlayFragment extends Fragment implements View.OnClickListener,
        SeekBar.OnSeekBarChangeListener, OnPlayerEventListener {
万齐军 committed
37 38 39 40 41 42 43 44 45 46 47 48 49
    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
50 51 52 53 54 55 56 57

    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
58
        bindView(rootView);
konghaorui committed
59 60 61 62 63 64 65
        return rootView ;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initPlayMode();
范玉宾 committed
66

konghaorui committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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);
89 90
        AudioPlayer.Companion.get().play();
        showFloatView();
konghaorui committed
91 92
    }

93 94 95 96 97
    @Override
    public boolean onPreLoad(int playPosition) {
        return true;
    }

konghaorui committed
98
    @Override
99
    public void onLoad(Music music) {
konghaorui committed
100 101 102 103
        onChangeImpl(music);
    }

    @Override
104
    public void onStartPlay() {
konghaorui committed
105 106 107 108
        ivPlay.setSelected(true);
    }

    @Override
109
    public void onPausePlay() {
konghaorui committed
110 111 112 113 114 115 116
        ivPlay.setSelected(false);
    }

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

    @Override
    public void onBufferingUpdate(int percent) {
125 126 127
        if(percent==0) {
            return;
        }
konghaorui committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141
        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();
142
                showFloatView();
konghaorui committed
143 144 145 146 147 148 149 150 151 152
                break;
            case R.id.iv_next:
                next();
                break;
            case R.id.iv_prev:
                prev();
                break;
        }
    }

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

konghaorui committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    @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();
184
                AudioPlayer.Companion.get().seekTo(-1, progress);
konghaorui committed
185 186 187 188 189 190 191 192 193 194 195 196 197
            } else {
                seekBar.setProgress(0);
            }
        }
    }

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

        tvTitle.setText(music.getTitle());
        tvArtist.setText(music.getArtist());
王佳洋 committed
198
        sbProgress.setProgress((int) AudioPlayer.Companion.get().getCurrentPosition());
konghaorui committed
199
        sbProgress.setSecondaryProgress(0);
200
//        sbProgress.setMax((int) AudioPlayer.Companion.get().getDuration());
201

konghaorui committed
202 203
        mLastProgress = 0;
        tvCurrentTime.setText(R.string.play_time_start);
204
//        tvTotalTime.setText(formatTime(AudioPlayer.Companion.get().getDuration()));
konghaorui committed
205 206 207 208 209 210 211 212 213
        setCoverAndBg(music);
        if (AudioPlayer.Companion.get().isPlaying() || AudioPlayer.Companion.get().isPreparing()) {
            ivPlay.setSelected(true);
        } else {
            ivPlay.setSelected(false);
        }
    }

    void play() {
214
        AudioPlayer.Companion.get().playOrPause();
konghaorui committed
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
    }

    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) {
251 252
        CoverImageUtils.INSTANCE.loadRound(Objects.requireNonNull(music.getCoverPath()), bitmap -> ivCover.setImageBitmap(bitmap));
        CoverImageUtils.INSTANCE.loadBlur(music.getCoverPath(), bitmap -> ivPlayingBg.setImageBitmap(bitmap));
konghaorui committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    }

    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);
270
        PlayerFloatHelper.Companion.removeResetView();
konghaorui committed
271 272
        super.onDestroy();
    }
273 274 275 276 277 278

    @Override
    public void onPrepared(long duration) {
        sbProgress.setMax((int) duration);
        tvTotalTime.setText(formatTime(duration));
    }
konghaorui committed
279 280 281

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

    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
300 301


konghaorui committed
302
}