溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android?SeekBar控制視頻播放進度實現(xiàn)的方法是什么

發(fā)布時間:2023-04-07 11:56:13 來源:億速云 閱讀:111 作者:iii 欄目:開發(fā)技術(shù)

這篇“Android SeekBar控制視頻播放進度實現(xiàn)的方法是什么”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Android SeekBar控制視頻播放進度實現(xiàn)的方法是什么”文章吧。

效果圖

Android?SeekBar控制視頻播放進度實現(xiàn)的方法是什么

簡介

使用VideoView控件播放視頻時,我們希望能夠調(diào)節(jié)播放的進度,一種方法是使用自帶的控制器MediaController進行控制,另一種方法是自己實現(xiàn)一個SeekBar控制。

使用MediaController控制器

在播放視頻時使用自帶的控制器MediaController進行進度控制。

MediaController mediaController = new MediaController(this);
mVideoView.setMediaController(mediaController);

使用SeekBar

在播放視頻時使用自己實現(xiàn)一個SeekBar控制播放進度。

Android?SeekBar控制視頻播放進度實現(xiàn)的方法是什么

自定義SeekBar背景drawable/bg_rounded.xml,實現(xiàn)圓角半透明效果。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 背景顏色 -->
    <solid android:color="#20ffffff"/>
    <!-- 圓角背景 -->
    <corners
        android:topLeftRadius="12dp"
        android:topRightRadius="12dp"
        android:bottomLeftRadius="12dp"
        android:bottomRightRadius="12dp"/>
</shape>

界面布局文件layout/activity_video.xml,界面中有一個用于視頻播放的VideoView控件和控制播放進度的SeekBar控件。 其中SeekBar使用第一步定義好的背景效果,android:background=“@drawable/bg_rounded”

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:keepScreenOn="true"
    tools:context=".activitys.VideoActivity">
    <VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="500dp"
        android:layout_height="25dp"
        android:background="@drawable/bg_rounded"
        android:layout_marginBottom="45dp"
        android:progressTint="#7FFFD4"
        android:thumbTint="#7FFFD4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

VideoActivity文件。

public class VideoActivity extends AppCompatActivity implements EdgeSensorView.MapDemo {
    private static final String TAG = "VideoActivity";
    private VideoView mVideoView;
    private SeekBar mSeekBar;
    private float curProgress;
    private float MAX_PROGRESS;
    private boolean isSeekBarProgress = false;
    private Handler handler = new Handler();
	// 播放過程,自動更新seekbar的進度
    private Runnable runnable = new Runnable() {
        public void run() {
            if (mVideoView.isPlaying()) {
                if (!isSeekBarProgress) {
                    int current = mVideoView.getCurrentPosition();
                    mSeekBar.setProgress(current);
                }
            }
            handler.postDelayed(runnable, 100);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        mSeekBar = findViewById(R.id.seekbar);
        mVideoView = findViewById(R.id.video_view);
        mVideoView.setVideoURI(Uri.parse("android.resource://"
                + getApplicationContext().getPackageName() + "/" + R.raw.vid_bigbuckbunny));
//        MediaController mediaController = new MediaController(this);
//        mVideoView.setMediaController(mediaController);
        mVideoView.requestFocus();
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                MAX_PROGRESS = mVideoView.getDuration();
                mSeekBar.setMax((int) MAX_PROGRESS);
                Log.i(TAG, "onCreate: " + MAX_PROGRESS + "," + curProgress + "," + bitmaps.length);
                // 開始線程,更新進度條的刻度
                handler.postDelayed(runnable, 0);
                mVideoView.start();
            }
        });
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                curProgress = 0;
                mSeekBar.setProgress(0);
            }
        });
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isSeekBarProgress = true;
                if (mVideoView.isPlaying()) {
                    mVideoView.pause();
                }
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int pro = seekBar.getProgress();
                mVideoView.seekTo(pro);
                if (!mVideoView.isPlaying()) {
                    mVideoView.seekTo(pro);
                    mVideoView.start();
                }
                isSeekBarProgress = false;
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
    private void updateSeekBarStatus(final boolean b) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mSeekBar.setPressed(b);
            }
        });
    }
}

拓展:

  • 增加控制播放/暫停的按鈕;

  • 增加播放視頻當前播放時間及總時長的文本顯示。

以上就是關(guān)于“Android SeekBar控制視頻播放進度實現(xiàn)的方法是什么”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI