5步學會使用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】調用如下兩個方法加載指定視頻
setVideoPath(String Path);加載路徑下的視頻
setVideoURL(URL url);加載url所對應的視頻。
mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");
【3】權限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
【4】調用
start()、stop()、pause()控制播放
【5】實際中常常結合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的視頻文件
//兩種調用方式
// 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();
}
});
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
android隱式意圖激活自定義界面和系統(tǒng)應用界面的實例
下面小編就為大家?guī)硪黄猘ndroid隱式意圖激活自定義界面和系統(tǒng)應用界面的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Android Studio 中的Gradle構建系統(tǒng)示例
這篇文章主要介紹了Android Studio 中的Gradle構建系統(tǒng)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android?MaterialAlertDialogBuilder修改按鈕屬性
這篇文章主要介紹了Android?MaterialAlertDialogBuilder修改按鈕屬性實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

