Android原生視頻播放VideoView的使用
更新時間:2020年05月29日 11:50:24 作者:lxb_android
這篇文章主要為大家詳細介紹了Android原生視頻播放VideoView的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android原生視頻播放VideoView的具體代碼,供大家參考,具體內容如下
布局文件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>
對應的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);
//調用系統(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() {
//初始化進度條
progressBar = (ProgressBar) findViewById(R.id.progressBar);
//初始化VideoView
videoView = (VideoView) findViewById(R.id.videoView);
//初始化videoview控制條
mediaController = new MediaController(this);
//設置videoview的控制條
videoView.setMediaController(mediaController);
//設置顯示控制條
mediaController.show(0);
//設置播放完成以后監(jiān)聽
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
//設置發(fā)生錯誤監(jiān)聽,如果不設置videoview會向用戶提示發(fā)生錯誤
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
//設置在視頻文件在加載完畢以后的回調函數
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressBar.setVisibility(View.GONE);
videoView.start();
}
});
//設置videoView的點擊監(jiān)聽
videoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
//設置網絡視頻路徑
Uri uri = Uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4");
videoView.setVideoURI(uri);
//設置為全屏模式播放
setVideoViewLayoutParams(2);
}
/**
* 設置videiview的全屏和窗口模式
*
* @param paramsType 標識 1為全屏模式 2為窗口模式
*/
public void setVideoViewLayoutParams(int paramsType) {
//全屏模式
if (1 == paramsType) {
//設置充滿整個父布局
RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
//設置相對于父布局四邊對齊
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);
//設置窗口模式距離邊框50
int videoHeight = DisplayMetrics.heightPixels;
int videoWidth = DisplayMetrics.widthPixels;
RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(videoWidth, videoHeight);
//設置居中
LayoutParams.addRule(RelativeLayout.ALIGN_TOP);
//為VideoView添加屬性
videoView.setLayoutParams(LayoutParams);
}
}
/**
* 頁面暫停效果處理
*/
@Override
protected void onPause() {
super.onPause();
//如果當前頁面暫停則保存當前播放位置,全局變量保存
intPositionWhenPause = videoView.getCurrentPosition();
//停止回放視頻文件
videoView.stopPlayback();
}
/**
* 頁面從暫停中恢復
*/
@Override
protected void onResume() {
super.onResume();
//跳轉到暫停時保存的位置
if (intPositionWhenPause >= 0) {
videoView.seekTo(intPositionWhenPause);
//初始播放位置
intPositionWhenPause = -1;
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在Android模擬器上模擬GPS功能總是null的解決方法
在我們開發(fā)時需要在模擬器上模擬GPS,可在Location的時候總是null,下面與大家分享下具體的解決方法,感興趣的朋友可以參考下哈2013-06-06

