当媒体播放器更改源时 UI 冻结

问题描述

是不是线程里面的线程引起的?

活动包含项目(每个项目都有自己的歌曲)。当用户单击该项目时,它将播放歌曲。但是,当用户在歌曲结束的那一刻滚动然后更改为新歌曲时,包含这些项目的回收器视图将冻结。 我知道这不是最佳实践,但我该如何解决这个问题? AllSongs 列表包含指向位于 SD 卡中的 mp3 的 URL。里面的线程改变了搜索栏。

我将不胜感激。这是我的第一个问题,不要对我的评价如此强烈。 :)

private void initPlayer(int position) {
    this.position = position;
    songTitle.setText(getarabic(id,position));
    new Thread(() -> {
        if (mMediaPlayer != null) {
            try {
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
            } catch (Exception e) {
                e.printstacktrace();
            }
            mMediaPlayer.release();
        }
        if (!prepareHimself(id,position))
            return;
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(allSongs.get(String.format("%03d",id) + String.format("%03d",position + 1)));
            mMediaPlayer.prepareAsync();

        } catch (NullPointerException | IllegalStateException | IOException e) {
            e.printstacktrace();
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
        if (mMediaPlayer == null)
            return;
        mMediaPlayer.setonPreparedListener(mediaPlayer -> {
            String totalTime = "05:00";
            totalTime = createTimeLabel(mMediaPlayer.getDuration());
            tottime.setText(totalTime);
            mSeekBar.setMax(mMediaPlayer.getDuration());
            mMediaPlayer.start();
            if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                curListIcon.setimageResource(R.drawable.ic_pause_black_24dp);
                curListIcon.setVisibility(VISIBLE);
            }
            playIcon.setimageResource(R.drawable.ic_pause_black_24dp);
        });

        mMediaPlayer.setonCompletionListener(mp -> {
            int curSongPoition = position;
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
            if (curSongPoition < suraLength - 1) {
                curSongPoition++;
                initPlayer(curSongPoition);
                createNotification(1);
            } else {
                curSongPoition = startPosition;
                repeatTime++;
                if (repeatTime >= repeatTimeAll) {
                    if (mMediaPlayer != null)
                        mMediaPlayer.stop();
                    stopMusic();
                } else {
                    initPlayer(curSongPoition);
                    createNotification(1);
                }
            }
            scrollPosition(curSongPoition);
        });

        new Thread(() -> {
            while (mMediaPlayer != null) {
                try {
                    if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                        Message msg = new Message();
                        msg.what = mMediaPlayer.getCurrentPosition();
                        handler.sendMessage(msg);
                        Thread.sleep(50);
                    }
                } catch (InterruptedException | IllegalStateException ignored) {
                }
            }
        }).start();
    }).start();

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)