<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android UniversalVideoView實現視訊播放器的具體程式碼,供大家參考,具體內容如下
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); //設定播放螢幕模式和設定播放地址 setVideoAreaSize(); //設定螢幕狀態和播放狀態的監聽 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(); } } /** * 置視訊區域大小 */ 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(); } }); } //當螢幕發生改變的時候,如果要保持進度必須自己重寫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); } /** * 全螢幕和預設的切換 * @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); //全螢幕就隱藏標題列 //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); //豎直方法的時候,顯示標題列 // 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(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45