国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 使用VideoView播放MP4的簡單實(shí)現(xiàn)

2019-10-22 18:10:32
字體:
供稿:網(wǎng)友

使用VideoView播放MP4

Android,VideoView,MP4

播放示例

實(shí)現(xiàn)簡單的播放功能,播放手機(jī)本地的MP4文件。不依賴任何第三方框架,不添加任何防腐劑。
添加一個(gè)系統(tǒng)自帶的控制條。

相關(guān)代碼請參閱: https://github.com/RustFisher/android-MediaPlayer/tree/master/appMp4

申請權(quán)限

讀取存儲中的MP4文件

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

準(zhǔn)備布局文件

frag_video_view.xml中放置VideoView;為了讓內(nèi)容居中顯示,將其套在LinearLayout中,并選擇android:layout_gravity="center"。否則可能會出現(xiàn)視頻內(nèi)容不居中的情況。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@android:color/black">  <LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <VideoView      android:id="@+id/video_view"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:layout_gravity="center" />  </LinearLayout>  <TextView    android:id="@+id/path_tv"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:textColor="@android:color/white"    android:textSize="13sp" /></RelativeLayout>

在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視圖創(chuàng)建完畢時(shí),設(shè)置MP4文件路徑,添加控制器,調(diào)整到最開始的地方,開始從頭播放。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桓台县| 阿克陶县| 康定县| 淮滨县| 富平县| 永康市| 东源县| 新巴尔虎左旗| 越西县| 山西省| 抚松县| 黎平县| 宿迁市| 邵阳县| 思茅市| 东台市| 东源县| 怀远县| 习水县| 玛纳斯县| 武强县| 阿城市| 缙云县| 康平县| 佛坪县| 灵宝市| 苏尼特右旗| 腾冲县| 若尔盖县| 通州区| 鄂尔多斯市| 建阳市| 铜梁县| 陇川县| 仁怀市| 青州市| 二连浩特市| 安宁市| 阿荣旗| 洛阳市| 筠连县|