Android 使用VideoView播放MP4的簡單實現(xiàn)
使用VideoView播放MP4
播放示例
實現(xiàn)簡單的播放功能,播放手機本地的MP4文件。不依賴任何第三方框架,不添加任何防腐劑。
添加一個系統(tǒng)自帶的控制條。
相關代碼請參閱: https://github.com/RustFisher/android-MediaPlayer/tree/master/appMp4
申請權限
讀取存儲中的MP4文件
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
準備布局文件
在frag_video_view.xml
中放置VideoView;為了讓內容居中顯示,將其套在LinearLayout
中,并選擇android:layout_gravity="center
"。否則可能會出現(xiàn)視頻內容不居中的情況。
<?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)建完畢時,設置MP4文件路徑,添加控制器,調整到最開始的地方,開始從頭播放。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android音頻錄制MediaRecorder之簡易的錄音軟件實現(xiàn)代碼
- Android簡單的利用MediaRecorder進行錄音的實例代碼
- Android應用開發(fā):電話監(jiān)聽和錄音代碼示例
- Android App調用MediaRecorder實現(xiàn)錄音功能的實例
- Android開發(fā)四大組件之實現(xiàn)電話攔截和電話錄音
- Android使用MediaRecorder實現(xiàn)錄音及播放
- Android錄音應用實例教程
- 一個html5播放視頻的video控件只支持android的默認格式mp4和3gp
- 詳解Android開發(fā)之MP4文件轉GIF文件
- Android錄音并且輸出為Mp4文件的方法教程
相關文章
Android UI實現(xiàn)單行文本水平觸摸滑動效果
這篇文章主要為大家詳細介紹了Android UI實現(xiàn)單行文本水平觸摸滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Android?studio開發(fā)實現(xiàn)計算器功能
這篇文章主要為大家詳細介紹了Android?studio開發(fā)實現(xiàn)計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Android實用小技巧之利用Lifecycle寫出更好維護的代碼
lifecycle是一個類,用于存儲有關組件(如Activity或Fragment)的生命周期狀態(tài)的信息,并允許其他對象觀察此狀態(tài),下面這篇文章主要給大家介紹了關于Android實用小技巧之利用Lifecycle寫出更好維護的代碼的相關資料,需要的朋友可以參考下2022-05-05