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)文章
SurfaceView開發(fā)[捉小豬]手機游戲 (一)
這篇文章主要介紹了用SurfaceView開發(fā)[捉小豬]手機游戲 (一)本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
Android中okhttp3.4.1+retrofit2.1.0實現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結(jié)合retrofit2.1.0實現(xiàn)離線緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

