好久没接触过视频这方面的了,但最近需求有需要所以又回来捡捡以前的知识点。需求有些简单,也就是加载本地视频播放并会重复播放。
所以网上也看参考了一下,写了个小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;
@Override
protected 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视频文件就行了。最后运行能用。
原文链接:https://blog.csdn.net/llixiangjian/article/details/73197643
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~