Android原生視頻播放VideoView的使用
本文實(shí)例為大家分享了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>
對(duì)應(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)聽(tīng)
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
//設(shè)置發(fā)生錯(cuò)誤監(jiān)聽(tīng),如果不設(shè)置videoview會(huì)向用戶提示發(fā)生錯(cuò)誤
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的點(diǎn)擊監(jiān)聽(tīng)
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)識(shí) 1為全屏模式 2為窗口模式
*/
public void setVideoViewLayoutParams(int paramsType) {
//全屏模式
if (1 == paramsType) {
//設(shè)置充滿整個(gè)父布局
RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
//設(shè)置相對(duì)于父布局四邊對(duì)齊
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 {
//窗口模式
//獲取整個(gè)屏幕的寬高
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);
}
}
/**
* 頁(yè)面暫停效果處理
*/
@Override
protected void onPause() {
super.onPause();
//如果當(dāng)前頁(yè)面暫停則保存當(dāng)前播放位置,全局變量保存
intPositionWhenPause = videoView.getCurrentPosition();
//停止回放視頻文件
videoView.stopPlayback();
}
/**
* 頁(yè)面從暫停中恢復(fù)
*/
@Override
protected void onResume() {
super.onResume();
//跳轉(zhuǎn)到暫停時(shí)保存的位置
if (intPositionWhenPause >= 0) {
videoView.seekTo(intPositionWhenPause);
//初始播放位置
intPositionWhenPause = -1;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android UniversalVideoView實(shí)現(xiàn)視頻播放器
- Android音視頻開(kāi)發(fā)之VideoView使用指南
- Android自定義videoview仿抖音界面
- android多媒體類(lèi)VideoView使用方法詳解
- Android編程實(shí)現(xiàn)VideoView循環(huán)播放功能的方法
- Android多媒體之VideoView視頻播放器
- Android VideoView類(lèi)實(shí)例講解
- Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法
- android使用videoview播放視頻
- Android中VideoView音視頻開(kāi)發(fā)的實(shí)現(xiàn)
相關(guān)文章
Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗a
這篇文章主要介紹了Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗a的相關(guān)資料,需要的朋友可以參考下2016-12-12
在Android模擬器上模擬GPS功能總是null的解決方法
在我們開(kāi)發(fā)時(shí)需要在模擬器上模擬GPS,可在Location的時(shí)候總是null,下面與大家分享下具體的解決方法,感興趣的朋友可以參考下哈2013-06-06
Android?hid發(fā)送apdu格式數(shù)據(jù)示例詳解
這篇文章主要介紹了Android?hid發(fā)送apdu格式數(shù)據(jù),在?Android?中,如果你想通過(guò)?HID(Human?Interface?Device)發(fā)送?APDU?格式的數(shù)據(jù),通常會(huì)涉及?USB?HID?設(shè)備或藍(lán)牙?HID?設(shè)備,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-08-08
Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比
這篇文章主要介紹了 Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
這篇文章將介紹如何定制當(dāng)今主流的對(duì)話框,通過(guò)自定義dialog實(shí)現(xiàn)加載對(duì)話框效果,具體實(shí)現(xiàn)代碼大家通過(guò)本文學(xué)習(xí)吧2018-05-05
Android?Studio實(shí)現(xiàn)彈窗設(shè)置
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)彈窗設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android內(nèi)置SQLite的使用詳細(xì)介紹
這篇文章主要介紹了Android內(nèi)置SQLite的使用詳細(xì)介紹,文章通過(guò)文章展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

