mp4 android自动播放,Android 使用VideoView播放MP4的简单实现

使用VideoView播放MP4

489f28698c8e4172f4f503793148d78e.gif

播放示例

实现简单的播放功能,播放手机本地的MP4文件。不依赖任何第三方框架,不添加任何防腐剂。

添加一个系统自带的控制条。

申请权限

读取存储中的MP4文件

准备布局文件

在frag_video_view.xml中放置VideoView;为了让内容居中显示,将其套在LinearLayout中,并选择android:layout_gravity="center"。否则可能会出现视频内容不居中的情况。

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/black">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/video_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center" />

android:id="@+id/path_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textColor="@android:color/white"

android:textSize="13sp" />

在Fragment中直接播放视频文件;

private static String mMP4Path;

VideoView mVideoView;

MediaController mMediaController;

@Override

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

super.onViewCreated(view, savedInstanceState);

TextView pathTv = view.findViewById(R.id.path_tv);

mVideoView = view.findViewById(R.id.video_view);

mMediaController = new MediaController(getContext());

if (!TextUtils.isEmpty(mMP4Path)) {

mVideoView.setVideoPath(mMP4Path);

mVideoView.setMediaController(mMediaController);

mVideoView.seekTo(0);

mVideoView.requestFocus();

mVideoView.start();

pathTv.setText(mMP4Path);

}

}

Fragment视图创建完毕时,设置MP4文件路径,添加控制器,调整到最开始的地方,开始从头播放。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值