SeekBar 无法在 Android 中使用在线流式传输

SeekBar not working with online streaming in Android

SeekBar 不工作。在应用程序中,我正在尝试从源播放音频,也尝试从 SeekBar 播放音频。但是 SeekBar 并没有移动音频文件。

在应用程序中,我尝试从源播放音频,也尝试从 SeekBar 播放音频。但是 SeekBar 并没有移动音频文件。

这是我的代码:

public class TrackDetailActivity extends AppCompatActivity {

    private static final String TAG = "TrackDetailActivity";
    private TextView mArtistName, mTrackName;
    private ImageView mAlbumArt;

    private MediaPlayer mMediaPlayer;
    private ImageView mPlayerPlay, mStopPlayer;
    private SeekBar mSeekBar;

    public static final String KEY_TRACK = "trackObj";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.track_detail_new);


        mArtistName = (TextView) findViewById(R.id.tv_artist_name);
        mTrackName = (TextView) findViewById(R.id.tv_track_name);
        mAlbumArt = (ImageView) findViewById(R.id.iv_album_art);
        mPlayerPlay = (ImageView) findViewById(R.id.playerControl);
        mStopPlayer = (ImageView) findViewById(R.id.stopControl);
        mSeekBar = (SeekBar) findViewById(R.id.seek);

        Intent intent = getIntent();


        final Track track = (Track) intent.getSerializableExtra(TrackDetailActivity.KEY_TRACK);

        String trackName = track.getTrackName();
        mTrackName.setText(trackName);
        mArtistName.setText(track.getArtistName());
        Log.d(TAG, "onCreate: " + track.getDuration());


        GlideApp.with(this)
                .asBitmap()
                .load(track.getAlbumArtUrl())
                .centerCrop()
                .listener(requestListener)
                .into(mAlbumArt);


        //Toast.makeText(this, "Track: " + track.getTrackName(), Toast.LENGTH_SHORT).show();

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mMediaPlayer.start();
                mSeekBar.setProgress(0);
                mSeekBar.setMax(mediaPlayer.getDuration());

                Log.d(TAG, "onPrepared: Max Seek: " + mediaPlayer.getDuration());
                togglePlayPause();

            }
        });


        mPlayerPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mMediaPlayer.isPlaying()) {
                    togglePlayPause();
                    mMediaPlayer.stop();
                    mMediaPlayer.reset();

                } else {
                    togglePlayPause();
                    try {
                        mMediaPlayer.setDataSource(track.getPreviewUrl());
                        mMediaPlayer.prepareAsync();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread durationThread = new SeekThread();
        durationThread.start();


    }

    public class SeekThread extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                Log.d("SeekThread", "run: " + 1);
                mSeekBar.setProgress(mMediaPlayer.getDuration());
                Log.d("SeekThread", "run: " + mMediaPlayer.getCurrentPosition());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    private void togglePlayPause() {
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
            mPlayerPlay.setImageResource(R.drawable.ic_action_play);
        } else {
            mMediaPlayer.start();
            mPlayerPlay.setImageResource(R.drawable.ic_pause);

        }
    }


    private RequestListener<Bitmap> requestListener = new RequestListener<Bitmap>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
            setBlurredImage(resource);
            return false;
        }
    };


    private void setBlurredImage(Bitmap source) {
        Bitmap blurredBitmap = NativeStackBlur.process(source, 50);

        ImageView screenBg = (ImageView) findViewById(R.id.screen_bg);

        screenBg.setImageBitmap(blurredBitmap);
    }


}

请将以下代码与您的 seekBar 相关联,然后这将起作用。

    mSeekBar.setMax(mMediaPlayer.getDuration());
    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
        }
    }, 0, 10);

    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            mMediaPlayer.seekTo(progress);

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });