Android UniversalVideoView实现视频播放器
更新时间:2022年04月19日 11:22:19 作者:抱着回忆旅行
这篇文章主要为大家详细介绍了Android UniversalVideoView实现视频播放器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Android GSYVideoPlayer视频播放器功能的实现
- Android项目实现视频播放器
- Android使用vitamio插件实现视频播放器
- Android基于IJKPlayer视频播放器简单封装设计
- Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现
- Android多媒体之VideoView视频播放器
- android webvie指定视频播放器播放网站视频
- Android 使用Vitamio打造自己的万能播放器(10)—— 本地播放 (缩略图、视频信息、视频扫描服务)
- Android 使用Vitamio打造自己的万能播放器(7)——在线播放(下载视频)
- Android 使用Vitamio打造自己的万能播放器(5)——在线播放(播放优酷视频)
相关文章
FloatingActionButton增强版一个按钮跳出多个按钮第三方开源之FloatingActionButton
这篇文章主要介绍了FloatingActionButton增强版一个按钮跳出多个按钮第三方开源之FloatingActionButton 的相关资料,需要的朋友可以参考下2015-12-12
最新评论