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

播放示例
實(shí)現(xiàn)簡(jiǎn)單的播放功能,播放手機(jī)本地的MP4文件。不依賴任何第三方框架,不添加任何防腐劑。
添加一個(gè)系統(tǒng)自帶的控制條。
相關(guān)代碼請(qǐng)參閱: https://github.com/RustFisher/android-MediaPlayer/tree/master/appMp4
申請(qǐng)權(quán)限
讀取存儲(chǔ)中的MP4文件
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
準(zhǔn)備布局文件
在frag_video_view.xml中放置VideoView;為了讓內(nèi)容居中顯示,將其套在LinearLayout中,并選擇android:layout_gravity="center"。否則可能會(huì)出現(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)整到最開始的地方,開始從頭播放。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼
- Android簡(jiǎn)單的利用MediaRecorder進(jìn)行錄音的實(shí)例代碼
- Android應(yīng)用開發(fā):電話監(jiān)聽和錄音代碼示例
- Android App調(diào)用MediaRecorder實(shí)現(xiàn)錄音功能的實(shí)例
- Android開發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android錄音應(yīng)用實(shí)例教程
- 一個(gè)html5播放視頻的video控件只支持android的默認(rèn)格式mp4和3gp
- 詳解Android開發(fā)之MP4文件轉(zhuǎn)GIF文件
- Android錄音并且輸出為Mp4文件的方法教程
相關(guān)文章
Android編程簡(jiǎn)單設(shè)置ListView分割線的方法
這篇文章主要介紹了Android編程簡(jiǎn)單設(shè)置ListView分割線的方法,涉及Android布局簡(jiǎn)單操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Android點(diǎn)擊按鈕返回頂部實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android返回頂部實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android中Bitmap、File與Uri之間的簡(jiǎn)單記錄
這篇文章主要給大家介紹了關(guān)于Android中Bitmap、File與Uri之間的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動(dòng)配對(duì)
這篇文章主要為大家詳細(xì)介紹了Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動(dòng)配對(duì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android懸浮對(duì)話框(即點(diǎn)即關(guān)對(duì)話框)實(shí)現(xiàn)代碼
本文給大家介紹android懸浮對(duì)話框和即點(diǎn)即關(guān)閉對(duì)話框,本文介紹非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-03-03
android分享純圖片到QQ空間實(shí)現(xiàn)方式
今天小編就為大家分享一篇關(guān)于android分享純圖片到QQ空間實(shí)現(xiàn)方式,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
Android DateUtil時(shí)間工具類使用方法示例解析
這篇文章主要為大家介紹了Android DateUtil時(shí)間工具類使用方法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

