SeekBar不适用于Android中的在线流媒体

问题描述 投票:2回答:1

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);
    }


}
android android-mediaplayer media seekbar
1个回答
0
投票

请将以下代码与您的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) {

        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.