Android原生視頻播放VideoView的使用
更新時間:2020年05月29日 11:50:24 作者:lxb_android
這篇文章主要為大家詳細(xì)介紹了Android原生視頻播放VideoView的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android原生視頻播放VideoView的具體代碼,供大家參考,具體內(nèi)容如下
布局文件activity_video.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
對應(yīng)的Avtivity:VideoActivity.java
package com.example.administrator.main; import android.content.Intent; import android.media.MediaPlayer; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.widget.MediaController; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.VideoView; public class VideoActivity extends AppCompatActivity { private ProgressBar progressBar; private VideoView videoView; private MediaController mediaController; private int intPositionWhenPause = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); //調(diào)用系統(tǒng)自帶視頻播放或者安裝的第三方播放器 // Uri uri=Uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4"); // Intent intent=new Intent(Intent.ACTION_VIEW); // intent.setDataAndType(uri,"video/*"); // startActivity(intent); initVideoView(); } /** * 初始化videoview播放 */ public void initVideoView() { //初始化進(jìn)度條 progressBar = (ProgressBar) findViewById(R.id.progressBar); //初始化VideoView videoView = (VideoView) findViewById(R.id.videoView); //初始化videoview控制條 mediaController = new MediaController(this); //設(shè)置videoview的控制條 videoView.setMediaController(mediaController); //設(shè)置顯示控制條 mediaController.show(0); //設(shè)置播放完成以后監(jiān)聽 videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { } }); //設(shè)置發(fā)生錯誤監(jiān)聽,如果不設(shè)置videoview會向用戶提示發(fā)生錯誤 videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { return false; } }); //設(shè)置在視頻文件在加載完畢以后的回調(diào)函數(shù) videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { progressBar.setVisibility(View.GONE); videoView.start(); } }); //設(shè)置videoView的點擊監(jiān)聽 videoView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); //設(shè)置網(wǎng)絡(luò)視頻路徑 Uri uri = Uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4"); videoView.setVideoURI(uri); //設(shè)置為全屏模式播放 setVideoViewLayoutParams(2); } /** * 設(shè)置videiview的全屏和窗口模式 * * @param paramsType 標(biāo)識 1為全屏模式 2為窗口模式 */ public void setVideoViewLayoutParams(int paramsType) { //全屏模式 if (1 == paramsType) { //設(shè)置充滿整個父布局 RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); //設(shè)置相對于父布局四邊對齊 LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); //為VideoView添加屬性 videoView.setLayoutParams(LayoutParams); } else { //窗口模式 //獲取整個屏幕的寬高 DisplayMetrics DisplayMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(DisplayMetrics); //設(shè)置窗口模式距離邊框50 int videoHeight = DisplayMetrics.heightPixels; int videoWidth = DisplayMetrics.widthPixels; RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(videoWidth, videoHeight); //設(shè)置居中 LayoutParams.addRule(RelativeLayout.ALIGN_TOP); //為VideoView添加屬性 videoView.setLayoutParams(LayoutParams); } } /** * 頁面暫停效果處理 */ @Override protected void onPause() { super.onPause(); //如果當(dāng)前頁面暫停則保存當(dāng)前播放位置,全局變量保存 intPositionWhenPause = videoView.getCurrentPosition(); //停止回放視頻文件 videoView.stopPlayback(); } /** * 頁面從暫停中恢復(fù) */ @Override protected void onResume() { super.onResume(); //跳轉(zhuǎn)到暫停時保存的位置 if (intPositionWhenPause >= 0) { videoView.seekTo(intPositionWhenPause); //初始播放位置 intPositionWhenPause = -1; } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android UniversalVideoView實現(xiàn)視頻播放器
- Android音視頻開發(fā)之VideoView使用指南
- Android自定義videoview仿抖音界面
- android多媒體類VideoView使用方法詳解
- Android編程實現(xiàn)VideoView循環(huán)播放功能的方法
- Android多媒體之VideoView視頻播放器
- Android VideoView類實例講解
- Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法
- android使用videoview播放視頻
- Android中VideoView音視頻開發(fā)的實現(xiàn)
相關(guān)文章
Android編程之SQLite數(shù)據(jù)庫操作方法詳解
這篇文章主要介紹了Android編程之SQLite數(shù)據(jù)庫操作方法,簡單介紹了SQLite數(shù)據(jù)庫及Android操作SQLite數(shù)據(jù)庫的步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-08-08android實現(xiàn)可自由移動、監(jiān)聽點擊事件的懸浮窗
這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)可自由移動、監(jiān)聽點擊事件的懸浮窗,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android使用IntentService進(jìn)行apk更新示例代碼
這篇文章主要介紹了Android使用IntentService進(jìn)行apk更新示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Android RxJava創(chuàng)建操作符Interval
這篇文章主要為大家詳細(xì)介紹了Android RxJava創(chuàng)建操作符Interval的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12詳解Android中Activity的啟動模式及應(yīng)用場景
今天給大家介紹下安卓開發(fā)中不得不涉及的Activity啟動模式及應(yīng)用場景,Activity一共有四種啟動模式,分別是Standard模式、SingleTop模式、SingleTask模式以及SingleInstance模式,,需要的朋友可以參考下2023-09-09