Android UniversalVideoView實(shí)現(xiàn)視頻播放器
本文實(shí)例為大家分享了Android UniversalVideoView實(shí)現(xiàn)視頻播放器的具體代碼,供大家參考,具體內(nèi)容如下
1.添加依賴 app下的 build.gradle
dependencies { ? ? ? ...... ? ? ? compile 'com.linsea:universalvideoview:1.1.0@aar' }
2.XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical"> ? ? ? ? <FrameLayout ? ? ? ? android:id="@+id/video_layout" ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:background="@android:color/black"> ? ? ? ? ? <com.universalvideoview.UniversalVideoView ? ? ? ? ? ? android:id="@+id/videoView" ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? app:uvv_autoRotation="true" ? ? ? ? ? ? app:uvv_fitXY="false" /> ? ? ? ? ? <com.universalvideoview.UniversalMediaController ? ? ? ? ? ? android:id="@+id/media_controller" ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? app:uvv_scalable="true" /> ? ? </FrameLayout> ? ? ? <LinearLayout ? ? ? ? android:id="@+id/bottom_layout" ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="0dp" ? ? ? ? android:layout_weight="1" ? ? ? ? android:orientation="vertical"> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/start" ? ? ? ? ? ? android:layout_margin="5dp" ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:text="start" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/introduction" ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:text="this is video introduciton ......" ? ? ? ? ? ? android:background="@color/uvv_gray" /> ? ? </LinearLayout> </LinearLayout>
3.MainActivity
import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import com.universalvideoview.UniversalMediaController; import com.universalvideoview.UniversalVideoView; ? public class MainActivity extends Activity implements UniversalVideoView.VideoViewCallback{ ? ? ? private static final String TAG = "MainActivity"; ? ? private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY"; ? ? private static final String VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; ? ? //視頻地址 ? ? //http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 ? ? //http://vjs.zencdn.net/v/oceans.mp4 ? ? //https://media.w3.org/2010/05/sintel/trailer.mp4 ? ? ? UniversalVideoView mVideoView; ? ? UniversalMediaController mMediaController; ? ? ? View mBottomLayout; ? ? View mVideoLayout; ? ? TextView mStart; ? ? ? private int mSeekPosition; ? ? private int cachedHeight; ? ? private boolean isFullscreen; ? ? private TextView tv_title; ? ? private LinearLayout titlebar; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? tv_title = (TextView) findViewById(R.id.introduction); ? ? ? ? //titlebar = (LinearLayout) findViewById(R.id.titlebar); ? ? ? ? tv_title.setText("UniversalVideoView"); ? ? ? ? ? mVideoLayout = findViewById(R.id.video_layout); ? ? ? ? mBottomLayout = findViewById(R.id.bottom_layout); ? ? ? ? mVideoView = (UniversalVideoView) findViewById(R.id.videoView); ? ? ? ? mMediaController = (UniversalMediaController) findViewById(R.id.media_controller); ? ? ? ? mVideoView.setMediaController(mMediaController); ? ? ? ? //設(shè)置播放屏幕模式和設(shè)置播放地址 ? ? ? ? setVideoAreaSize(); ? ? ? ? //設(shè)置屏幕狀態(tài)和播放狀態(tài)的監(jiān)聽 ? ? ? ? mVideoView.setVideoViewCallback(this); ? ? ? ? mStart = (TextView) findViewById(R.id.start); ? ? ? ? ? mStart.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? if (mSeekPosition > 0) { ? ? ? ? ? ? ? ? ? ? mVideoView.seekTo(mSeekPosition); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? mVideoView.start(); ? ? ? ? ? ? ? ? mMediaController.setTitle("Big Buck Bunny"); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? ? mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onCompletion(MediaPlayer mp) { ? ? ? ? ? ? ? ? Log.d(TAG, "onCompletion "); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? ? mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onPrepared(MediaPlayer mp) { ? ? ? ? ? ? ? ? if (mSeekPosition > 0) { ? ? ? ? ? ? ? ? ? ? mVideoView.seekTo(mSeekPosition); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? mVideoView.start(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? ? @Override ? ? protected void onPause() { ? ? ? ? super.onPause(); ? ? ? ? Log.d(TAG, "onPause "); ? ? ? ? if (mVideoView != null && mVideoView.isPlaying()) { ? ? ? ? ? ? mSeekPosition = mVideoView.getCurrentPosition(); ? ? ? ? ? ? Log.d(TAG, "onPause mSeekPosition=" + mSeekPosition); ? ? ? ? ? ? mVideoView.pause(); ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 置視頻區(qū)域大小 ? ? ?*/ ? ? private void setVideoAreaSize() { ? ? ? ? mVideoLayout.post(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? int width = mVideoLayout.getWidth(); ? ? ? ? ? ? ? ? cachedHeight = (int) (width * 405f / 720f); // ? ? ? ? ? ? ? ?cachedHeight = (int) (width * 3f / 4f); // ? ? ? ? ? ? ? ?cachedHeight = (int) (width * 9f / 16f); ? ? ? ? ? ? ? ? ViewGroup.LayoutParams videoLayoutParams = mVideoLayout.getLayoutParams(); ? ? ? ? ? ? ? ? videoLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; ? ? ? ? ? ? ? ? videoLayoutParams.height = cachedHeight; ? ? ? ? ? ? ? ? mVideoLayout.setLayoutParams(videoLayoutParams); ? ? ? ? ? ? ? ? mVideoView.setVideoPath(VIDEO_URL); ? ? ? ? ? ? ? ? mVideoView.requestFocus(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? //當(dāng)屏幕發(fā)生改變的時(shí)候,如果要保持進(jìn)度必須自己重寫onSaveInstanceState方法 ? ? @Override ? ? protected void onSaveInstanceState(Bundle outState) { ? ? ? ? super.onSaveInstanceState(outState); ? ? ? ? Log.d(TAG, "onSaveInstanceState Position=" + mVideoView.getCurrentPosition()); ? ? ? ? outState.putInt(SEEK_POSITION_KEY, mSeekPosition); ? ? } ? ? ? @Override ? ? protected void onRestoreInstanceState(Bundle outState) { ? ? ? ? super.onRestoreInstanceState(outState); ? ? ? ? mSeekPosition = outState.getInt(SEEK_POSITION_KEY); ? ? ? ? Log.d(TAG, "onRestoreInstanceState Position=" + mSeekPosition); ? ? } ? ? ? /** ? ? ?* 全屏和默認(rèn)的切換 ? ? ?* @param isFullscreen ? ? ?*/ ? ? @Override ? ? public void onScaleChange(boolean isFullscreen) { ? ? ? ? this.isFullscreen = isFullscreen; ? ? ? ? if (isFullscreen) { ? ? ? ? ? ? ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams(); ? ? ? ? ? ? layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; ? ? ? ? ? ? layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; ? ? ? ? ? ? mVideoLayout.setLayoutParams(layoutParams); ? ? ? ? ? ? mBottomLayout.setVisibility(View.GONE); ? ? ? ? ? ? ? //全屏就隱藏標(biāo)題欄 ? ? ? ? ? ? //titlebar.setVisibility(View.GONE); ? ? ? ? ? } else { ? ? ? ? ? ? ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams(); ? ? ? ? ? ? layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; ? ? ? ? ? ? layoutParams.height = this.cachedHeight; ? ? ? ? ? ? mVideoLayout.setLayoutParams(layoutParams); ? ? ? ? ? ? mBottomLayout.setVisibility(View.VISIBLE); ? ? ? ? ? ? //豎直方法的時(shí)候,顯示標(biāo)題欄 ? ? ? ? ? // ?titlebar.setVisibility(View.VISIBLE); ? ? ? ? } // ? ? ? ?switchTitleBar(!isFullscreen); ? ? } ? // ? ?private void switchTitleBar(boolean show) { // ? ? ? ?android.support.v7.app.ActionBar supportActionBar = getSupportActionBar(); // ? ? ? ?if (supportActionBar != null) { // ? ? ? ? ? ?if (show) { // ? ? ? ? ? ? ? ?supportActionBar.show(); // ? ? ? ? ? ?} else { // ? ? ? ? ? ? ? ?supportActionBar.hide(); // ? ? ? ? ? ?} // ? ? ? ?} // ? ?} ? ? ? @Override ? ? public void onPause(MediaPlayer mediaPlayer) { ? ? ? ? Log.d(TAG, "onPause UniversalVideoView callback"); ? ? } ? ? ? @Override ? ? public void onStart(MediaPlayer mediaPlayer) { ? ? ? ? Log.d(TAG, "onStart UniversalVideoView callback"); ? ? } ? ? ? @Override ? ? public void onBufferingStart(MediaPlayer mediaPlayer) { ? ? ? ? Log.d(TAG, "onBufferingStart UniversalVideoView callback"); ? ? } ? ? ? @Override ? ? public void onBufferingEnd(MediaPlayer mediaPlayer) { ? ? ? ? Log.d(TAG, "onBufferingEnd UniversalVideoView callback"); ? ? } ? ? ? @Override ? ? public void onBackPressed() { ? ? ? ? if (this.isFullscreen) { ? ? ? ? ? ? mVideoView.setFullscreen(false); ? ? ? ? } else { ? ? ? ? ? ? super.onBackPressed(); ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android音視頻開發(fā)之VideoView使用指南
- Android自定義videoview仿抖音界面
- Android原生視頻播放VideoView的使用
- android多媒體類VideoView使用方法詳解
- Android編程實(shí)現(xiàn)VideoView循環(huán)播放功能的方法
- Android多媒體之VideoView視頻播放器
- Android VideoView類實(shí)例講解
- Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法
- android使用videoview播放視頻
- Android中VideoView音視頻開發(fā)的實(shí)現(xiàn)
相關(guān)文章
android 使用kotlin 實(shí)現(xiàn)點(diǎn)擊更換全局語言(中日英切換)
這篇文章主要介紹了android kotlin 點(diǎn)擊更換全局語言的實(shí)現(xiàn)方法,這里主要介紹中日英切換,需要的朋友可以參考下2019-11-11詳解Android數(shù)據(jù)存儲之Android 6.0運(yùn)行時(shí)權(quán)限下文件存儲的思考
本篇文章主要介紹了Android數(shù)據(jù)存儲之Android 6.0運(yùn)行時(shí)權(quán)限下文件存儲的思考,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12Android 無障礙全局懸浮窗實(shí)現(xiàn)示例
本文主要介紹了Android 無障礙全局懸浮窗實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Android使用自定義view在指定時(shí)間內(nèi)勻速畫一條直線的實(shí)例代碼
這篇文章主要介紹了Android使用自定義view在指定時(shí)間內(nèi)勻速畫一條直線的實(shí)例代碼,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05基于Android SDK-在64位Linux中使用需要注意的問題
本篇文章是對Android SDK-在64位Linux中使用需要注意的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05基于Android實(shí)現(xiàn)的文件同步設(shè)計(jì)方案
隨著用戶對自身數(shù)據(jù)保護(hù)意識的加強(qiáng),讓用戶自己維護(hù)自己的數(shù)據(jù)也成了獨(dú)立開發(fā)產(chǎn)品時(shí)的一個(gè)賣點(diǎn),若只針對少量的文件進(jìn)行同步,則實(shí)現(xiàn)起來比較簡單,當(dāng)針對一個(gè)多層級目錄同步時(shí),情況就復(fù)雜多了,本文我分享下我的設(shè)計(jì)思路2023-10-10Android之AnimationDrawable簡單模擬動態(tài)圖
這篇文章主要為大家詳細(xì)介紹了Android之AnimationDrawable簡單模擬動態(tài)圖的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09