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】實際中常常結(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隱式意圖激活自定義界面和系統(tǒng)應(yīng)用界面的實例
下面小編就為大家?guī)硪黄猘ndroid隱式意圖激活自定義界面和系統(tǒng)應(yīng)用界面的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Android Studio 中的Gradle構(gòu)建系統(tǒng)示例
這篇文章主要介紹了Android Studio 中的Gradle構(gòu)建系統(tǒng)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
android通過servlet服務(wù)器保存文件到手機
這篇文章主要為大家詳細介紹了android通過servlet服務(wù)器保存文件到手機,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
Android?MaterialAlertDialogBuilder修改按鈕屬性
這篇文章主要介紹了Android?MaterialAlertDialogBuilder修改按鈕屬性實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

