5步學(xué)會使用VideoView播放視頻
我們可以試想ImageView能顯示圖片,而VideoView就是用來顯示視頻的。
使用VideoView播放視頻的步驟如下
【1】在界面布局中定義VideoView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <VideoView android:id="@+id/videoview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <Button android:id="@+id/button" android:text="播放" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
【2】調(diào)用如下兩個方法加載指定視頻
setVideoPath(String Path);加載路徑下的視頻
setVideoURL(URL url);加載url所對應(yīng)的視頻。
mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");
【3】權(quán)限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
【4】調(diào)用
start()、stop()、pause()控制播放
【5】實(shí)際中常常結(jié)合MediaController類,它提供一個友好的圖像控制界面控制視頻播放;
mVideoView.setMediaController(new MediaController(MainActivity.this));
完整程序代碼如下
public class MainActivity extends Activity { private VideoView mVideoView; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mVideoView= (VideoView) findViewById(R.id.videoview); mButton= (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //得到sdcard下面aa.mp4的視頻文件 //兩種調(diào)用方式 // File videofile =new File("/mut/extSdCard/DCIM/Camera/20150915_160202.mp4"); // mVideoView.setVideoPath(videofile.getAbsolutePath()); mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/20150915_160202.mp4"); mVideoView.setMediaController(new MediaController(MainActivity.this)); mVideoView.start(); } }); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突
這篇文章主要為大家介紹了Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android?實(shí)現(xiàn)APP可切換多語言步驟詳解
如果是單獨(dú)給app加上國際化,其實(shí)很容易,創(chuàng)建對應(yīng)的國家資源文件夾即可,如values-en,values-pt,這篇文章主要介紹了Android?實(shí)現(xiàn)APP可切換多語言,需要的朋友可以參考下2023-11-11Android 全局Dialog的簡單實(shí)現(xiàn)方法
本篇文章主要介紹了Android 全局Dialog的簡單實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02SurfaceView開發(fā)[捉小豬]手機(jī)游戲 (一)
這篇文章主要介紹了用SurfaceView開發(fā)[捉小豬]手機(jī)游戲 (一)本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08android從系統(tǒng)圖庫中取圖片的實(shí)例代碼
這篇文章主要介紹了android從系統(tǒng)圖庫中取圖片的方法,涉及Android讀取及選擇圖片等相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07Android中okhttp3.4.1+retrofit2.1.0實(shí)現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結(jié)合retrofit2.1.0實(shí)現(xiàn)離線緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10浮動AppBar中的textField焦點(diǎn)回滾問題解決
這篇文章主要為大家介紹了浮動AppBar中的textField焦點(diǎn)回滾問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08