5步学会使用VideoView播放视频

Nafisa ·
更新时间:2024-09-21
· 935 次阅读

我们可以试想ImageView能显示图片,而VideoView就是用来显示视频的。

使用VideoView播放视频的步骤如下

【1】在界面布局中定义VideoView

<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <VideoView android:id="@+id/videoview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <Button android:id="@+id/button" android:text="播放" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

【2】调用如下两个方法加载指定视频

setVideoPath(String Path);加载路径下的视频
setVideoURL(URL url);加载url所对应的视频。

mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");

【3】权限

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

【4】调用

start()、stop()、pause()控制播放

【5】实际中常常结合MediaController类,它提供一个友好的图像控制界面控制视频播放;

mVideoView.setMediaController(new MediaController(MainActivity.this));

完整程序代码如下

public class MainActivity extends Activity { private VideoView mVideoView; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mVideoView= (VideoView) findViewById(R.id.videoview); mButton= (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //得到sdcard下面aa.mp4的視頻文件 //两种调用方式 // File videofile =new File("/mut/extSdCard/DCIM/Camera/20150915_160202.mp4"); // mVideoView.setVideoPath(videofile.getAbsolutePath()); mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/20150915_160202.mp4"); mVideoView.setMediaController(new MediaController(MainActivity.this)); mVideoView.start(); } }); } } 您可能感兴趣的文章:Android VideoCache视频缓存的方法详解Android视频点播的实现代码(边播边缓存)Android音频录制MediaRecorder之简易的录音软件实现代码Android提高之MediaPlayer播放网络音频的实现方法Android音频可视化开发案例说明Android使用音频信息绘制动态波纹Android音频系统AudioTrack使用方法详解基于VideoView自定义控制面板的视频播放器VideoView实现视频无缝连续播放Android视频/音频缓存框架AndroidVideoCache(Okhttp)详解



videoview

需要 登录 后方可回复, 如果你还没有账号请 注册新账号