安卓视频播放器(VideoView)

电脑杂谈  发布时间:2019-09-15 11:06:14  来源:网络整理

videoview_videoview播放黑屏_android videoview

VideoView是安卓自带的视频播放器类,该类集成有显示和控制两大部分,在发力文件中添加VideoView然后在java文件中简单的读取控制命令,即可实现本地或者网络视频的播放。本章实现视频的居中播放、网络视频播放、本地视频播放、视频卡顿、网络连接出错、视频外自定义视频控件、视频内自定义视频控件等。

支持的格式:flv、3gp、mp4

void start(); //开始播放

void pause(); //暂停

void resume(); //重新播放,使用时必须在本句后加上开始播放

void seekTo(int msec); //从第几毫秒开始播放

void stopPlayback(); //停止播放并释放资源

int getCurrentPosition(); //获取当前播放的位置。

int getDuration(); //获取当前播放视频的总长度。

void setVideoPath(String path); //以文件模式的形式设定VideoView播放的视频源。

void setVideoURI(Uri uri); //以Uri的形式设置VideoView播放的视频源,可以是网路Uri或本地Uri

videoview播放黑屏_videoview_android videoview

void isPlaying(); //当前VideoView是否在播放视频

setMediaController(MediaController controller); //设置MediaController控制器

setOnCompletionListener(MediaPlayer.onCompletionListener l); //播放完成的事件

setOnErrorListener(MediaPlayer.OnErrorListener l); //播放出现出错之后的事件

setOnPreparedListener(MediaPlayer.OnPreparedListener l); //视频装载完成的事件

setOnInfoListener(new MediaPlayer.OnInfoListener(){}); //视频卡顿

布局文件:

实现视频播放重要在于VideoView标签,如果让该标签的父级标签为FramLayout,设置相应属性就可以推动视频居中播放和默认的播放控件 MediaController 在视频内显示效果,还可以在VidoView标签上层加入TextView标签,实现视频播放的一些状况显示,如播放卡顿、切换播放、播放失败等的提醒信息显示。另外可加入ImageView标签,即可显示照片或gif图android videoview,更具人性化。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="0dp"
 7     android:paddingLeft="0dp"
 8     android:paddingRight="0dp"
 9     android:orientation="vertical"
10     android:paddingTop="0dp"
11     tools:context="com.example.videodong.MainActivity">
12 
13     <TextView
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="视频播放器:\n"
17         android:textSize="15dp"
18         android:id="@+id/textView" />
19     <FrameLayout
20         android:layout_width="match_parent"
21         android:background="@color/colorBlack"
22         android:layout_height="300dp">
23        <VideoView
24         android:layout_width="match_parent"
25         android:layout_gravity="center"
26         android:layout_height="300dp"
27         android:id="@+id/videoView"
28         />
29 
30         <TextView
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:textSize="15dp"
34             android:text="点击播放"
35             android:textColor="@color/colorWhite"
36             android:id="@+id/vv_text"
37             android:layout_gravity="center" />
38     </FrameLayout>
39 
40     <ProgressBar
41         android:id="@+id/vv_bar"
42         style="?android:attr/progressBarStyleHorizontal"
43         android:layout_width="match_parent"
44         android:layout_height="wrap_content"/>
45 
46         <TextView
47             android:layout_width="wrap_content"
48             android:text="时间轴为:0.00/000"
49             android:id="@+id/vv_starttime"
50             android:layout_height="wrap_content" />
51 
52 
53      <LinearLayout
54          android:layout_width="match_parent"
55          android:orientation="horizontal"
56          android:layout_gravity="center"
57          android:layout_height="wrap_content">
58          <TextView
59              android:layout_width="wrap_content"
60              android:layout_weight="1"
61              android:layout_height="wrap_content" />
62          <Button
63              android:layout_width="wrap_content"
64              android:text="开始"
65              android:id="@+id/vv_start"
66              android:layout_height="wrap_content" />
67          <Button
68              android:layout_width="wrap_content"
69              android:text="暂停"
70              android:id="@+id/vv_pause"
71              android:layout_height="wrap_content" />
72          <Button
73              android:layout_width="wrap_content"
74              android:text="重播"
75              android:id="@+id/vv_restart"
76              android:layout_height="wrap_content" />
77          <Button
78              android:layout_width="wrap_content"
79              android:text="下一个"
80              android:id="@+id/vv_next"
81              android:layout_height="wrap_content" />
82          
83      </LinearLayout>
84     <TextView
85         android:layout_width="wrap_content"
86         android:id="@+id/vv_state"
87         android:layout_height="wrap_content" />
88 </LinearLayout>

android videoview_videoview播放黑屏_videoview

View Code

需要添加的权限:

因为视频来源可以是网络以及本地,所以必须动态申请网络访问权限和本地文件读写权限,文件读写权限通常还必须自动获取

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

寻找到VideoView控件:

vv=(VideoView)findViewById(R.id.videoView);

视频预装完成:

1 //视频准备完成时进入
2 vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
3             @Override
4             public void onPrepared(MediaPlayer mp) {
5                 //设置屏幕提示信息为空
6                 vv_text.setText("");
7             }
8 });


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/bofangqi/article-122936-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 聂绪龙
      聂绪龙

      说明人家还是看不起我们

    热点图片
    拼命载入中...