EspoirX / StarrySky

🔥A Powerful and Streamline MusicLibrary(一个丰富的音乐播放封装库,支持多种音频格式,完美解决你的问题。)
https://github.com/EspoirX/StarrySky
MIT License
1.39k stars 210 forks source link

播放问题 #153

Closed tdxtxt closed 4 years ago

tdxtxt commented 4 years ago

设置为顺序播放模式情况下,我传入一个播放列表,列表中媒体地址相同,songId不同,结果只能播放一个,也不能切换上一首、下一首,请问怎么解决?

EspoirX commented 4 years ago

我这边测试没问题,你提供一下你的代码逻辑

tdxtxt commented 4 years ago

整体代码:

public class SexAudioPlayerActivity extends AppCompatActivity {

    TextView tvMusicTitle;
    TextView tvMusicDesc;
    ImageView ivMusicCover;
    SeekBar seekbar;

    TextView tvTimeCurrent;
    TextView tvTimeTotal;
    ImageButton btnAudioList;
    ImageButton btnAudioLast;
    CheckBox checkBoxAudioPlay;
    ImageButton btnAudioNext;
    TextView btnSpeed;

    List<SongInfo> mData;//播放列表
    int playIndex;//开始播放的位置
    boolean isRePlayer;//是否为恢复之前的播放状态,默认否

    private TimerTaskManager mTimerTask;

    private void getParmas(Intent intent) {
        mData = intent.getParcelableArrayListExtra("data");
        playIndex = intent.getIntExtra("playIndex",0);

        if(!(mData != null && mData.size() > 0)){
            mData = StarrySky.with().getPlayList();
            playIndex = StarrySky.with().getNowPlayingIndex();
            isRePlayer = true;
        }
        if(mData == null) mData = new ArrayList<>();
        if(playIndex < 0) playIndex = 0;
        if(playIndex >= mData.size()) playIndex = 0;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_audioplayer);
        getParmas(getIntent());
        initView();
    }

    private void initView() {
        tvMusicTitle = findViewById(R.id.tv_title);
        tvMusicDesc = findViewById(R.id.tv_desc);
        ivMusicCover = findViewById(R.id.iv_cover);
        seekbar = findViewById(R.id.seekbar);
        tvTimeCurrent = findViewById(R.id.tv_time_current);
        tvTimeTotal = findViewById(R.id.tv_time_total);
        btnAudioList = findViewById(R.id.btn_audio_list);
        btnAudioLast = findViewById(R.id.btn_audio_last);
        checkBoxAudioPlay = findViewById(R.id.check_audio_play);
        btnAudioNext = findViewById(R.id.btn_audio_next);
        btnSpeed = findViewById(R.id.btn_speed);

        clickView(btnAudioNext);
        clickView(btnAudioLast);
        clickView(btnAudioList);
        clickView(btnSpeed);

        mTimerTask = new TimerTaskManager();
        mTimerTask.setUpdateProgressTask(new Runnable() {
            @Override
            public void run() {
                updateProgress();
            }
        });

        checkBoxAudioPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {//播放
                    StarrySky.with().restoreMusic();
                } else {//暂停
                    StarrySky.with().pauseMusic();
                }
            }
        });
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                StarrySky.with().seekTo(seekBar.getProgress());
            }
        });

        //状态监听
        StarrySky.with().playbackState().observe(this, new Observer<PlaybackStage>() {
            @Override
            public void onChanged(@Nullable PlaybackStage playbackStage) {
                if (playbackStage == null) return;
                SongInfo songInfo = playbackStage.getSongInfo();
                if(songInfo == null) return;
                switch (playbackStage.getStage()) {
                    case PlaybackStage.NONE:
                        break;
                    case PlaybackStage.START:
                        btnSpeed.setText((StarrySky.with().getPlaybackSpeed() == 0 ? 1 : StarrySky.with().getPlaybackSpeed())+ "X");
                        if (!checkBoxAudioPlay.isChecked()) checkBoxAudioPlay.setChecked(true);
                        mTimerTask.startToUpdateProgress();
                        updateSongUI(songInfo);
                        break;
                    case PlaybackStage.SWITCH:
                        break;
                    case PlaybackStage.PAUSE:
                        if (checkBoxAudioPlay.isChecked()) checkBoxAudioPlay.setChecked(false);
                        mTimerTask.stopToUpdateProgress();
                        break;
                    case PlaybackStage.STOP:
                        mTimerTask.stopToUpdateProgress();
                        break;
                    case PlaybackStage.COMPLETION:
                        mTimerTask.stopToUpdateProgress();
                        seekbar.setProgress(0);
                        tvTimeCurrent.setText("00:00");
                        if(mData.size() == 1) checkBoxAudioPlay.setChecked(false);
                        break;
                    case PlaybackStage.BUFFERING:
                        break;
                    case PlaybackStage.ERROR:
                        mTimerTask.stopToUpdateProgress();
                }
            }
        });

        if(mData.size() > 0){
            updateSongUI(mData.get(playIndex));
//            StarrySky.with().updatePlayList(mData);
            if(isRePlayer && PlaybackStateCompat.STATE_PAUSED == StarrySky.with().getState()){//暂停状态
                StarrySky.with().pauseMusic();
                updateProgress();
            }else{
                StarrySky.with().playMusic(mData,playIndex);
            }
        }

        if(mData.size() == 1){
            StarrySky.with().setRepeatMode(PlaybackStateCompatExtKt.getSINGLE_MODE_ONE());
        }else{
            StarrySky.with().setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);
            StarrySky.with().setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE);
        }
    }

    private void updateProgress() {
        long position = StarrySky.with().getPlayingPosition();
        long duration = StarrySky.with().getDuration();
        long buffered = StarrySky.with().getBufferedPosition();
        if(seekbar.getMax() != duration){
            seekbar.setMax((int) duration);
        }
        seekbar.setProgress((int) position);
    }

    private void updateSongUI(SongInfo songInfo) {
        if(songInfo == null) songInfo = new SongInfo();
        tvMusicTitle.setText(songInfo.getSongName());
        tvMusicDesc.setText(songInfo.getDescription());
        btnSpeed.setText((StarrySky.with().getPlaybackSpeed() == 0 ? 1 : StarrySky.with().getPlaybackSpeed())+ "X");
        Glide.with(this).load(songInfo.getAlbumCover()).into(ivMusicCover);
    }

    private void clickView(View view) {
        if(view == null) return;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.btn_audio_next:
                        StarrySky.with().skipToNext();
                        break;
                    case R.id.btn_audio_last:
                        StarrySky.with().skipToPrevious();
                        break;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mTimerTask != null) mTimerTask.removeUpdateProgressTask();
    }

    /**
     * 用于还原正在播放的音乐
     */
    public static void open(Context context) {
        if (context == null) return;
        SexAudioPlayerActivity.open(context,0, null);
    }
    /**
     * 简单封装,只放一首歌曲
     */
    public static void open(Context context, SongInfo songInfo) {
        List<SongInfo> data = new ArrayList<>();
        if(songInfo != null) data.add(songInfo);
        SexAudioPlayerActivity.open(context,0, data);
    }

    /**
     * 播放data的集合,playIndex为当前播放的音乐
     */
    public static void open(Context context,int playIndex, List<SongInfo> data){
        if(context == null) return;
        if(data == null) data = new ArrayList<>();
        if(playIndex < 0) playIndex = 0;
        if(playIndex >= data.size()) playIndex = 0;
        Intent intent = new Intent(context, SexAudioPlayerActivity.class)
                .putExtra("playIndex",playIndex)
                .putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) data);
        if(context instanceof Activity){
            context.startActivity(intent);
        }else{
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    public static List<SongInfo> testCreateMode(){
        List<SongInfo> data = new ArrayList<>();
        SongInfo info1 = new SongInfo();
        info1.setSongId("6");
        info1.setSongName("6");
        info1.setAlbumCover("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=521328773,3191367834&fm=11&gp=0.jpg");
        info1.setSongUrl("https://test-1259026738.cos.ap-shanghai.myqcloud.com//012_%E7%BC%A9%E6%B7%B7.mp3");

        SongInfo info2 = new SongInfo();
        info2.setSongId("7");
        info1.setSongName("7");
        info2.setAlbumCover("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582884567910&di=6051a283b94ac92bc784632eaf735660&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F16%2F12%2F01300535031999137270128786964.jpg");
        info2.setSongUrl("https://test-1259026738.cos.ap-shanghai.myqcloud.com//012_%E7%BC%A9%E6%B7%B7.mp3");

        SongInfo info3 = new SongInfo();
        info3.setSongId("8");
        info1.setSongName("8");
        info3.setDescription("第107会");
        info3.setAlbumCover("https://imagev2.xmcdn.com/group65/M03/64/16/wKgMal2cB2jSy3efABgKFmuKQm0097.jpg!strip=1&quality=7&magick=jpg&op_type=5&upload_type=cover&name=web_large&device_type=ios");
        info3.setSongUrl("https://test-1259026738.cos.ap-shanghai.myqcloud.com//012_%E7%BC%A9%E6%B7%B7.mp3");

        SongInfo info4 = new SongInfo();
        info4.setSongId("9");
        info1.setSongName("9");
        info4.setDescription("第导氮会");
        info4.setAlbumCover("https://imagev2.xmcdn.com/group18/M04/C5/14/wKgJJVfDl-LR6m97AAUbRT3EQ6A497.jpg!strip=1&quality=7&magick=jpg&op_type=5&upload_type=cover&name=web_large&device_type=ios");
        info4.setSongUrl("https://test-1259026738.cos.ap-shanghai.myqcloud.com//012_%E7%BC%A9%E6%B7%B7.mp3");

        SongInfo info5 = new SongInfo();
        info5.setSongId("10");
        info5.setSongName("10");
        info5.setDescription("说明游戏以滴滴滴滴");
        info5.setAlbumCover("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1903168043,1444443519&fm=26&gp=0.jpg");
        info5.setSongUrl("https://test-1259026738.cos.ap-shanghai.myqcloud.com//012_%E7%BC%A9%E6%B7%B7.mp3");

        data.add(info2); data.add(info1); data.add(info3); data.add(info4); data.add(info5);
        return data;
    }
}
EspoirX commented 4 years ago

有个 testCreateMode 方法,但是没用到。。什么意思。。

tdxtxt commented 4 years ago

这是跳转测试代码 SexAudioPlayerActivity.open(activity,0,SexAudioPlayerActivity.testCreateMode());

tdxtxt commented 4 years ago

找到解决办法了,需要屏蔽这句代码,但还不知道其中原因StarrySky.with().setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);

另外想问一下StarrySky.with().setShuffleMode() 与 StarrySky.with().setRepeatMode()这两种方式设置播放模式有什么区别吗?

EspoirX commented 4 years ago

可以理解为一个是在播放队列顺序正常的情况下的播放模式,一个是是否要打乱播放队列的顺序,因为播放器api问题,所以分开了2个方法,为了避免误解,后面会考虑合并成一个方法。