vedioview加载本地视频播放

来源:互联网 发布:上海菜鸟网络公司地址 编辑:程序博客网 时间:2024/06/05 08:42

好久没接触过视频这方面的了,但最近需求有需要所以又回来捡捡以前的知识点。需求有些简单,也就是加载本地视频播放并会重复播放。

   所以网上也看参考了一下,写了个小demo测试一下,三下五除二就添加好了,代码没有多少。好了不多说了直接上代码。

package com.example.video;import android.app.Activity;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.widget.VideoView;public class MainActivity extends Activity { VideoView videoView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);  videoView = (VideoView)findViewById(R.id.vv_video_View); /***          * 将播放器关联上一个音频或者视频文件          * videoView.setVideoURI(Uri uri)          * videoView.setVideoPath(String path)          * 以上两个方法都可以。          */          videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.test));//test是我的mp4视频        videoView.start();                //监听视频播放完的代码        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {                            @Override            public void onCompletion(MediaPlayer mPlayer) {            // TODO Auto-generated method stub            mPlayer.start();            mPlayer.setLooping(true);            }        });}}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent" android:layout_height="match_parent"      android:orientation="horizontal">      <VideoView         android:id="@+id/vv_video_View"         android:layout_height="match_parent"          android:layout_width="wrap_content"        </VideoView>  </LinearLayout>  
在res->raw放上.mp4视频文件就行了。最后运行能用。